4

I have a bitmap out of which I'm cutting out a multipi point polygon. I'm curious what the correct process is for taking the pixels within the arbitrary shape and copying them onto a new bitmap where the rest of the pixels are transparent. The objective is to allow the user to trace the shape and then remove everything outside the polygon.

I have the polygon part worked out (as a an array of points), but now am stumped as to how to transfer just the selected pixels to a new Bitmap.

TIA

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236

1 Answers1

9

Not sure how your code works, but here's an idea on how to do it:

  1. Calculate the bounding rectangle of the selected area (find min x, min y, max x and max y from your points).
  2. Crop your image to the bounding rectangle using any of the Bitmap or Canvas-methods.
  3. Create a Path from your points, all moved into your new bitmap (x-=minX, y-=minY);
  4. Set your Paths FillType to one that is inverse (fill the outside).
  5. On your new cropped canvas, draw the Path using a paint with the Xfermode as PorterDuff.CLEAR, which removes all color.
Jave
  • 31,598
  • 14
  • 77
  • 90
  • Dr.Dredel, did you manage to cut out a poligon out of a Bitmap? I'm trying to follow @Jave recomendation but it seems I am missing something here: http://stackoverflow.com/questions/11579645/android-crop-an-image-from-multipoints – Andy Res Jul 20 '12 at 13:19
  • @AndyRes, I wound up using a library that does it, however, I'm now finding that this lib sometimes (albeit infrequently) produces the dreaded OutOfMemory error. Looking at the instructions in this answer, I suspect they should work correctly... I actually came back to this answer to see about implementing it. I'll post my code if it works. – Yevgeny Simkin Sep 08 '12 at 23:56
  • I tried doing exactly that and it does cut everything outside the defined path but cut out area is still black and not transparent. Do you know how to make it transparent? – MikeL Jan 08 '13 at 14:11
  • @MichaelLiberman is the image format jpeg? jpeg images dont have transparency like gif or png's – Illegal Argument Sep 20 '15 at 12:29
  • @IllegalArgument, Actually I don't recall the project I was working on then, as almost three years past since then, but I am pretty sure it was a PNG as this is the type I normally work with in my Android projects and due to the fact that I am aware of the transparency for these formats. But I might be wrong. – MikeL Sep 20 '15 at 13:08