0

I am implementing the freehand cropping on the image in android. I am able to draw the arbitrary shape on the image using touch and collect all the points on the path in the array list. But I am not able to extract the part of image inside the arbitrary shape.

I searched quite a lot but was not able to find the appropriate answer. Does any body have a working example of this.

EDIT: I am able to create a arbitrary shape using the below code on the bitmap.

@Override
protected void onDraw(Canvas canvas) {


    canvas.drawBitmap(scaledBitmap, 0, 0,null);
    path.reset();


    boolean firstTouchPoint = true;

    for (int i = 0; i < lastPath.size(); i += 2) {
        Point point = lastPath.get(i);

        if (firstTouchPoint) {
            firstTouchPoint = false;
            path.moveTo(point.fXPosition, point.fYPosition);
        } else if (i < lastPath.size() - 1) {
            Point next = lastPath.get(i + 1);
            path.quadTo(point.fXPosition, point.fYPosition, next.fXPosition, next.fYPosition);
        } else {
            path.lineTo(point.fXPosition, point.fYPosition);
        }
    }

    canvas.drawPath(path, paint); 


}

but I am not able to extract the bitmap region inside this path.

Smern
  • 18,746
  • 21
  • 72
  • 90
user80189
  • 1
  • 2
  • Can you post your code? Is it safe to assume you have a `Path` object for the custom shape that is drawn? – Amulya Khare Oct 17 '13 at 05:35
  • I have edit my post. The above code shows I have the path for the custom shape. Now I want to extract the shape inside the path. – user80189 Oct 17 '13 at 17:20
  • Please update your post with the code you have tried using to extract the region inside the arbitrary shape, and explain what goes wrong with that code. – Esoteric Screen Name Oct 17 '13 at 17:46
  • I was trying to extract the region inside the path using this post: http://stackoverflow.com/questions/8993292/cutting-a-multipoint-ploygon-out-of-bitmap-and-placing-it-on-transparency – user80189 Oct 17 '13 at 17:50
  • But I am not getting where to put this code so that after I have created the path the cropped image will be displayed on my screen – user80189 Oct 17 '13 at 17:51
  • I tried looking for the code but I didn't find any that will extract the region inside the path – user80189 Oct 17 '13 at 17:51

1 Answers1

0

You should try this:

//the image should support transparency.
Bitmap scaledBitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
// fill the area around the path with alpha
Canvas c = new Canvas(scaledBitmap);
c.clipPath(path, Region.Op.DIFFERENCE);
c.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
  • Thanks for responding. But c.clipPath will not work on hardware as it needs hardware acceleration to be turned off. Can you also tell me where should I write this part of code to be rendered after I draw my path on the screen. – user80189 Oct 18 '13 at 06:42
  • Put this code inside maybe click of `Crop Image` button. So after the user clicks on the button, run this code. Then update your imageview to show the cropped image. As you mentioned about hardware acceleration, i think you should take a look at this and consider if you want to keep it on or off. http://stackoverflow.com/questions/8895677/work-around-canvas-clippath-that-is-not-supported-in-android-any-more – Amulya Khare Oct 18 '13 at 07:06