OpenCV with Android Capture Images

2019-03-10 11:41发布

问题:

What is the best way to capture images in android and process them with opencv ? should i use the surface view As here or i should use the opencv camera as in opencv samples ? and how can i load the captured image and convert it to Mat? Thanks in advance

 private PictureCallback mPicture = new PictureCallback() {

        @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null) {
            Log.d("E",
                    "Error creating media file, check storage permissions ");
            return;
        }

        try {

            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
            //processImage();
        } catch (FileNotFoundException e) {
            Log.d("E", "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d("E", "Error accessing file: " + e.getMessage());
        }
    }

回答1:

In the OpenCV samples there is a CameraBridgeViewBase that helps in capturing, processing and showing the results of the camera preview (continuous stream of lower resolution images).

If you want to process a single capture with more resolution, you should use something similar to onPictureTaken(). Then you can read the image using Highgui.imread() and process it.

You could also avoid writing and reading to a file by creating a Bitmap from the data array with BitmapFactory.decodeByteArray() and then converting the Bitmap to a Mat using Utils.bitmapToMat()