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);
ADK
  • 513
  • 3
  • 8
  • 22
  • 1
    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. – Eddy_Em Apr 12 '13 at 10:36
  • 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. – Dory Apr 24 '13 at 10:06
  • Hi! http://stackoverflow.com/questions/12614542/maskingcrop-image-in-frame not enought? Could you provide me more details? Maybe you show me your code? – ADK Apr 24 '13 at 10:38
  • 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. – Dory Apr 25 '13 at 12:55
  • 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);` – Dory Apr 25 '13 at 13:21
  • 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. – Dory Apr 26 '13 at 05:41
  • Ооо! Sorry, my fault) Corrected, see again. – ADK Apr 26 '13 at 05:51
  • Okay. I tried by changing canvas. It is not cropping. :( – Dory Apr 26 '13 at 06:02
  • 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. – Dory Apr 26 '13 at 06:40
  • `mCanvas.drawBitmap(obmp, 0, 0, maskCanvas);` is causing problem there is no method with 2 bitmaps – Dory Apr 26 '13 at 10:35
  • 1
    Hello ADK, thank you for great help. Finally done with cropping with few edits in above code.Thanks a lot :) – Dory Apr 26 '13 at 13:25
  • hey ADK, how did you cut the outcropped image from final masked image,as it shows white background after masking. – Dory Aug 29 '13 at 07:24
  • Hi Nidhi, you need Canvas with alpha channel – ADK Sep 12 '13 at 13:26
  • 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. – Dory Sep 27 '13 at 06:01
  • I found a easy solution inspired from [here][1] [1]: http://stackoverflow.com/questions/12614542/maskingcrop-image-in-frame – alkber May 21 '14 at 17:45
  • @alkber I took my solution from that topic too, its the same) Check "Good example" link in the answer below – ADK May 22 '14 at 07:05

2 Answers2

2

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

Community
  • 1
  • 1
ADK
  • 513
  • 3
  • 8
  • 22
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)

  }
Amos Korir
  • 123
  • 6