Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中InSampleSize计算错误吗?_Android_Bitmap - Fatal编程技术网

在Android中InSampleSize计算错误吗?

在Android中InSampleSize计算错误吗?,android,bitmap,Android,Bitmap,这是谷歌提供的方法 public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1;

这是谷歌提供的方法

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

    final int halfHeight = height / 2;
    final int halfWidth = width / 2;

    // Calculate the largest inSampleSize value that is a power of 2 and keeps both
    // height and width larger than the requested height and width.
    while ((halfHeight / inSampleSize) >= reqHeight
            && (halfWidth / inSampleSize) >= reqWidth) {
        inSampleSize *= 2;
    }
}

return inSampleSize;
}
如果需要将500*500图像的大小调整为100*100,则此片段的结果为4,因为它们使用
halfWidth
halfHeight
。但如果我理解正确,结果应该是8。我认为代码应该修改为:

while ((halfHeight / inSampleSize) > reqHeight
            && (halfWidth / inSampleSize) > reqWidth) {
        inSampleSize *= 2;
    }
inSampleSize *= 2;

有人能解释一下吗?我发现他们多次修改了这段代码,但似乎还是错了?

这一想法是,图像按两个步骤缩小:首先按小于或等于所需缩小采样因子的2的最大幂进行缩小,然后按小于2的量进行缩小,最后得到最终要求的图像大小

如果需要将500*500图像的大小调整为100*100,则此代码段的结果为4,因为它们使用半宽和半高。但如果我理解正确,结果应该是8

如果比例因子为8,则500x500像素的图像将缩小到62x62像素,然后需要放大到请求的100x100像素大小

正如代码注释所说:

计算最大inSampleSize值,该值为2的幂,并使高度和宽度均大于请求的高度和宽度

这将是4,因为这样您将得到一个125x125像素的图像,它大于请求的100x100像素的高度和宽度。其想法是,您希望在最后一步中缩小比例,而不是缩小太远,然后必须向上缩放并获得模糊图像


所需的下采样系数为5,小于或等于该系数的2的最高幂为4。

谢谢,解释是正确的。但我觉得要求的尺寸应该像底线一样,你不能越过。对于700*700图像,重采样大小为175*175,这似乎太大了。这是模糊和记忆之间的折衷。