1

This is a really hard question to explain in words (well it is for me anyway). I need to be able to take an image (bitmap) and crop the image down to a certain size in the centre of the screen but keeping the size of the image the same. Hopefully the picture below can explain what I mean:

enter image description here

So the image as a whole is cropped down to the square in the middle but is not stretched across the screen and remains in the centre, so basically removing the pointless part of the image but keeping to co-ordinates of the pixels the same.

Community
  • 1
  • 1
SamRowley
  • 3,435
  • 7
  • 48
  • 77
  • I think what you're trying to describe is called the Region of Interest (ROI). That may help you Google for some answers. – mpenkov Jan 28 '11 at 14:30

3 Answers3

3

So let's say you have done your face detection, and have found one face in your image. Your image is 320 x 240, and the face is bound by the rectangle with location 100,40 and width 20 x 30. Now what would you like to do with that information? I'll do my best to help, but you'll probably need to clear up any poor assumptions on my part.

First, you can grab the face and store it into a new bitmap with something like Bitmap.createBitmap():

Bitmap face = Bitmap.createBitmap(largeSource, 100, 40, 20, 30);

This should be done outside of the draw loop, like in onCreate or some other initialization step.

It sounds like you've got some container (ImageView? Custom View with overridden onDraw?) which is housing your large image. And now you want to just draw the face in that container, at its original position? If you've got a custom view, that's as simple as the following in your onDraw:

canvas.drawBitmap(face, 100, 40, facePaint);

If you're using an ImageView instead, I'd suggest going to a custom-drawn view instead, since it sounds like you need some fine-grained drawing control.

Finally, if you've got a bunch of these faces, create a new FaceObj POJO object, which just has a bitmap, x, and y coordinate. As you detect faces, add them to an ArrayList, and then iterate over this in in your onDraw to draw all your faces:

faces.add(new FaceObj(Bitmap.createBitmap(largeSource, 100, 40, 20, 30), 100, 40);

...

foreach(FaceObj f : faces)
    canvas.drawBitmap(f.bitmap, f.x, f.y, facePaint);
Josh
  • 10,618
  • 2
  • 32
  • 36
0

If I understand you don't really want to crop your image but "hide" any pixels around the square.

There are many ways to do this depending on what you are trying to do. For example you can fill the uninteresting part of the picture with black or make it transparent.

This way the coordinates of your "cropped" picture will remain the same on the screen.

Dalmas
  • 26,409
  • 9
  • 67
  • 80
  • It may also be easier to create a new black image the same size as the original, and copy the region of interest to the new image at the same x/y/width/height as the original. – Jess Jan 28 '11 at 14:44
  • The problem is that I need to not just hide the pixels but get rid of them. I need to keep the image in bitmap format but trying to reduce the size of the image as low as possible so I can detect faces. By changing the pixels to black it still stores that pixel therefore no loss in file size. – SamRowley Jan 28 '11 at 14:55
  • Ok so maybe this is what you want to do : first store the top left coordinates of the square, then crop it. Next clear your drawing area and use canvas.drawBitmap(bitmap, left, top, paint) where left and top are the coordinates you stored. – Dalmas Jan 28 '11 at 15:14
0

If all you are interested in is the center of the image, then one really easy way to do this is to just add the android:scaleType="center" attribute to your ImageView, and set the ImageView to the crop size you want. If you're wanting it to be positionable, though, that's a different story.

Using Canvas.drawBitmap() you might be able to work to copy a part of the image to a different bitmap, and discard the other. With this particular version of the method, you can send in the array of colors you get with getPixels(), and set an offset, and the width and height that you want to copy. The stride parameter is important though, as it needs to be set to the width of the original image, even if your final image will be smaller, as it's pulling pixels from the original image.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274