Crop image by polygon area
Asked Answered
B

2

8

I want to crop image by polygon area, but couldn`t find any library, which can make it. OpenCV is too big for this small thing. JJIL [enter link description here] crop just rectangle area. Maybe you have any ideas how i can achieve it? Thanks for help!

enter image description here enter image description here

FOR Nidhi: Try something like this, if doesnot work - create another canvas for path, and than get Bitmap from it (for mask), and apply this mask bitmap to your initial canvas instead drawPath.

Bitmap obmp = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
Bitmap resultImg = Bitmap.createBitmap(obmp.getWidth(), obmp.getHeight(), Bitmap.Config.ARGB_8888);
Bitmap maskImg = Bitmap.createBitmap(obmp.getWidth(), obmp.getHeight(), Bitmap.Config.ARGB_8888);

Canvas mCanvas = new Canvas(resultImg);
Canvas maskCanvas = new Canvas(maskImg);

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);;
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

Path path = new Path();
path.moveTo(view.mx,view.my);
path.lineTo(view.x1,view.y1);
path.lineTo(view.x2,view.y2 );
path.lineTo(view.x3,view.y3);
path.lineTo(view.x4,view.y4);
path.close();

maskCanvas.drawPath(path, paint);   
mCanvas.drawBitmap(obmp, 0, 0, null);
mCanvas.drawBitmap(maskImg, 0, 0, paint);
Birddog answered 12/4, 2013 at 10:30 Comment(16)
What's your problems? It's quite simple: first - make mask filling by ones inside your area of interest, second - substitute all image pixels where mask==0 by some background value.Isidoro
Hello ADK, actually I am newbie in android.And I am also finding the solution on how to crop image using a polygon. Can you give me some hint on how did you achieve this. Some hint will also help me lot.Baton
Hi! https://mcmap.net/q/241296/-masking-crop-image-in-frame not enought? Could you provide me more details? Maybe you show me your code?Birddog
Hello ADK, I went through this link. And tried the solution given. My problem is, my cropped bitmap crops from the top left of the original bitmap. It does give me the shape. but the content is not exact.Baton
code, Bitmap obmp=BitmapFactory.decodeResource(getResources(), R.drawable.image1); Bitmap resultImg=Bitmap.createBitmap(320,480, bitmap1.getConfig()); Canvas canvas = new Canvas(resultImg); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setAntiAlias(true); Path path=new Path(); path.moveTo(view.mx,view.my); path.lineTo(view.x1,view.y1); path.lineTo(view.x2,view.y2 ); path.lineTo(view.x3,view.y3); path.lineTo(view.x4,view.y4); canvas.drawPath(path, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(obmp, 0, 0, paint);Baton
hello ADK, thankyou for help. Sorry I am bothering you again but, in edit you have specified mCanvas and maskCanvas, I am not getting what to put as maskCanvas and mCanvas.Baton
Ооо! Sorry, my fault) Corrected, see again.Birddog
Okay. I tried by changing canvas. It is not cropping. :(Baton
as suggested by you i tried to create another canvas for path. like this. Bitmap obmp = BitmapFactory.decodeResource(getResources(), R.drawable.image1); Bitmap mask = Bitmap.createBitmap(obmp.getWidth(),obmp.getHeight(), Bitmap.Config.ARGB_8888); Canvas maskCanvas = new Canvas(mask); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setAntiAlias(true); paint.setStyle(Paint.Style.FILL); paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); Path path=new Path(); whole path maskCanvas.drawPath(path,paint); Now what should i do.Baton
mCanvas.drawBitmap(obmp, 0, 0, maskCanvas); is causing problem there is no method with 2 bitmapsBaton
Hello ADK, thank you for great help. Finally done with cropping with few edits in above code.Thanks a lot :)Baton
hey ADK, how did you cut the outcropped image from final masked image,as it shows white background after masking.Baton
Hi Nidhi, you need Canvas with alpha channelBirddog
Hey @ADK, I tried to change the alpha channel but as the masked image size was the same as the original one, it showed me the transparent background in cropped image.Baton
I found a easy solution inspired from [here][1] [1]: https://mcmap.net/q/241296/-masking-crop-image-in-frameBruns
@Bruns I took my solution from that topic too, its the same) Check "Good example" link in the answer belowBirddog
B
2

Thanks for Eddy_Em, i have achieved this by using PorterDuffXfermode. Good example

Birddog answered 12/4, 2013 at 14:6 Comment(0)
O
0

This a working Kotlin example, that clips and image to a polygon share depending on the path

  private fun createBitmap() {
    var bitmap = BitmapFactory.decodeResource(resources, R.drawable.gr)
    val mutableBitmap: Bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)

    val bitmap2 = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888)
    val polyCanvas = Canvas(bitmap2)
    val canvas = Canvas(mutableBitmap)
   
    var paint = Paint()
    paint.strokeWidth = 9f
   
    val path = Path()
    path.moveTo(150f, 0f)
    path.lineTo(230f, 120f)
    path.lineTo(290f, 160f)
    path.lineTo(150f, 170f)
    path.lineTo(70f, 200f)
    path.lineTo(150f, 0f)
    polyCanvas.drawPath(path, paint)
    paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
    polyCanvas.drawBitmap(mutableBitmap, 0f, 0f, paint)

    imageView.setImageBitmap(bitmap2)

  }
Omer answered 13/7, 2020 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.