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

时间:2013-08-09 14:53:42

标签: java android save photo zxing

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

 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");
}}

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

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


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

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");
}}

检查宽度和高度变量是否正确设置为Nexus 7的相机宽度和高度1280x960。我相信这个问题来自于尝试每帧保存图像,因为“尝试保存到文件”在logcat中出现得有些迅速;几秒钟。值得注意的是,保存的损坏图像是方形(ish)。

提前致谢。

1 个答案:

答案 0 :(得分:0)

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