Crop and save Android image size

This is a very difficult question to explain in words (well, this is for me anyway). I need to make an image (bitmap) and crop the image to a certain size in the center of the screen, but keeping the image size the same. Hope the picture below can explain what I mean:

enter image description here

Thus, the image as a whole is cropped to a square in the middle, but does not stretch across the screen and remains in the center, so basically removing the meaningless part of the image, but preserving the pixel coordinates is the same.

+1
source share
3 answers

So, let's say you made your face detection and found one face in your image. Your image is 320 x 240, and your face is connected by a rectangle with a place of 100.40 and a width of 20 x 30. Now, what would you like to do with this information? I will do my best to help, but you will probably have to clarify any bad assumptions on my part.

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

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

This should be done outside of the drawing loop, for example in onCreate or some other initialization step.

It looks like you have a container (ImageView? Custom View with overridden onDraw?) That hosts your large image. And now you just want to draw a face in this container in its original position? If you have a custom view, it is as simple as the following in your onDraw:

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

If you use ImageView instead, I would suggest switching to a custom view instead, as it sounds like you need a fine-grained drawing control.

Finally, if you have a bunch of these faces, create a new POQO FaceObj that has only raster, x, and y coordinates. When you detect faces, add them to an ArrayList, and then iterate over it inside 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, fx, fy, facePaint); 
+3
source

If I understand that you really don’t 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 image with black or make it transparent.

Thus, the coordinates of your β€œcropped” image will remain unchanged on the screen.

0
source

If all you are interested in is the center of the image, then one very simple way to do this is to simply add the android attribute: scaleType = "center" to your ImageView and set the ImageView to the cropping size you want. If you want it to be positioned, it was a different story.

Using Canvas.drawBitmap() , you can work to copy part of the image to another bitmap and discard another. With this particular version of the method, you can send to the array of colors that you get with getPixels() and set the offset, as well as the width and height that you want to copy. The stride parameter is important because it needs to be set to the width of the original image, even if your final image will be smaller because it will draw pixels from the original image.

0
source

Source: https://habr.com/ru/post/1339992/


All Articles