I know the subject was on the board many times, but i can not get it work anyhow... I want to save view frames from preview to jpeg files. It looks more or less(code is simp
Simply saving to a jpeg is an easier task than converting to bitmap, no need for that YUV decoding code thanks to YuvImage
class.
import android.graphics.YuvImage;
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
try {
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
YuvImage image = new YuvImage(data, parameters.getPreviewFormat(),
size.width, size.height, null);
File file = new File(Environment.getExternalStorageDirectory(), "out.jpg");
FileOutputStream filecon = new FileOutputStream(file);
image.compressToJpeg(
new Rect(0, 0, image.getWidth(), image.getHeight()), 90,
filecon);
} catch (FileNotFoundException e) {
Toast toast = Toast
.makeText(getBaseContext(), e.getMessage(), 1000);
toast.show();
}
}
In order to avoid degraded JPEG image on the output (e.g. with interleaving green/red spots), you need to flush and close FileOutputStream
FileOutputStream filecon = new FileOutputStream(file);
image.compressToJpeg(
new Rect(0, 0, image.getWidth(), image.getHeight()), 90,
filecon);
filecon.flush();
filecon.close();
as I found out the width and height read from PreviewSize were wrong.... In other words the conversion went wrong because it was based on false values. After use a new way of setting the preview settings(from example deleivered by google) everything works fine.
EDIT:
Sorry for the quick answer above and long delay.
It was while ago and I can not dig the code now, but i think that I used:
http://developer.android.com/reference/android/hardware/Camera.Parameters.html#getSupportedPreviewSizes()
and took the first element from the list and set the values with
http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setPreviewSize(int, int)
i stored the width and height and used it to transform picture.
It dont know if it was the best solution, but it worked out.
If I am not mistaken, you want the width and height of the picture to decode, not the width and heigth of the preview.
Alternatively, if you DO need a bitmap for some reason, and/or want to do this without creating a YUVImage and compressing to JPEG, you can use the handy RenderScript 'ScriptIntrinsicYuvToRGB' (API 17+):
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
Bitmap bitmap = Bitmap.createBitmap(r.width(), r.height(), Bitmap.Config.ARGB_8888);
Allocation bmData = renderScriptNV21ToRGBA8888(
mContext, r.width(), r.height(), data);
bmData.copyTo(bitmap);
}
public Allocation renderScriptNV21ToRGBA8888(Context context, int width, int height, byte[] nv21) {
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);
Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
in.copyFrom(nv21);
yuvToRgbIntrinsic.setInput(in);
yuvToRgbIntrinsic.forEach(out);
return out;
}