Android,OpenCv,捕获的照片尺寸与预览不匹配

问题描述 投票:1回答:2

我有一个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
2个回答
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为您的布局选择的预览尺寸匹配的图片尺寸,您有两个选择:要么寻找另一个预览尺寸,要么尝试一些图片大小,是“足够接近”。


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.