gpt4 book ai didi

android - 将位图转换为固定大小

转载 作者:行者123 更新时间:2023-11-29 20:11:58 25 4
gpt4 key购买 nike

比如我有一张10mb的图片;我想将其转换为 300kb。我经历过很多例子用过

bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);

(这里将 100 更改为较小的值会减小大小,但它如何导致接近 300-350kb 的大小)

BitmapFactory.decodeFile(filePath, options);

我提供的地方

options.inSampleSize = 5/*样本*/;

但不知何故我遗漏了一些东西。

更新通过将 11mb 转换为 2mb 来结算。如果我找到更好的方法,将会更新。

最佳答案

我认为因为 PNG 是无损的,所以质量参数没有影响。它不会“压缩”您的 PNG。然而,这种方法适用于 jpg:

反复试验,结果为 binary search会让你很快接近,可能需要 3-4 次尝试,具体取决于可接受范围的大小。

int minQuality = 10;
int maxQuality = 100;
long size = 0;
while(true) {
int mid = (maxQuality + minQuality)/2;
long size = compress(mid);

if (size > minSize) { //too large
if (maxQuality == minQuality){
throw new RuntimeException("Cannot compress this image down in to this size range.");
}
maxQuality = mid - 1;
continue;
}

if (size < maxSize) { //too small
if(maxQuality == 100){
break; //this means the image is smaller than the acceptable range even at 100
}
minQuality = mid + 1;
continue;
}

break;//done, falls in range
}

关于android - 将位图转换为固定大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34629124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com