从相机预览回调,保存数据为JPG或YUV格式

@Override
public void onPreviewFrame(final byte[] data, final Camera camera) {
// Log.d("eric", "onPreviewFrame");
...

Camera.Size size = camera.getParameters().getPreviewSize();
YuvImage image = new YuvImage(data,ImageFormat.NV21, size.width, size.height, null);
Bitmap bmp = null;
Log.d("eric", "count : " + count);
// test data if it's valid eric
if(count <= 50 && image != null) {
Log.d("eric", "enabled");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compressToJpeg(new Rect(0, 0, size.width, size.height), 80, stream);
bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
saveBitmap(bmp, "sssssssssssssss");
Log.d("eric", "saveBitmap");
count ++;
try {

stream.close();
}catch (IOException e) {
Log.d("eric", "eeeeeeeeeeeeeeeeeeeeeee " + e.getMessage());
}
}

if(count <= 50) {
saveYUV(data);
count ++;
}

} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
camera.addCallbackBuffer(new byte[calculateFrameSize(ImageFormat.NV21)]);
}
}

private void saveYUV(byte[] data) {
Log.e("eric", "保存YUV");
File f = getOutputMediaFile(4);
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
out.write(data);
out.flush();
out.close();
Log.i("eric", "YUV已经保存");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** 保存方法 */
public void saveBitmap(Bitmap bmp, String picName) {
Log.e("eric", "保存图片");
File f = getOutputMediaFile(1);
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
Log.i("eric", "已经保存");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/** Create a File for saving an image or video */
private File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.

// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("eric", "failed to create directory");
return null;
}
}

// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == 1){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ count + ".jpg");
} else if(type == 2) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else if(type == 3) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"AVD_"+ count + ".h264");
} else if(type == 4) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"YUV_"+ count + ".yuv");
} else {
Log.d("eric", "null : ");
return null;
}

Log.d("eric", "path : " + mediaFile.getAbsolutePath());
return mediaFile;
}

转载于:https://www.cnblogs.com/Jokeyyu/p/7261472.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Android Camera2的ImageReader函数示例代码,用于将Image的YUV数据转换为RGB格式的Bitmap并保存到本地。在这个示例中,我们将使用JavaCV库来进行YUV转RGB的操作,并使用Bitmap类来保存图像。 ```java private ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() { @Override public void onImageAvailable(ImageReader reader) { Image image = reader.acquireNextImage(); if (image == null) { return; } // 获取图像的宽度和高度 int width = image.getWidth(); int height = image.getHeight(); // 将YUV数据转换为RGB格式的Bitmap Mat yuvMat = new Mat(height + height / 2, width, CvType.CV_8UC1); ByteBuffer buffer = image.getPlanes()[0].getBuffer(); byte[] data = new byte[buffer.remaining()]; buffer.get(data); yuvMat.put(0, 0, data); Mat rgbMat = new Mat(height, width, CvType.CV_8UC3); Imgproc.cvtColor(yuvMat, rgbMat, Imgproc.COLOR_YUV2RGB_NV21); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Utils.matToBitmap(rgbMat, bitmap); // 保存Bitmap到本地 String fileName = "image_" + System.currentTimeMillis() + ".jpg"; String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + fileName; try { FileOutputStream outputStream = new FileOutputStream(filePath); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } image.close(); } }; ``` 请注意,这只是一个简单的示例代码,可能需要根据你的实际需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值