在cameraX中ImageCapture提供了takePicture 的2个方法:
public void takePicture(@NonNull Executor executor,
final @NonNull OnImageCapturedCallback callback)
public void takePicture(
final @NonNull OutputFileOptions outputFileOptions,
final @NonNull Executor executor,
final @NonNull OnImageSavedCallback imageSavedCallback)
其中方法二 网上有很多方法保存图片,这点就不记录了.
方法一 我研究的时候博文很少讲到,特别对这个 ImageCapture.OnImageCapturedCallback()的描素 太少了 ,所以我就研究了一番。
/**
* 拍摄照片
*/
@SuppressLint("RestrictedApi")
private void takenPictureInternal() {
if (mImageCapture != null) {
mImageCapture.takePicture(CameraXExecutors.mainThreadExecutor(), new ImageCapture.OnImageCapturedCallback() {
@SuppressLint("UnsafeOptInUsageError")
@Override
public void onCaptureSuccess(@NonNull ImageProxy image) {
super.onCaptureSuccess(image);
LogUtils.i("拍照图片格式:" + ImageUtilKt.getImageName(image.getFormat()));
if (image.getFormat() == YUV_420_888) {
//创建一个新空白位图
Bitmap bgBitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);
YuvToRgbConverter yuvToRgbConverter = new YuvToRgbConverter(mContext);
yuvToRgbConverter.yuvToRgb(image.getImage(), bgBitmap);
if (bgBitmap != null) {
String path = SDP + "photo/" + TimeExtUtils.getCurr7() + ".jpg";
boolean fileByDeleteOldFile = FileUtils.createFileByDeleteOldFile(path);
if (fileByDeleteOldFile) {
ImageUtils.save(bgBitmap, path, Bitmap.CompressFormat.JPEG);
LogUtils.i("保存路径:" + path);
ThreadUtils.runOnUiThread(() -> {
T.info("保存路径:" + path);
});
}
}
} else if (image.getFormat() == ImageFormat.JPEG) {
ImageProxy.PlaneProxy[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
int size = buffer.remaining();
byte[] jpeg = new byte[size];
buffer.get(jpeg, 0, size);
Bitmap bitmap = ImageUtils.getBitmap(jpeg, 0);
String path = SDP + "photo/" + TimeExtUtils.getCurr7() + ".jpg";
boolean save = ImageUtils.save(bitmap, path, Bitmap.CompressFormat.JPEG);
T.info("图片保存:" + (save ? "成功!!!" : "失败!!!"));
}
}
@Override
public void onError(@NonNull ImageCaptureException exception) {
super.onError(exception);
exception.printStackTrace();
}
//使用完image关闭
image.close();
});
// final ContentValues contentValues = new ContentValues();
// contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, CAPTURED_FILE_NAME
// + "_" + TimeExtUtils.getCurr7());
// contentValues.put(MediaStore.MediaColumns.MIME_TYPE, CAPTURED_FILE_NAME_END);
// //
// ImageCapture.OutputFileOptions outputFileOptions =
// new ImageCapture.OutputFileOptions.Builder(
// getContentResolver(),
// MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
// .build();
// mImageCapture.takePicture(outputFileOptions, CameraXExecutors.ioExecutor(),
// new ImageCapture.OnImageSavedCallback() {
// @Override
// public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
// StringBuilder builder = new StringBuilder();
// builder.append("图片保存路径:").append(outputFileResults.getSavedUri().getPath()).append("\n");
// LogUtils.i(builder);
// ThreadUtils.runOnUiThread(() -> {
// T.info(builder.toString());
// });
// }
//
// @Override
// public void onError(@NonNull ImageCaptureException exception) {
// exception.printStackTrace();
// }
// });
}
}
这个获取到的图片格式为 image.getFormat() == ImageFormat.JPEG.
通过获取 image.getPlanes()拿到字节数组,然后生成bitmap保存 ,这点也可以直接保存.
参考博文:cAndroid CameraX Analyzer ImageProxy YUV_420_888 to NV21 - 简书
|