Android: How to save a preview frame as jpeg image?
16,797
Solution 1
checkout this code. i hope it helps
camera.setPreviewCallback(new PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// TODO Auto-generated method stub
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
YuvImage image = new YuvImage(data, ImageFormat.NV21,
size.width, size.height, null);
Rect rectangle = new Rect();
rectangle.bottom = size.height;
rectangle.top = 0;
rectangle.left = 0;
rectangle.right = size.width;
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
image.compressToJpeg(rectangle, 100, out2);
DataInputStream in = new DataInputStream();
in.write(out2.toByteArray());
}
}
});
camera.startPreview();
Solution 2
You have to convert it manually, there are some examples on the android-developers list if you browse the archive - mostly dealing with the format (luminance/chrominance,etc) conversion, then writing the image to a bitmap, then saving to a file.
It's all a bit of a pain really.
Author by
Niko Gamulin
Engineer/Researcher, working in the field of Machine Learning.
Updated on June 09, 2022Comments
-
Niko Gamulin almost 2 years
I would like to save a preview frame as a jpeg image.
I have tried to write the following code:
public void onPreviewFrame(byte[] _data, Camera _camera) { if(settings.isRecording()) { Camera.Parameters params = _camera.getParameters(); params.setPictureFormat(PixelFormat.JPEG); _camera.setParameters(params); String path = "ImageDir" + frameCount; fileRW.setPath(path); fileRW.WriteToFile(_data); frameCount++; } }
but it's not possible to open a saved file as a jpeg image. Does anyone know how to save preview frames as jpeg images?
Thanks
-
Heysem Katibi over 11 yearsall android cameras dosent support jpeg format for preview data