5

我一直在尝试在拍摄照片后立即处理图像,即在onPictureTaken()回调中。据我了解,我应该将字节数组转换为 OpenCV 矩阵,但是当我尝试这样做时,整个应用程序都会冻结。基本上我所做的就是:

@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
    Log.w(TAG, "picture taken!");

    if (bytes != null) {
        Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        Mat matImage = new Mat();

        // This is where my app freezes.
        Utils.bitmapToMat(image, matImage);

        Log.w(TAG, matImage.dump());
    }

    mCamera.startPreview();
    mCamera.setPreviewCallback(this);
}

有谁知道它为什么会冻结以及如何解决它?

注意:我使用 OpenCV4Android 教程 3 作为基础。

更新1:我还尝试解析字节(没有任何成功),如下所示:

Mat mat = Imgcodecs.imdecode(
    new MatOfByte(bytes), 
    Imgcodecs.CV_LOAD_IMAGE_UNCHANGED
);

更新 2:据说这应该有效,但它不适合我。

Mat mat = new Mat(1, bytes.length, CvType.CV_8UC3);
mat.put(0, 0, bytes);

这个变种也没有:

Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC1);
mat.put(0, 0, bytes);

更新 3:这对我也不起作用:

Mat mat = new MatOfByte(bytes);
4

1 回答 1

1

我得到了一位同事的帮助。他通过执行以下操作设法解决了这个问题:

BitmapFactory.Options opts = new BitmapFactory.Options(); // This was missing.
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);

Mat mat = new Mat();
Utils.bitmapToMat(bitmap, mat);

// Note: when the matrix is to large mat.dump() might also freeze your app.
Log.w(TAG, mat.size()); 

希望这会帮助所有也在为此苦苦挣扎的人。

于 2016-05-02T10:00:16.083 回答