我在发布前真的在整个网上搜索过。我的问题是,我无法调整位图的大小而不会丢失图像的质量(质量非常差和像素化)。Android:在不丢失质量的情况下调整位图大小
我从相机取得位图,然后我必须缩小它的大小,这样我就可以更快地将它上传到服务器。
这是做采样
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;
的功能,这就是我从活动结果获得图像
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);
}
}
}
我开始认为最好的方式来获得小图片尺寸是设置相机分辨率。任何人都可以帮忙吗?
谢谢...但我已经试过: resizedBitmap = Bitmap.createScaledBitmap(bitmap,newWidth,newHeight,true); 但它是一样的...仍然块状和像素化 – Vahn84 2013-05-09 15:51:23
我明白了。我记住的最后一个提示是你检查BitmapFactory.Options类,初始化和解码你的位图时使用质量相关的选项。这里的API链接:http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html – Mena 2013-05-09 17:47:31
我已经达到了至少可以接受的解决从位图uri bitmap.factory选项的位图。但它仍然不是我想的质量。 Facebook和G +如何处理照片上传?我看到他们调整了您上传的所有图片。他们如何做到这一点,而不会丢失一个质量像素? – Vahn84 2013-05-10 07:23:23