Android 位图裁剪中心
我有正方形或矩形的位图。我取最短的一边并执行如下操作:
int value = 0;
if (bitmap.getHeight() <= bitmap.getWidth()) {
value = bitmap.getHeight();
} else {
value = bitmap.getWidth();
}
Bitmap finalBitmap = null;
finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, value, value);
然后我使用以下方法将其缩放为 144 x 144 位图:
Bitmap lastBitmap = null;
lastBitmap = Bitmap.createScaledBitmap(finalBitmap, 144, 144, true);
问题是它裁剪了原始位图的左上角,有人有裁剪位图中心的代码吗?
I have bitmaps which are squares or rectangles. I take the shortest side and do something like this:
int value = 0;
if (bitmap.getHeight() <= bitmap.getWidth()) {
value = bitmap.getHeight();
} else {
value = bitmap.getWidth();
}
Bitmap finalBitmap = null;
finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, value, value);
Then I scale it to a 144 x 144 Bitmap using this:
Bitmap lastBitmap = null;
lastBitmap = Bitmap.createScaledBitmap(finalBitmap, 144, 144, true);
Problem is that it crops the top left corner of the original bitmap, Anyone has the code to crop the center of the bitmap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这可以通过以下方式实现: Bitmap.createBitmap(source, x, y, width, height )
This can be achieved with: Bitmap.createBitmap(source, x, y, width, height)
虽然上面的大多数答案都提供了一种方法来执行此操作,但已经有一种内置方法可以完成此操作,并且只需一行代码 (
ThumbnailUtils.extractThumbnail()
)如果您想要位图对象要被回收,您可以传递使其如此的选项:
From: ThumbnailUtils 文档
在使用接受的答案时,有时会出现内存错误,而使用 ThumbnailUtils 为我解决了这些问题。另外,这更干净且更可重复使用。
While most of the above answers provide a way to do this, there is already a built-in way to accomplish this and it's 1 line of code (
ThumbnailUtils.extractThumbnail()
)If you want the bitmap object to be recycled, you can pass options that make it so:
From: ThumbnailUtils Documentation
I was getting out of memory errors sometimes when using the accepted answer, and using ThumbnailUtils resolved those issues for me. Plus, this is much cleaner and more reusable.
您是否考虑过从
layout.xml
执行此操作?您可以为您的ImageView
设置 ScaleType 到android:scaleType="centerCrop"
并在layout.xml
内的ImageView
中设置图像的尺寸。Have you considered doing this from the
layout.xml
? You could set for yourImageView
the ScaleType toandroid:scaleType="centerCrop"
and set the dimensions of the image in theImageView
inside thelayout.xml
.到目前为止可能是最简单的解决方案:
导入:
Probably the easiest solution so far:
imports:
您可以使用以下代码来解决您的问题。
上述方法在裁剪之前对图像进行 postScalling,因此您可以获得裁剪图像的最佳结果,而不会出现 OOM 错误。
有关更多详细信息,您可以参考此博客
You can used following code that can solve your problem.
Above method do postScalling of image before cropping, so you can get best result with cropped image without getting OOM error.
For more detail you can refer this blog
这里有一个更完整的代码片段,它裁剪出任意尺寸的[位图]的中心,并将结果缩放到您想要的[IMAGE_SIZE]。因此,您将始终获得图像中心的具有固定大小的[croppedBitmap]缩放正方形。非常适合缩略图等。
它是其他解决方案的更完整的组合。
Here a more complete snippet that crops out the center of an [bitmap] of arbitrary dimensions and scales the result to your desired [IMAGE_SIZE]. So you will always get a [croppedBitmap] scaled square of the image center with a fixed size. ideal for thumbnailing and such.
Its a more complete combination of the other solutions.
纠正@willsteel 解决方案:
To correct @willsteel solution: