Niko Gamulin
Niko Gamulin

Reputation: 66575

Android: How to save a preview frame as jpeg image?

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

Upvotes: 10

Views: 17788

Answers (6)

Marko
Marko

Reputation: 151

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.

Upvotes: 0

Dany's
Dany's

Reputation: 928

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();

Upvotes: 9

artsylar
artsylar

Reputation: 2678

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).

Upvotes: 0

piemmm
piemmm

Reputation: 11

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.

Upvotes: 1

dakui
dakui

Reputation:

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....

Upvotes: 0

hacken
hacken

Reputation: 2145

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

Upvotes: -1

Related Questions