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)
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)
public static Bitmap extractThumbnail(Bitmap source,int width,int height)
在API级别8中添加创建所需大小的居中位图.
参数source原始位图源宽度目标宽度高度目标高度
在使用接受的答案时,我有时会出现内存错误,并且使用ThumbnailUtils为我解决了这些问题.此外,这更清洁,更可重复使用.
您可以使用以下代码来解决您的问题.
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错误.
有关更多详细信息,请参阅此博客
这是一个更完整的片段,用于裁剪任意尺寸的[位图]的中心,并将结果缩放到所需的[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)
到目前为止可能是最简单的解决方案:
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)
归档时间: |
|
查看次数: |
96937 次 |
最近记录: |