【问题标题】:Android, OpenCv, captured photo dimensions do not match previewAndroid、OpenCv、拍摄的照片尺寸与预览不匹配
【发布时间】:2017-12-17 05:54:10
【问题描述】:

我有一个 JavaCameraView 子类,它允许用户拍摄和保存照片。问题是我通过 onCameraFrame() 收到的预览帧与捕获和保存的照片的尺寸不同,导致保存的图像相对于帧出现裁剪。输入帧为 1080 * 1440 (3:4),捕获为 2988 * 5312 (9:16)。

我的代码如下:

public void takePicture() {
    Log.i(CaptureActivity.TAG, "MyCameraView, takePicture()");
    // Postview and jpeg are sent in the same buffers if the queue is not empty when performing a capture.
    // Clear up buffers to avoid mCamera.takePicture to be stuck because of a memory issue
    mCamera.setPreviewCallback(null);

    // PictureTakenListener is implemented by the current class
    mCamera.takePicture(null, null, this);
}

@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
    // The camera preview was automatically stopped. Start it again.
    mCamera.startPreview();
    mCamera.setPreviewCallback(this);

    Mat mat = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
    Log.d(CaptureActivity.TAG, mat.toString());

    //an interface method to be implemented by CaptureActivity
    if (mPictureTakenListener != null)
        mPictureTakenListener.receivePicture(mat);
}

在另一个班级,我收到图片并保存...

@Override
public void receivePicture(Mat mat) {
    File path = new File(Environment.getExternalStorageDirectory() + "/myCamera/");
    path.mkdirs();
    File file = new File(path, System.currentTimeMillis() + "recieved" + ".png");
    String filename = file.toString();
    Log.d(CaptureActivity.TAG, "was imwrite a success? "
        + Imgcodecs.imwrite(filename, mat));    
}

这个类也实现了CameraBridgeViewBase.CvCameraViewListener2...

public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        Log.d(TAG, "frame info " + frame.toString());
        return frame;
    }

【问题讨论】:

    标签: android opencv android-camera


    【解决方案1】:

    你是对的。不建议使用相同的纵横比进行预览和捕捉。因此,在您的子类中,您应该像这样覆盖 initializeCamera() 方法:

    @Override
    protected void AllocateCache() {
        super.AllocateCache();
        setPictureSize();
    }
    
    protected boolean setPictureSize() {
        try {
            Camera.Parameters params = mCamera.getParameters();
            Log.d(TAG, "getSupportedPictureSizes()");
            List<Camera.Size> sizes = params.getSupportedPictureSizes();
            if (sizes == null) {
                Log.w(TAG, "getSupportedPictureSizes() = null, cannot set a custom size");
                return false;
            }
    
            int maxSize = 0;
            // choose the largest size that matches the preview AR
            for (android.hardware.Camera.Size size : sizes) {
                if (size.height * mFrameWidth != size.width * mFrameHeight) {
                    continue; // the picture size doesn't match
                }
                if (maxSize > size.width * size.height) {
                    continue; // don't need this size
                }
                params.setPictureSize(size.width, size.height);
                maxSize = size.width * size.height;
            }
            if (maxSize == 0) {
                Log.w(TAG, "getSupportedPictureSizes() has no matches for " + mFrameWidth + 'x' + mFrameHeight);
                return false;
            }
            Log.d(TAG, "try Picture size " + params.getPictureSize().width + 'x' + params.getPictureSize().height);
            mCamera.setParameters(params);
        } catch (Exception e) {
            Log.e(TAG, "setPictureSize for " + mFrameWidth + 'x' + mFrameHeight, e);
            return false;
        }
        return true;
    }
    

    }

    如您所见,我实际上重写了方法AllocateCache(),因为OpenCV 没有在打开相机设备和开始预览之间提供更好的挂钩。在预览期间更改相机参数(甚至图片大小)可能会在某些设备上失败。

    请注意,上面的代码甚至没有尝试设置不完全适合所选预览的图片大小。对于 99.9% 的情况,这是可以的,但是如果一个奇怪的相机没有与 OpenCV 为您的布局选择的预览尺寸匹配的图片尺寸,您有两个选择:或者寻找另一个预览尺寸,或者尝试一些图片尺寸是“足够接近”。

    【讨论】:

      【解决方案2】:

      您应该使用setPreviewSizesetPictureSize 来配置您从预览和拍摄照片中获得的内容。请注意,您不一定会为它们找到相同的尺寸(通常 takePicture 可用尺寸可能高于预览尺寸),因此请查看 getSupportedPreviewSizesgetSupportedPictureSizes 以找到适合您任务的尺寸大多数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-23
        • 2014-08-15
        • 1970-01-01
        • 2015-01-13
        • 2015-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多