简体   繁体   中英

using opencv in android

I have a code which captures/selects an image and sets it to an imageview. What I want is, store this image in a variable, pass it to a function and set an integer output in the textView. Now, if I manually use an image which is stored in drawable (converting into bitmap and then mat), I get the desired result. But, when I try to use the bitmap image which is being taken dynamically from the camera/gallery (after converting into mat), an exception arises. From what I understand, The captured/selected image is not being converted or stored in a variable as it shows bmp=null. The codes are: For capturing the image:

public void onCaptureImageResult(Intent data) {
       Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

        File destination = new File(Environment.getExternalStorageDirectory(),
                System.currentTimeMillis() + ".jpg");

        FileOutputStream fo;
        try {
            destination.createNewFile();
            fo = new FileOutputStream(destination);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
     thumbnail1=thumbnail;
        ivImage.setImageBitmap(thumbnail);
    }

For selecting the image:

public void onSelectFromGalleryResult(Intent data) {

     bm = null;
    if (data != null) {
        try {
            bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    ivImage.setImageBitmap(bm);
}

For processing the image and setting the output:

   public void countDents(Mat src) {
    int count = 0;
    Mat source;
    source = src;
    int cnt = 0, cnt2 = 0;
    Mat middle, destination1, destination2;
    List<MatOfPoint> contours1 = new ArrayList<>();
    List<MatOfPoint> contours2 = new ArrayList<>();
    middle = new Mat();
    destination1 = new Mat();
    destination2 = new Mat();
    /*Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.car);
    Utils.bitmapToMat(img, source);*/
    Imgproc.cvtColor(source, middle, Imgproc.COLOR_RGB2GRAY);
    Imgproc.equalizeHist(middle, middle);
   // ivImage.setImageBitmap(img);
    Imgproc.threshold(middle, destination1, 150, 255, Imgproc.THRESH_BINARY);
    Imgproc.threshold(middle, destination2, 150, 255, Imgproc.THRESH_BINARY_INV);
    Imgproc.findContours(destination1, contours1, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
    Imgproc.findContours(destination2, contours2, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
  //  Utils.matToBitmap(destination2, img);
    for (int i = 0; i < contours1.size(); i++) {
        if (Imgproc.contourArea(contours1.get(i)) > 5 && Imgproc.contourArea(contours1.get(i)) < 200) {
            cnt++;
        }
    }

    for (int i = 0; i < contours2.size(); i++) {
        if (Imgproc.contourArea(contours2.get(i)) > 5 && Imgproc.contourArea(contours2.get(i)) < 200) {
            cnt2++;


            count = cnt + cnt2;
            String c = Integer.toString(count);
            TVdents.setText(c);

        }
    }

onClick function for where function is being called:

    public void onClick(View v) {
            Utils.bitmapToMat(thumbnail1,tmp);
            Utils.bitmapToMat(bm,tmp);
            countDents(tmp);

        }

07-25 11:19:21.727 19363-19363/com.example.smartlayer.imageprocessing E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.smartlayer.imageprocessing, PID: 19363 java.lang.IllegalArgumentException: bmp == null at org.opencv.android.Utils.bitmapToMat(Utils.java:90) at org.opencv.android.Utils.bitmapToMat(Utils.java:102) at com.example.smartlayer.imageprocessing.MainActivity$2.onClick(MainActivity.java:80) at android.view.View.performClick(View.java:4785) at android.view.View$PerformClick.run(View.java:19884) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

If there is any discrepancy int the question, the code or way of asking, please let me know, I'll rectify.

Actually, the problem is that thumbnail1 is null ; if it was tmp , you would see java.lang.IllegalArgumentException: mat == null , according to the source code on GitHub.

So, what I think is the problem is that in onCaptureImageResult() after the assignment:

thumbnail1 = thumbnail;

Things get garbage collected and thumbnail1 is no longer valid. So, what you can do is take a copy of thumbnail by doing this instead :

thumbnail1 = thumbnail.copy(thumbnail.getConfig(), true);

(I'm not 100% sure this will work, though...)

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