裁剪并保持图像android的大小 [英] Crop and keep the size of an image android

查看:179
本文介绍了裁剪并保持图像android的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常难以用语言解释的问题(无论如何它对我来说都是如此)。我需要能够拍摄图像(位图)并将图像裁剪到屏幕中心的特定尺寸,但保持图像的大小相同。希望下面的图片可以解释我的意思:

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:

因此整个图像被裁剪到中间的正方形,但没有在屏幕上拉伸并保持在中心,所以基本上删除图像的无意义部分,但保持像素的坐标相同。

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.

推荐答案

所以,假设你已经完成了你的脸部检测,并在你的图像中找到了一张脸。你的图像是320 x 240,面部是由位置100,40和宽度20 x 30的矩形绑定。现在你想用这些信息做什么?我会尽力帮忙,但你可能需要澄清我的任何不良假设。

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.

首先,你可以抓住脸并存储它使用类似 Bitmap.createBitmap()

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);

这应该在绘制循环之外完成,就像在onCreate或其他一些初始化步骤中一样。

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

听起来你有一些容器(ImageView?自定义视图,覆盖onDraw?),它容纳你的大图像。现在你想在那个容器中画出原始位置的脸?如果你有一个自定义视图,就像onDraw中的以下内容一样简单:

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);

如果您使用的是ImageView,我建议您转到自定义绘制的视图,因为听起来你需要一些细粒度的绘图控件。

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.

最后,如果你有一堆这些面孔,创建一个新的FaceObj POJO对象,只有一个位图,x和y坐标。在检测到面部时,将它们添加到ArrayList中,然后在onDraw中对其进行迭代以绘制所有面部:

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);

这篇关于裁剪并保持图像android的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆