ANDROID:将 PreviewCallback 图像保存为字节数组中的 .jpg 会导致文件损坏

2022-01-24 00:00:00 save android photo zxing java

我一直在扩充 QR 扫描库 Zxing,以便在扫描后立即保存照片.我被建议在 PreviewCallback.java 中的 onPreviewFrame 方法中这样做:

I have been augmenting the QR scanning library Zxing to save a photo instantly upon scan. I was advised to do so within the onPreviewFrame method within PreviewCallback.java as thus:

 public void onPreviewFrame(byte[] data, Camera camera) {
Point cameraResolution = configManager.getCameraResolution();
Handler thePreviewHandler = previewHandler;

YuvImage im = new YuvImage(data, ImageFormat.NV21, 1200,
        800, null);
        Rect r = new Rect(0, 0, 1200, 800);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        im.compressToJpeg(r, 50, baos);

        try {
            FileOutputStream output = new FileOutputStream("/sdcard/test_jpg.jpg");
            output.write(baos.toByteArray());
            output.flush();
            output.close();

            System.out.println("Attempting to save file");
            System.out.println(data);
        } catch (FileNotFoundException e) {
            System.out.println("Saving to file failed");
        } catch (IOException e) {
            System.out.println("Saving to file failed");
        }

if (cameraResolution != null && thePreviewHandler != null) {
  Message message = thePreviewHandler.obtainMessage(previewMessage, cameraResolution.x,
      cameraResolution.y, data);
  message.sendToTarget();
  previewHandler = null;

} else {
  Log.d(TAG, "Got preview callback, but no handler or resolution available");
}}

运行此代码的结果是设置文件目录中的图像损坏.我相信这是由于每帧都运行代码.如果允许保存完整图像,有没有办法将其限制为每秒左右,或者有没有一种方法可以让图像仅在完成扫描时保存.

The result of running this code is a corrupt image at the set file directory. I believe this is due to the code being run every frame. Is there a way to limit this to every second or so if that will allow the full image to save, or is there a method I can use to cause the image to only save upon completed scan.

我有一个不太有利的工作选择,因为我可以成功保存扫描时显示的黑白图像;颜色当然是首选.

I have a less favourable working alternative, in that I can successfully save the black and white image that is shown upon scan; colour is the preferable option of course.

更新:代码更改为(理论上)适应任何设备上的相机分辨率.图像仍然损坏.

Update: Code changed to (in theory) accommodate camera resolution on any device. Image is still corrupt.

public void onPreviewFrame(byte[] data, Camera camera) {
Point cameraResolution = configManager.getCameraResolution();
Handler thePreviewHandler = previewHandler;

android.hardware.Camera.Parameters parameters = camera.getParameters();
android.hardware.Camera.Size size = parameters.getPictureSize();

int height = size.height;
int width = size.width;

YuvImage im = new YuvImage(data, ImageFormat.NV21, width,
        height, null);
        Rect r = new Rect(0, 0, width, height);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        im.compressToJpeg(r, 50, baos);

        try {
            FileOutputStream output = new FileOutputStream("/sdcard/test_jpg.jpg");
            output.write(baos.toByteArray());
            output.flush();
            output.close();

            System.out.println("Attempting to save file");
            System.out.println(data);

        } catch (FileNotFoundException e) {             
            System.out.println("Saving to file failed");                
        } catch (IOException e) {               
            System.out.println("Saving to file failed");                
        }    
if (cameraResolution != null && thePreviewHandler != null) {
  Message message = thePreviewHandler.obtainMessage(previewMessage, cameraResolution.x,
      cameraResolution.y, data);
  message.sendToTarget();
  previewHandler = null;

} else {
  Log.d(TAG, "Got preview callback, but no handler or resolution available");
}}

检查 width 和 height 变量是否正确设置为 Nexus 7 的相机宽度和高度 1280x960.我相信问题来自于尝试保存每一帧图像,因为尝试保存到文件"在 logcat 中出现的速度有点快;每秒几次.还可能值得注意的是,保存的损坏图像是方形的(ish).

Having checked the width and height variable are correctly set to the Nexus 7's camera width and height of 1280x960. I am confident the issue comes from attempting to save the image every frame, as "Attempting to save to file" appears somewhat rapidly within the logcat; several times a second. It may also be worth noting that the corrupt image saved is square(ish).

提前致谢.

推荐答案

1200 x 800?你确定这是你的预览尺寸吗?检查你的参数.可能是 1280 x 720

1200 x 800? are you sure this is your preview size? check your parameters. Probably it's 1280 x 720

相关文章