Mat*_*ijs 5 android opencv opencv3.1
我一直试图在拍摄照片时处理图像,即在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);
}
Run Code Online (Sandbox Code Playgroud)
有谁知道它冻结的原因以及如何解决它?
注意:我使用OpenCV4Android教程3作为基础.
更新1:我还尝试解析字节(没有任何成功),如下所示:
Mat mat = Imgcodecs.imdecode(
new MatOfByte(bytes),
Imgcodecs.CV_LOAD_IMAGE_UNCHANGED
);
Run Code Online (Sandbox Code Playgroud)
更新2:据说这应该工作,但它不适合我.
Mat mat = new Mat(1, bytes.length, CvType.CV_8UC3);
mat.put(0, 0, bytes);
Run Code Online (Sandbox Code Playgroud)
这种变体也没有:
Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC1);
mat.put(0, 0, bytes);
Run Code Online (Sandbox Code Playgroud)
更新3:这对我来说也不起作用:
Mat mat = new MatOfByte(bytes);
Run Code Online (Sandbox Code Playgroud)
我得到了一位同事的帮助。他通过执行以下操作成功解决了该问题:
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());
Run Code Online (Sandbox Code Playgroud)
希望这对所有同样为此苦苦挣扎的人有所帮助。