简体   繁体   中英

Opencv image cropping and displaying not working

Imageview showing the image from the uri perfectly fine. But i am trying to crop a portion from the image and display the cropped section using opencv.

so i converted my bitmap object into mat and tried to crop it but the resulted bitmap not showing the image

bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imgUri);
Utils.bitmapToMat(bitmap, unCropped);
Rect roi = new Rect(Utility.x, Utility.y, Utility.width, Utility.height);
Mat cropped = new Mat(unCropped, roi);
Utils.matToBitmap(cropped,bitmap);
imageView.setImageBitmap(bitmap);
imageView.setVisibility(View.VISIBLE);

Whats i am doing wrong in here??

you can use the manual method for tht like i did in my project: Its for round image result:

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
    int targetWidth = 50;
    int targetHeight = 50;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
            targetHeight,Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2,
            ((float) targetHeight - 1) / 2,
            (Math.min(((float) targetWidth),
                    ((float) targetHeight)) / 2),
            Path.Direction.CCW);

    canvas.clipPath(path);
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap,
            new Rect(0, 0, sourceBitmap.getWidth(),
                    sourceBitmap.getHeight()),
            new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}

so just do::

imageView.setImageBitmap(getRoundedShape(bitmap));

It will give you a round image. You can make change as shape your want..Hope this will help. Appreciate if help...

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