我正在创建一个应用程序并想要设置Image。我不希望图像尺寸相同,但图像质量相同。如何在Android中调整图像大小但图像质量相同?
我正在使用此方法调整图像大小
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
.getAbsolutePath());
int h = myBitmap.getHeight() / 20;
int w = myBitmap.getWidth() / 20;
Bitmap scaled = Bitmap.createScaledBitmap(myBitmap, w, h,
true);
imageTab.setImageBitmap(scaled);
答案 0 :(得分:0)
在下面的方法
中传递要调整大小的位图和要调整大小的位置 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;
}
答案 1 :(得分:0)
您的要求毫无意义。无论何时调整大小,质量都会改变。降尺度会使您的图像丢失像细线等图像数据,并且升级会使图像模糊并失去清晰度。
这是因为您只有很多图像数据,并且您无法随意更改生成高质量图像所需的数据量。
也就是说,使用像JPG这样的格式可以帮助你保存一些质量,因为JPG会更加压缩数据。
答案 2 :(得分:0)
以下是代码段..
private static BufferedImage resizeImage(BufferedImage originalImage, int type){
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
return resizedImage;
}
BufferedImage resizeImagePng = resizeImage(originalImage, type);
ImageIO.write(resizeImagePng, "png", new File("Image PAth"));