Android:如何将预览框保存为jpeg图像?

时间:2021-11-22 16:22:36

I would like to save a preview frame as a jpeg image.

我想将预览框保存为jpeg图像。

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?

但是无法将保存的文件作为jpeg图像打开。有谁知道如何将预览帧保存为jpeg图像?

Thanks

6 个解决方案

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

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

您必须手动转换它,如果浏览存档,android-developers列表中有一些示例 - 主要处理格式(亮度/色度等)转换,然后将图像写入位图,然后保存到文件。

It's all a bit of a pain really.

这真的有点痛苦。

#3


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

我在预览之前用Camera.Parameters.setPreviewFormat(PixelFormat.JPEG)设置PreviewFormat,但似乎它无法真正设置预格式......顺便说一下,预览的默认格式是YCbCr_420_SP .. ..

#4


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

您必须首先通过调用getSupportedPreviewFormats()来检查设备支持的预览格式。在调用setPreviewFormat(PixelFormat.JPEG)之前,请确保支持JPEG。

#5


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

JPEG不是Camera Preview的格式。正如官方文件所说,

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

“现在只支持ImageFormat.NV21和ImageFormat.YUY2”

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

要从Camera Preview获取图片,您需要定义预览格式,如下所示:

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

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

之后,您可以像在Dany的示例中那样压缩和保存JPEG。

#6


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

_data可能不是JPEG格式。你在调用开始预览之前调用了Camera.Parameters.setPreviewFormat(PixelFormat.JPEG)吗?

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

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

您必须手动转换它,如果浏览存档,android-developers列表中有一些示例 - 主要处理格式(亮度/色度等)转换,然后将图像写入位图,然后保存到文件。

It's all a bit of a pain really.

这真的有点痛苦。

#3


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

我在预览之前用Camera.Parameters.setPreviewFormat(PixelFormat.JPEG)设置PreviewFormat,但似乎它无法真正设置预格式......顺便说一下,预览的默认格式是YCbCr_420_SP .. ..

#4


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

您必须首先通过调用getSupportedPreviewFormats()来检查设备支持的预览格式。在调用setPreviewFormat(PixelFormat.JPEG)之前,请确保支持JPEG。

#5


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

JPEG不是Camera Preview的格式。正如官方文件所说,

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

“现在只支持ImageFormat.NV21和ImageFormat.YUY2”

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

要从Camera Preview获取图片,您需要定义预览格式,如下所示:

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

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

之后,您可以像在Dany的示例中那样压缩和保存JPEG。

#6


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

_data可能不是JPEG格式。你在调用开始预览之前调用了Camera.Parameters.setPreviewFormat(PixelFormat.JPEG)吗?