将位图的大小调整为72x72

时间:2014-01-04 16:42:56

标签: android bitmap

我从画廊中挑选了一张图片并对其进行了解码。现在我只想将该位图的大小调整为标准的72x72大小,以便用作个人资料照片。

我搜索了很多但没有任何效果,其中一些人无缘无故地旋转了我的图像,其中一些使图像质量非常低。那难吗?

检查缩放的72x72和原始的:

72x72(如你所见,旋转且质量非常差) http://imgim.com/9958incic3494599.png

原: http://imgim.com/3847incix7666386.png

代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
{ 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode)
    { 
    case 100:   // SELECT_PHOTO
        if(resultCode == RESULT_OK)
        {  
            Uri selectedImage = imageReturnedIntent.getData();
            InputStream imageStream;
            try
            {
                imageStream = getContentResolver().openInputStream(selectedImage);
            }catch (Exception e){ return; }

            Bitmap bm = BitmapFactory.decodeStream(imageStream);
            bm = Bitmap.createScaledBitmap(bm, 72, 72, true);
            UpdateAvatar(bm);
        }
        break;
    }
}

1 个答案:

答案 0 :(得分:1)

尝试此功能:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.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 resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}

这对我有帮助。