我创建了一个函数,可以将位图直接缩放到特定的表面区域。该函数首先获取位图的宽度和高度,然后查找最接近所需大小的样本大小。最后,图像缩放到确切的大小。这是我能找到解码缩放位图的唯一方法。问题是从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);
}
我增加了这个功能,以缩放大型相机图像到特定数量的百万像素。因此,一个典型的区域通过100万像素为100万。解码后的摄像机图像产生一个1952年的outWidth和一个3264的输出。然后我用这种方法计算比例,我可以用缩放的图像保持相同的高宽比,在这种情况下比率是0.598 ...使用比例和新的表面积,我可以找到新的宽度是773和1293的高度。773x1293 = 999489这仅仅是大约100万像素。接下来,我计算解码新图像的样本大小,在这种情况下,样本大小为4,图像解码为976x1632。所以我通过了1273的高度773的宽度。
什么是inSampleSize和宽度和高度的典型值? – EboMike 2011-05-12 06:53:51