【发布时间】:2011-08-23 20:02:22
【问题描述】:
我创建了一个将位图直接缩放到特定表面区域的函数。该函数首先获取位图的宽度和高度,然后找到最接近所需大小的样本大小。最后,图像被缩放到确切的大小。这是我能找到解码缩放位图的唯一方法。问题是从 BitmapFactory.createScaledBitmap(src,width,height,filter) 返回的位图总是以 -1 的宽度和高度返回。我已经实现了使用 createScaledBitmap() 方法的其他函数而没有出现此错误,并且我找不到任何创建缩放位图会产生无效输出的原因。我还发现,如果我创建一个可变的图像位图副本,则会导致相同的错误。谢谢
public static Bitmap load_scaled_image( String file_name, int area) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file_name, options);
double ratio = (float)options.outWidth / (float)options.outHeight;
int width, height;
if( options.outWidth > options.outHeight ) {
width = (int)Math.sqrt(area/ratio);
height = (int)(width*ratio);
}
else {
height = (int)Math.sqrt(area/ratio);
width = (int)(height*ratio);
}
BitmapFactory.Options new_options = new BitmapFactory.Options();
new_options.inSampleSize = Math.max( (options.outWidth/width), (options.outHeight/height) );
Bitmap image = BitmapFactory.decodeFile(file_name, new_options);
return Bitmap.createScaledBitmap(image, width, height, true);
}
我添加了这个功能来将大型相机图像缩放到特定数量的兆像素。因此,输入的典型区域为 1000000(1 兆像素)。解码后的相机图像产生 1952 的 outWidth 和 3264 的 outHieght。然后我以这种方式计算比率,我可以与缩放图像保持相同的高宽比,在这种情况下,比率为 0.598... 使用比率和新的表面积我可以找到新的宽度,即 773,高度为 1293。773x1293=999489,大约 1 兆像素。接下来我计算解码新图像的样本大小,在这种情况下,样本大小为 4,图像被解码为 976x1632。所以我传递的宽度为 773,高度为 1293。
【问题讨论】:
-
inSampleSize 以及宽度和高度的典型值是什么?