我正在从相机获取预览数据。它采用NV21格式。我想将预览保存到SD卡,即位图。我的代码是获取图像并保存它,但在图库中它没有捕获预览。它只是黑色矩形,这是代码。
public void processImage() {
Bitmap bitmap = null;
if (flag == true) {
flag = false;
if (mCamera != null) {
Camera.Parameters parameters = mCamera.getParameters();
int imageFormat = parameters.getPreviewFormat();
if (imageFormat == ImageFormat.NV21) {
Toast.makeText(mContext, "Format: NV21", Toast.LENGTH_SHORT)
.show();
int w = parameters.getPreviewSize().width;
int h = parameters.getPreviewSize().height;
YuvImage yuvImage = new YuvImage(mData, imageFormat, w, h,
null);
Rect rect = new Rect(0, 0, w, h);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvImage.compressToJpeg(rect, 100, baos);
byte[] jData = baos.toByteArray();
bitmap = BitmapFactory.decodeByteArray(jData, 0,
jData.length);
}
else if (imageFormat == ImageFormat.JPEG
|| imageFormat == ImageFormat.RGB_565) {
Toast.makeText(mContext, "Format: JPEG||RGB_565",
Toast.LENGTH_SHORT).show();
bitmap = BitmapFactory.decodeByteArray(mData, 0,
mData.length);
}
}
if (bitmap != null) {
saveImage(bitmap);
Toast.makeText(mContext, "Image Saved", Toast.LENGTH_SHORT)
.show();
} else
Toast.makeText(mContext, "Bitmap Null", Toast.LENGTH_SHORT)
.show();
}
}
答案 0 :(得分:0)
如果要保存NV21预览图像以便在库中查看,最简单的方法是从NV21字节数组创建一个YuvImage,然后将其压缩为文件输出流中的JPEG,如下面的代码所示:
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/imagename.jpg");
YuvImage yuvImage = new YuvImage(nv21bytearray, ImageFormat.NV21, width, height, null);
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, fos);
fos.close();
请注意,您可能想要更改保存图像的路径。