我在發佈前真的在整個網上搜索過。我的問題是,我無法調整位圖的大小而不會丟失圖像的質量(質量非常差和像素化)。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