从相机预览回调,保存数据为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;
}
posted @ 2017-07-31 09:32  Jokeyyu  阅读(2363)  评论(0编辑  收藏  举报