[英]Android: Resizing Bitmaps without losing quality
i've really searched through over the entire web before posting.在发布之前,我真的在整个网络上进行了搜索。 My problem is that i cannot resize bitmap without losing the quality of the image (the quality is really bad and pixelated).
我的问题是我无法在不损失图像质量的情况下调整位图大小(质量非常差且像素化)。
I take the bitmap from camera and then i have to downscale it, so i can upload it to the server much faster.我从相机中获取位图,然后我必须缩小它,这样我就可以更快地将它上传到服务器。
This is the function that does the sampling这是进行采样的函数
public Bitmap resizeBitmap(Bitmap bitmap){
Canvas canvas = new Canvas();
Bitmap resizedBitmap = null;
if (bitmap !=null) {
int h = bitmap.getHeight();
int w = bitmap.getWidth();
int newWidth=0;
int newHeight=0;
if(h>w){
newWidth = 600;
newHeight = 800;
}
if(w>h){
newWidth = 800;
newHeight = 600;
}
float scaleWidth = ((float) newWidth) / w;
float scaleHeight = ((float) newHeight) / h;
Matrix matrix = new Matrix();
// resize the bit map
matrix.preScale(scaleWidth, scaleHeight);
resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawBitmap(resizedBitmap, matrix, paint);
}
return resizedBitmap;
and this is how i get the image from activity result这就是我从活动结果中获取图像的方式
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if(resultCode != RESULT_CANCELED){
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
getContentResolver().notifyChange(mImageUri, null);
ContentResolver cr = this.getContentResolver();
try
{
b = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
Log.d("foto", Integer.toString(b.getWidth()));
Log.d("foto", Integer.toString(b.getHeight()));
addPhoto.setImageBitmap(b);
}
catch (Exception e)
{
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
Log.d("TAG", "Failed to load", e);
}
}
}
I'm starting to think that the best way to get a small picture size is to set the camera resolution.我开始认为获得小尺寸图片的最佳方法是设置相机分辨率。 Anyone else can help?
还有其他人可以帮忙吗?
Good downscaling algorithm (not nearest neighbor like) consists of just 2 steps (plus calculation of the exact Rect for input/output images crop):好的缩小算法(不是最近邻)只包含 2 个步骤(加上计算输入/输出图像裁剪的精确 Rect):
Here is detailed explanation how SonyMobile resolved this task: http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/以下是 SonyMobile 如何解决此任务的详细说明: http : //developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/
Here is the source code of SonyMobile scale utils: http://developer.sonymobile.com/downloads/code-example-module/image-scaling-code-example-for-android/这是 SonyMobile scale utils 的源代码: http : //developer.sonymobile.com/downloads/code-example-module/image-scaling-code-example-for-android/
Try below mentioned code for resizing bitmap.尝试下面提到的用于调整位图大小的代码。
public Bitmap get_Resized_Bitmap(Bitmap bmp, int newHeight, int newWidth) {
int width = bmp.getWidth();
int height = bmp.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap newBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, false);
return newBitmap ;
}
I used this code to downsize my bitmap, and its quality, well.. , was acceptable.我用这段代码缩小了我的位图,它的质量,嗯..,是可以接受的。
Hope this helps.希望这可以帮助。
Try Bitmap.createScaledBitmap .尝试Bitmap.createScaledBitmap 。 It also has an option to filter the source.
它还具有过滤源的选项。
the best rescaling method I have come across the method uses createScaledBitmap
whereby the height and width are calculated based on the bitmap height and width and a scale ratio hence quality is not lost我遇到的最好的重新缩放方法该方法使用
createScaledBitmap
,其中高度和宽度是根据位图高度和宽度以及缩放比例计算的,因此质量不会丢失
public Bitmap resize(Bitmap imaged, int maxWidth, int maxHeight) {
Bitmap image = imaged;
if (maxHeight > 0 && maxWidth > 0) {
int width = image.getWidth();
int height = image.getHeight();
float ratioBitmap = (float) width / (float) height;
float ratioMax = (float) maxWidth / (float) maxHeight;
int finalWidth = maxWidth;
int finalHeight = maxHeight;
if (ratioMax > 1) {
finalWidth = Math.round(((float) maxHeight * ratioBitmap));
} else {
finalHeight = Math.round(((float) maxWidth / ratioBitmap));
}
return image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, false);
}
return image;
}
how to use:如何使用:
Bitmap resizedBitmap = resize(scrBitMap,640,640);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.