How to get the Mat object from the Byte[] in openCV android?

后端 未结 5 2205
梦谈多话
梦谈多话 2020-12-29 10:46

I am working with OpenCV library in Android. I have a class which implements PictureCallBack.

The override method onPictureTaken() is as g

5条回答
  •  半阙折子戏
    2020-12-29 10:58

    opencv has a neat way to writing Mats to files - it can generate different formats based on the file extension provided. Here is a minimal example:

        public void writeImageToFile(Mat image, String filename) {
    
        File root = Environment.getExternalStorageDirectory();
        File file = new File(root, filename);
    
        Highgui.imwrite(file.getAbsolutePath(), image);
    
        if (DEBUG)
            Log.d(TAG,
                    "writing: " + file.getAbsolutePath() + " (" + image.width()
                            + ", " + image.height() + ")");
    }
    

    if red and blue is swapped then it sounds like your byte data from onPictureTaken() is in BGRA format. You can swap it to RGBA using:

    Imgproc.cvtColor(bgrImg, rgbImg, Imgproc.COLOR_BGR2RGB);
    

    the format is actually device specific - on one device I had it comes through as YUV.

提交回复
热议问题