简体   繁体   中英

OpenCV with Android Capture Images

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());
        }
    }

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM