Android裁剪中心位图

时间:2011-08-02 07:29:37

标签: android bitmap crop

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

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

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

8 个答案:

答案 0 :(得分:330)

enter image description here

这可以通过以下方式实现: Bitmap.createBitmap(来源,x,y,宽度,高度)

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

答案 1 :(得分:293)

虽然上面的大部分答案都提供了一种方法,但已经有了一种内置的方法来实现这一点,它是一行代码(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());
}

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

bitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

来自:ThumbnailUtils Documentation

  

public static Bitmap extractThumbnail(Bitmap source,int width,int   高度)

     

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

     

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

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

答案 2 :(得分:12)

您是否考虑过layout.xml这样做?您可以为ImageView设置ScaleTypeandroid:scaleType="centerCrop",并在ImageViewlayout.xml内设置图片的尺寸。

答案 3 :(得分:9)

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

Matrix matrix = new Matrix();
matrix.postScale(0.5f, 0.5f);
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100, matrix, true);

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

有关详细信息,请参阅this blog

答案 4 :(得分: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);
}

答案 5 :(得分:5)

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

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

进口:

import android.media.ThumbnailUtils;
import java.lang.Math;
import android.graphics.Bitmap;

答案 6 :(得分:1)

map

答案 7 :(得分:1)

public Bitmap getResizedBitmap(Bitmap bm) {
    int width = bm.getWidth();
    int height = bm.getHeight();

    int narrowSize = Math.min(width, height);
    int differ = (int)Math.abs((bm.getHeight() - bm.getWidth())/2.0f);
    width  = (width  == narrowSize) ? 0 : differ;
    height = (width == 0) ? differ : 0;

    Bitmap resizedBitmap = Bitmap.createBitmap(bm, width, height, narrowSize, narrowSize);
    bm.recycle();
    return resizedBitmap;
}