Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ANDROID:将PreviewCallback图像从字节数组保存为.jpg会导致文件损坏_Java_Android_Save_Photo_Zxing - Fatal编程技术网

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

Java ANDROID:将PreviewCallback图像从字节数组保存为.jpg会导致文件损坏,java,android,save,photo,zxing,Java,Android,Save,Photo,Zxing,我一直在扩充QR扫描库Zxing,以便扫描后立即保存照片。有人建议我在PreviewCallback.java中的onPreviewFrame方法中这样做,如下所示: public void onPreviewFrame(byte[] data, Camera camera) { Point cameraResolution = configManager.getCameraResolution(); Handler thePreviewHandler = previewHandler; Y

我一直在扩充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");
}}
运行此代码的结果是set file目录中的映像损坏。我相信这是因为代码每帧都在运行。如果允许保存完整图像,是否有方法将此限制为每秒左右,或者是否有方法使图像仅在完成扫描后保存

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


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

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)


提前谢谢。

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

是的,是摄像头,检查您的摄像头预览尺寸我如何找到任何设备的摄像头高度和宽度?我已经通过Camera.GetParameters看到了这一点,但我很难找到合适的示例代码。更新后的示例代码应能适应任何相机大小,图像仍然会损坏。您应该使用getPreviewSize()而不是getPictureSize(),我想它们是不同的。尝试一下谢谢你,现在可以成功了,但是我没有办法在扫描时只保存一张照片。我将提出一个新问题来帮助解决这个问题。再次感谢。