选择一个图像,并调整到的72x72图像

2023-09-07 22:57:59 作者:黑的彻底

我的画廊,去codeD它挑的图像。现在,我只是想调整位图到标准的72x72大小,以便为资料照片使用。

I picked an image from gallery and decoded it. Now I just want to resize that bitmap to standard 72x72 size in order to use as an profile photo.

我搜索了很多,但没有什么工作,他们中的一些旋转,我的形象没有任何理由,他们中的一些,使图像质量非常低。是不是很辛苦?

I searched a lot but nothing worked, some of them rotated my image for no reason, some of them makes image very low quality. Is it that hard?

修改

code:

@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和原来的:

Not useful. Check scaled 72x72 and original one:

的72x72(正如你看到的,旋转和极坏的品质) https://m.xsw88.com/allimgs/daicuo/20230907/6663.png

72x72 (As you see, rotated and very bad quality) https://m.xsw88.com/allimgs/daicuo/20230907/6663.png

原文: https://m.xsw88.com/allimgs/daicuo/20230907/6664.png

推荐答案

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

This is an example of a method that would give me a maximum of 120x120 image, hope this helps you :

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;

}