HDR做180都旋转

main
Matthew 8 months ago
parent 1db258f1df
commit 3714d08296

@ -275,6 +275,7 @@ public class Camera2RawFragment extends Fragment
* ID of the current {@link CameraDevice}.
*/
private String mCameraId;
private boolean mHdr = false;
/**
* A {@link CameraCaptureSession } for camera preview.
@ -496,7 +497,7 @@ public class Camera2RawFragment extends Fragment
String currentDateTime = generateTimestamp();
File jpegFile = new File(Environment.
getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
"JPEG_" + currentDateTime + ".jpg");
"IMG_" + currentDateTime + ".jpg");
// Look up the ImageSaverBuilder for this request and update it with the file name
// based on the capture start time.
@ -579,6 +580,12 @@ public class Camera2RawFragment extends Fragment
@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
Activity activity = getActivity();
Intent intent = activity.getIntent();
if (intent != null) {
mHdr = intent.getBooleanExtra("hdr", false);
}
Resources resources = getResources();
ImageView imageView = (ImageView)view.findViewById(R.id.picture);
imageView.setImageDrawable(resources.getDrawable(R.drawable.ic_take_photo));
@ -1224,9 +1231,7 @@ public class Camera2RawFragment extends Fragment
// Create an ImageSaverBuilder in which to collect results, and add it to the queue
// of active requests.
ImageSaver.ImageSaverBuilder jpegBuilder = new ImageSaver.ImageSaverBuilder(activity)
.setCharacteristics(mCharacteristics);
ImageSaver.ImageSaverBuilder rawBuilder = new ImageSaver.ImageSaverBuilder(activity)
.setCharacteristics(mCharacteristics);
.setCharacteristics(mCharacteristics).setHdr(mHdr);
mJpegResultQueue.put((int) request.getTag(), jpegBuilder);
@ -1328,6 +1333,8 @@ public class Camera2RawFragment extends Fragment
*/
private final CaptureResult mCaptureResult;
private final boolean mHdr;
/**
* The CameraCharacteristics for this camera device.
*/
@ -1343,11 +1350,12 @@ public class Camera2RawFragment extends Fragment
*/
private final RefCountedAutoCloseable<ImageReader> mReader;
private ImageSaver(Image image, File file, CaptureResult result,
private ImageSaver(boolean hdr, Image image, File file, CaptureResult result,
CameraCharacteristics characteristics, Context context,
RefCountedAutoCloseable<ImageReader> reader) {
mImage = image;
mFile = file;
mHdr = hdr;
mCaptureResult = result;
mCharacteristics = characteristics;
mContext = context;
@ -1375,8 +1383,16 @@ public class Camera2RawFragment extends Fragment
success = bm2.compress(Bitmap.CompressFormat.JPEG, 95, output);
bm2.recycle();
} else {
output.write(bytes);
success = true;
if (mHdr) {
Bitmap bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
Bitmap bm2 = flipBitmap180(bm);
bm.recycle();
success = bm2.compress(Bitmap.CompressFormat.JPEG, 95, output);
bm2.recycle();
} else {
output.write(bytes);
success = true;
}
}
} catch (IOException e) {
e.printStackTrace();
@ -1444,6 +1460,20 @@ public class Camera2RawFragment extends Fragment
return new2;
}
private Bitmap flipBitmap180(Bitmap bitmap) {
if (null == bitmap) {
return null;
}
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix m = new Matrix();
// m.postScale(-1, 1); //镜像水平翻转
m.postScale(1, -1); //镜像垂直翻转
//m.postRotate(-90); //旋转-90度
Bitmap new2 = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, true);
return new2;
}
/**
* Builder class for constructing {@link ImageSaver}s.
* <p/>
@ -1452,6 +1482,7 @@ public class Camera2RawFragment extends Fragment
public static class ImageSaverBuilder {
private Image mImage;
private File mFile;
private boolean mHdr;
private CaptureResult mCaptureResult;
private CameraCharacteristics mCharacteristics;
private Context mContext;
@ -1481,6 +1512,11 @@ public class Camera2RawFragment extends Fragment
return this;
}
public synchronized ImageSaverBuilder setHdr(final boolean hdr) {
mHdr = hdr;
return this;
}
public synchronized ImageSaverBuilder setFile(final File file) {
if (file == null) throw new NullPointerException();
mFile = file;
@ -1504,7 +1540,7 @@ public class Camera2RawFragment extends Fragment
if (!isComplete()) {
return null;
}
return new ImageSaver(mImage, mFile, mCaptureResult, mCharacteristics, mContext,
return new ImageSaver(mHdr, mImage, mFile, mCaptureResult, mCharacteristics, mContext,
mReader);
}
@ -1685,7 +1721,7 @@ public class Camera2RawFragment extends Fragment
* @return a {@link String} representing a time.
*/
private static String generateTimestamp() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS", Locale.US);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS", Locale.US);
return sdf.format(new Date());
}
@ -1737,7 +1773,7 @@ public class Camera2RawFragment extends Fragment
}
private int getRotationAdjustment() {
return TextUtils.equals(mCameraId, "1") ? 90 : 0;
return TextUtils.equals(mCameraId, "1") ? 270 : 0;
}
/**

@ -43,12 +43,12 @@ public class MainActivity extends Activity {
protected void takePhoto(int channel, boolean hdr) {
int cameraId = channel - 1;
if (channel == 1 && hdr) {
cameraId = 1;
}
Intent intent = new Intent(MainActivity.this, CameraActivity.class);
intent.putExtra("cameraId", cameraId);
if (channel == 1) {
intent.putExtra("hdr", hdr);
}
startActivity(intent);
}
}
Loading…
Cancel
Save