我有一個小的方法,旨在調整圖像大小(存儲爲文件)以產生具有某種預定義文件大小的新圖像(在我的情況下爲512 KB )。該方法看起來是這樣的:Android - 縮小圖像尺寸不會減小尺寸的比例
private static final int maximumFileSizeInKBs = 512;
public static Uri resizeImage(Context context, Uri imageUri) {
// get image width and height
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap imageBitmap = BitmapFactory.decodeFile(new File(imageUri.getPath()).getAbsolutePath(), options);
int imageWidth = options.outWidth;
int imageHeight = options.outHeight;
// get image file size (in KBs)
File file = new File(imageUri.getPath());
int fileSizeInKBs = Integer.parseInt(String.valueOf(file.length()/1024));
// get the scaling factor
// (square root because when we scale both the width and height by the
// square root of the scaling factor, then the size of the final image
// will be scaled by the scaling factor)
double scalingFactor = Math.sqrt(fileSizeInKBs/maximumFileSizeInKBs);
// no need to scale if file already within maximum file size limit
if(scalingFactor <= 1) {
return imageUri;
}
// get scaled dimensions
int scaledImageWidth = (int) Math.floor(imageWidth/scalingFactor);
int scaledImageHeight = (int) Math.floor(imageHeight/scalingFactor);
// load original bitmap (load a scaled copy to avoid OutOfMemory errors)
options = new BitmapFactory.Options();
options.inSampleSize = Math.max(imageHeight/scaledImageWidth, imageHeight/scaledImageHeight);
imageBitmap = BitmapFactory.decodeFile(new File(imageUri.getPath()).getAbsolutePath(), options);
// the scaled image above will be scaled by a value equal to the
// nearest power of 2, hence it wont be scaled to the exact value
// that we want. So, we need to calculate new scaling factors
// based on the scaled loaded bitmap
Matrix m = new Matrix();
RectF inRect = new RectF(0, 0, imageWidth, imageHeight);
RectF outRect = new RectF(0, 0, scaledImageWidth, scaledImageHeight);
m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
float[] values = new float[9];
m.getValues(values);
// create a scaled bitmap with required file size
Bitmap scaledBitmap = Bitmap.createScaledBitmap(imageBitmap, (int) (imageWidth*values[0]), (int) (imageHeight*values[4]), true);
// delete original image
new File(imageUri.getPath()).delete();
// write bitmap to file
File newImageFile = BaseResourceClass.makeTempResourceFile(Slide.ResourceType.IMAGE, context);
try {
newImageFile.createNewFile();
FileOutputStream fos = new FileOutputStream(newImageFile);
scaledBitmap.compress(Bitmap.CompressFormat.PNG, 1, fos);
fos.close();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, "Something went wrong", Toast.LENGTH_SHORT).show();
}
// recycle bitmaps to reallocate memory
Storage.recycleBitmap(imageBitmap);
Storage.recycleBitmap(scaledBitmap);
// scaling done, return new Image
return Uri.fromFile(newImageFile);
}
當這個方法運行,圖像尺寸得到縮小到確切大小(如在什麼我希望它是),但在KB的圖像大小不按比例縮小由相同的因素。例如,運行此方法後,我得到了以下數字
before resizing
imageWidth : 3120, imageHeight : 4160, fileSize : 2960 KB
after resizing
imageWidth : 1395, imageHeight : 1860, fileSize : 2491 KB
我不明白,從圖像消除這麼多的像素之後,究竟是什麼佔用了這麼大的空間,以保持圖像文件的大小這麼多。試圖看看其他的StackOverflow問題和Android的BitmapFactory和Bitmap類文檔,但這個問題沒有提到。任何幫助,將不勝感激!