10

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

6 Answers 6

9

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(); 
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

I set the PreviewFormat with Camera.Parameters.setPreviewFormat(PixelFormat.JPEG) before preview,but it seems that it can't really set the previewformat...... By the way, the default format of the preview is YCbCr_420_SP....

Comments

0

You must first check what are the supported preview formats for your device by calling getSupportedPreviewFormats(). Make sure JPEG is supported before calling setPreviewFormat(PixelFormat.JPEG).

Comments

0

JPEG is not a format for Camera Preview. As official documentation says,

"Only ImageFormat.NV21 and ImageFormat.YUY2 are supported for now"

In order to get a picture from Camera Preview, you need to define preview format, as below:

Camera.Parameters parameters; parameters.setPreviewFormat(ImageFormat.NV21); //or ImageFormat.YU2 

After that, you compress & save JPEG as in Dany's example.

Comments

-1

_data probably isn't in JPEG format. Did you call Camera.Parameters.setPreviewFormat(PixelFormat.JPEG) before calling start preview?

1 Comment

all android cameras dosent support jpeg format for preview data

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.