裁剪并保持图像 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您已完成人脸检测,并在图像中找到了一张人脸。您的图像尺寸为 320 x 240,脸部由位置为 100,40、宽度为 20 x 30 的矩形包围。现在您想如何处理该信息?我会尽力提供帮助,但您可能需要澄清我的任何错误假设。
首先,您可以抓取脸部并将其存储到新的位图中,例如 Bitmap.createBitmap():
这应该在绘制循环之外完成,就像在 onCreate 或其他一些初始化步骤中一样。
听起来你有一些容器(ImageView?带有重写 onDraw 的自定义视图?)来容纳你的大图像。现在您只想在该容器中的原始位置绘制脸部?如果您有一个自定义视图,那么就像 onDraw 中的以下内容一样简单:
如果您使用的是 ImageView,我建议改为使用自定义绘制视图,因为听起来您需要一些精细的-粒度绘图控制。
最后,如果您有一堆这样的面孔,请创建一个新的 FaceObj POJO 对象,它只有一个位图、x 和 y 坐标。当您检测到面部时,将它们添加到 ArrayList,然后在 onDraw 中迭代此以绘制所有面部:
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():
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:
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:
如果我理解,您并不是真的想裁剪图像,而是“隐藏”正方形周围的任何像素。
有很多方法可以做到这一点,具体取决于您想要做什么。例如,您可以将图片中不感兴趣的部分填充为黑色或使其透明。
这样,“裁剪”图片的坐标将在屏幕上保持不变。
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.
如果您感兴趣的是图像的中心,那么一种非常简单的方法就是将 android:scaleType="center" 属性添加到您的 ImageView 中,并将 ImageView 设置为您想要的裁剪尺寸。不过,如果您希望它可定位,那就是另一回事了。
使用
Canvas.drawBitmap()
您也许可以将图像的一部分复制到一个不同的位图,并丢弃另一个。使用此特定版本的方法,您可以发送通过 getPixels() 获得的颜色数组,并设置偏移量以及要复制的宽度和高度。不过,stride 参数很重要,因为它需要设置为原始图像的宽度,即使最终图像会更小,因为它从原始图像中提取像素。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 withgetPixels()
, and set an offset, and the width and height that you want to copy. Thestride
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.