gpt4 book ai didi

android - 将旋转位图保存到 SD 卡后图像质量差

转载 作者:行者123 更新时间:2023-11-29 21:21:20 25 4
gpt4 key购买 nike

我正在制作一个应用程序,在其中一个 Activity 中,我从图库中获取图像并在适配器中显示它,如下图所示

enter image description here

我必须旋转该图像并将其保存到 SD 卡。我的代码运行良好,但将其保存到 SD 卡后,图像质量很差。我的代码是:

 viewHolder.imgViewRotate.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

imagePosition = (Integer) v.getTag();
Matrix matrix = new Matrix();
matrix.postRotate(90);

Bitmap rotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

try {
FileOutputStream out = new FileOutputStream(new File(uriList.get(rotatePosition).toString()));
rotated.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}

notifyDataSetChanged();

}
});

任何建议都会有很大帮助。

最佳答案

尝试下面的代码来减小图像大小而不损失其质量:

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

已编辑:

使用 BitmapFactory inSampleSize 选项调整图像大小,图像质量丝毫没有下降。代码:

          BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bm = BitmapFactory.decodeFile(tempDir+"/"+photo1_path , bmpFactoryOptions);

int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)600);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)800);

if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio){
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}

bmpFactoryOptions.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(tempDir+"/"+photo1_path, bmpFactoryOptions);

// recreate the new Bitmap
src = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(), bm.getHeight(), matrix, true);
src.compress(Bitmap.CompressFormat.PNG, 100, out);

关于android - 将旋转位图保存到 SD 卡后图像质量差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20696489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com