【发布时间】:2015-07-28 05:30:22
【问题描述】:
我需要一些有关 BitmapFactory.decode 和 BitmapFactory.createScaledBitmap 的帮助。
在我们的应用程序中,我们使用以下代码来调整图像大小:
public static synchronized File resizeBitmap(File file, int expectedWidth) throws IOException {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
float width = bitmap.getWidth() / (float) expectedWidth;
int expectedHeight = (int) (bitmap.getHeight() / width);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, expectedWidth, expectedHeight, true);
String path = Environment.getExternalStorageDirectory().toString();
File tempFile = new File(path, "temp.jpg");
if (tempFile.exists())
tempFile.delete();
FileOutputStream fileOutputStream = new FileOutputStream(tempFile.getAbsolutePath());
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
return tempFile;
}
但是我们在之后的位图上有质量损失。我们能做些什么来解决这个问题?
如您所见,图像变得更加清晰
【问题讨论】:
标签: android bitmap bitmapfactory