首页 文章

Android裁剪中心位图

提问于
浏览
131

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

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

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

9 回答

  • 5

    enter image description here

    这可以通过以下方式实现: 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() 
         );
    }
    
  • 1

    虽然上面的大多数答案提供了一种方法来实现这一点,但已经有一种内置的方法来实现这一点,它是一行代码( 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 height)在API级别8中添加创建所需大小的居中位图 . 参数source原始位图源宽度目标宽度高度目标高度

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

  • 3

    你有没有考虑过 layout.xml 这样做?您可以为ScaleType设置ScaleTypeandroid:scaleType="centerCrop" 并在 layout.xml 内的 ImageView 中设置图像的尺寸 .

  • 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

  • 281

    这是一个更完整的代码段,用于裁剪任意尺寸的 [bitmap] 的中心,并将结果缩放到您想要的 [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);
    }
    
  • 1

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

    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;
    
  • 12

    要纠正@willsteel解决方案:

    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);
                }
    
  • 321
    public static Bitmap resizeAndCropCenter(Bitmap bitmap, int size, boolean recycle) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        if (w == size && h == size) return bitmap;
        // scale the image so that the shorter side equals to the target;
        // the longer side will be center-cropped.
        float scale = (float) size / Math.min(w,  h);
        Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
        int width = Math.round(scale * bitmap.getWidth());
        int height = Math.round(scale * bitmap.getHeight());
        Canvas canvas = new Canvas(target);
        canvas.translate((size - width) / 2f, (size - height) / 2f);
        canvas.scale(scale, scale);
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (recycle) bitmap.recycle();
        return target;
    }
    
    private static Bitmap.Config getConfig(Bitmap bitmap) {
        Bitmap.Config config = bitmap.getConfig();
        if (config == null) {
            config = Bitmap.Config.ARGB_8888;
        }
        return config;
    }
    
  • 9
    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;
    }
    

相关问题