选择图像并调整为72x72

时间:2014-01-04 15:45:41

标签: android bitmap

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

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

修改

代码:

@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;
    }
}

没用。检查缩放72x72和原始的:

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

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

3 个答案:

答案 0 :(得分:0)

试试这个 -

Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);

或其他方式 -

resized = Bitmap.createScaledBitmap(yourBitmap,(int)(yourBitmap.getWidth()*0.8), (int)(yourBitmap.getHeight()*0.8), true);

答案 1 :(得分:0)

不,这并不难。只需使用Bitmap.createScaledBitmap()

Bitmap.createScaledBitmap(bitmap, width, height, true);

答案 2 :(得分:0)

这是一个方法的例子,它会给我一个最大120x120的图像,希望这可以帮助你:

public static Bitmap decodeWithBounds(String srcImg) {

    Bitmap image;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(srcImg, options);


    if (options.outHeight > 120 || options.outWidth > 120) {

        options.inSampleSize = Math.max(
                Math.round(options.outHeight / 120),
                Math.round(options.outWidth / 120));
    }
    options.inJustDecodeBounds = false;
    image = BitmapFactory.decodeFile(srcImg, options);
    return image;

}