Android裁剪中心位图

Mau*_*ice 141 android crop bitmap

我有正方形或矩形的位图.我采取最短的一面,做这样的事情:

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);
Run Code Online (Sandbox Code Playgroud)

然后我使用它将它缩放到144 x 144位图:

Bitmap lastBitmap = null;
lastBitmap = Bitmap.createScaledBitmap(finalBitmap, 144, 144, true);
Run Code Online (Sandbox Code Playgroud)

问题是它裁剪原始位图的左上角,任何人都有代码来裁剪位图的中心?

Lum*_*mis 338

在此输入图像描述

这可以通过以下方法实现: Bitmap.createBitmap(source,x,y,width,height)

if (srcBmp.getWidth() >= srcBmp.getHeight()){

  dstBmp = Bitmap.createBitmap(
     srcBmp, 
     srcBmp.getWidth()/2 - srcBmp.getHeight()/2,
     0,
     srcBmp.getHeight(), 
     srcBmp.getHeight()
     );

}else{

  dstBmp = Bitmap.createBitmap(
     srcBmp,
     0, 
     srcBmp.getHeight()/2 - srcBmp.getWidth()/2,
     srcBmp.getWidth(),
     srcBmp.getWidth() 
     );
}
Run Code Online (Sandbox Code Playgroud)

  • 请参阅我关于使用内置ThumbnailUtils.extractThumbnail()方法的答案.为什么重新发明轮子??? http://stackoverflow.com/a/17733530/1103584 (10认同)

Dis*_*Dev 300

虽然上面的大多数答案提供了一种方法来实现这一点,但已经有一种内置的方法来实现这一点,它是一行代码(ThumbnailUtils.extractThumbnail())

int dimension = getSquareCropDimensionForBitmap(bitmap);
bitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension);

...

//I added this method because people keep asking how 
//to calculate the dimensions of the bitmap...see comments below
public int getSquareCropDimensionForBitmap(Bitmap bitmap)
{
    //use the smallest dimension of the image to crop to
    return Math.min(bitmap.getWidth(), bitmap.getHeight());
}
Run Code Online (Sandbox Code Playgroud)

如果您希望回收位图对象,可以传递使其成为的选项:

bitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
Run Code Online (Sandbox Code Playgroud)

来自:ThumbnailUtils文档

public static Bitmap extractThumbnail(Bitmap source,int width,int height)

在API级别8中添加创建所需大小的居中位图.

参数source原始位图源宽度目标宽度高度目标高度

在使用接受的答案时,我有时会出现内存错误,并且使用ThumbnailUtils为我解决了这些问题.此外,这更清洁,更可重复使用.

  • +1我认为你必须改进这个代码,而不是使用400px,传递最短的bmp的大小,以便为上面的原始帖子提供替代.但是,谢谢你引起我们的注意,它似乎是一个非常有用的功能.可惜我以前没见过...... (6认同)
  • @DiscDev - 这必须是整个网站上最有用的答案之一.认真.Android问题很奇怪 - 在找到简单明了的答案之前,你经常可以搜索两个小时.不知道怎么感谢你.赏金途中! (4认同)
  • 您甚至可以使用一个不同的功能,以回收旧的位图:= ThumbnailUtils.extractThumbnail(位图,宽度,高度,ThumbnailUtils.OPTIONS_RECYCLE_INPUT) (2认同)

Ovi*_*tcu 12

你有没有考虑过这样做layout.xml?你可以设置你ImageViewScaleTypeandroid:scaleType="centerCrop",并在设定的图像的尺寸ImageViewlayout.xml.


Hit*_*tel 9

您可以使用以下代码来解决您的问题.

Matrix matrix = new Matrix();
matrix.postScale(0.5f, 0.5f);
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100, matrix, true);
Run Code Online (Sandbox Code Playgroud)

上面的方法在裁剪之前做postScalling图像,这样你可以获得裁剪图像的最佳效果而不会出现OOM错误.

有关更多详细信息,请参阅此博客


msc*_*ock 9

这是一个更完整的片段,用于裁剪任意尺寸的[位图]的中心,并将结果缩放到所需的[IMAGE_SIZE].因此,您将始终获得具有固定大小的图像中心的 [croppedBitmap]缩放平方.缩略图等的理想选择.

它是其他解决方案更完整的组合.

final int IMAGE_SIZE = 255;
boolean landscape = bitmap.getWidth() > bitmap.getHeight();

float scale_factor;
if (landscape) scale_factor = (float)IMAGE_SIZE / bitmap.getHeight();
else scale_factor = (float)IMAGE_SIZE / bitmap.getWidth();
Matrix matrix = new Matrix();
matrix.postScale(scale_factor, scale_factor);

Bitmap croppedBitmap;
if (landscape){
    int start = (tempBitmap.getWidth() - tempBitmap.getHeight()) / 2;
    croppedBitmap = Bitmap.createBitmap(tempBitmap, start, 0, tempBitmap.getHeight(), tempBitmap.getHeight(), matrix, true);
} else {
    int start = (tempBitmap.getHeight() - tempBitmap.getWidth()) / 2;
    croppedBitmap = Bitmap.createBitmap(tempBitmap, 0, start, tempBitmap.getWidth(), tempBitmap.getWidth(), matrix, true);
}
Run Code Online (Sandbox Code Playgroud)

  • 什么是tempBitmap? (3认同)

Kir*_*kov 7

到目前为止可能是最简单的解决方案:

public static Bitmap cropCenter(Bitmap bmp) {
    int dimension = Math.min(bmp.getWidth(), bmp.getHeight());
    return ThumbnailUtils.extractThumbnail(bmp, dimension, dimension);
}
Run Code Online (Sandbox Code Playgroud)

进口:

import android.media.ThumbnailUtils;
import java.lang.Math;
import android.graphics.Bitmap;
Run Code Online (Sandbox Code Playgroud)