gpt4 book ai didi

Android 双线性图像降尺度

转载 作者:行者123 更新时间:2023-11-29 00:23:14 24 4
gpt4 key购买 nike

我正在尝试使用双线性过滤缩小位图,但显然我的代码中有问题,因为图像似乎比最近邻更好,或者只是使用 android 的下采样,但作为 Image Magick 的双线性过滤器还不够好。

您是否发现我的调整大小方法有问题?

private Bitmap resize(Bitmap immutable, int reqWidth, int reqHeight) {
Bitmap bitmap = Bitmap.createBitmap(reqWidth, reqHeight, Config.ARGB_8888);
float scaleFactor = immutable.getHeight() / (float) reqHeight;
for (int x = 1; x < reqWidth - 1; x++) {
for (int y = 1; y < reqHeight - 1; y++) {
float sx = x * scaleFactor;
float sy = y * scaleFactor;
int rx = (int) (x * scaleFactor);
int ry = (int) (y * scaleFactor);

final int tl = immutable.getPixel(rx, ry);
final int tr = immutable.getPixel(rx + 1, ry);
final int bl = immutable.getPixel(rx, ry+1 );
final int br = immutable.getPixel(rx + 1, ry+1);

float xC1 = sx- rx;
float xC2 = 2-xC1;

float yC1 = sy - ry;
float yC2 = 2 - yC1;

xC1/=2;
xC2/=2;
yC1/=2;
yC2/=2;

final float firstAlpha = (Color.alpha(tl) * xC2 + Color.alpha(tr) * xC1);
final float firstRed = (Color.red(tl) * xC2 + Color.red(tr) * xC1);
final float firstBlue = (Color.blue(tl) * xC2 + Color.blue(tr) * xC1);
final float firstGreen = (Color.green(tl) * xC2 + Color.green(tr) * xC1);

final float secondAlpha = (Color.alpha(bl) * xC2 + Color.alpha(br) * xC1);
final float secondRed = (Color.red(bl) * xC2 + Color.red(br) * xC1);
final float secondGreen = (Color.green(bl) * xC2 + Color.green(br) * xC1);
final float secondBlue = (Color.blue(bl) * xC2 + Color.blue(br) * xC1);

int finalColor = Color.argb((int) (yC2 * firstAlpha + yC1 * secondAlpha), (int) (yC2 * firstRed + yC1 * secondRed), (int) (yC2 * firstGreen + yC1
* secondGreen), (int) (yC2 * firstBlue + yC1 * secondBlue));
bitmap.setPixel(x, y, finalColor);
}
}

return bitmap;
}

My Result

而 Image magick 的结果是这样的: Image magick's result

最佳答案

Android 在调用 BitmapFactory.Options::inSampleSize->BitmapFactory.decodeResource() 时使用双线性缩小算法。

如此好的缩小算法(不像最近的邻居)只包含 2 个步骤(加上计算输入/输出矩形裁剪的精确 Rect):

  1. 使用 BitmapFactory.Options::inSampleSize->BitmapFactory.decodeResource() 尽可能接近您需要的分辨率,但不要低于它
  2. 通过使用 Canvas::drawBitmap()
  3. 缩小一点来获得准确的分辨率

这里详细解释了 SonyMobile 如何解决这个任务:http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/

这是 SonyMobile scale utils 的源代码:http://developer.sonymobile.com/downloads/code-example-module/image-scaling-code-example-for-android/

关于Android 双线性图像降尺度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21627996/

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