gpt4 book ai didi

java - 安卓框模糊算法

转载 作者:搜寻专家 更新时间:2023-11-01 09:11:12 26 4
gpt4 key购买 nike

我一直在尝试在 android 中实现框模糊算法。该代码似乎没问题,但在尝试应用它时,模糊图像中的某些区域在整个模糊照片中都有大的黄色和白色污迹。谁能帮我找出我做错了什么?谢谢:

这是我所拥有的:

public static Bitmap boxBlur(Bitmap bmp, int range) {
assert (range & 1) == 0 : "Range must be odd.";

Bitmap blurred = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),
Config.ARGB_8888);
Canvas c = new Canvas(blurred);

int w = bmp.getWidth();
int h = bmp.getHeight();

int[] pixels = new int[bmp.getWidth() * bmp.getHeight()];
bmp.getPixels(pixels, 0, w, 0, 0, w, h);

boxBlurHorizontal(pixels, w, h, range / 2);
boxBlurVertical(pixels, w, h, range / 2);

c.drawBitmap(pixels, 0, w, 0.0F, 0.0F, w, h, true, null);

return blurred;
}

private static void boxBlurHorizontal(int[] pixels, int w, int h,
int halfRange) {
int index = 0;
int[] newColors = new int[w];

for (int y = 0; y < h; y++) {
int hits = 0;
long r = 0;
long g = 0;
long b = 0;
for (int x = -halfRange; x < w; x++) {
int oldPixel = x - halfRange - 1;
if (oldPixel >= 0) {
int color = pixels[index + oldPixel];
if (color != 0) {
r -= Color.red(color);
g -= Color.green(color);
b -= Color.blue(color);
}
hits--;
}

int newPixel = x + halfRange;
if (newPixel < w) {
int color = pixels[index + newPixel];
if (color != 0) {
r += Color.red(color);
g += Color.green(color);
b += Color.blue(color);
}
hits++;
}

if (x >= 0) {
newColors[x] = Color.argb(0xFF, (byte) (r / hits),
(byte) (g / hits), (byte) (b / hits));
}
}

for (int x = 0; x < w; x++) {
pixels[index + x] = newColors[x];
}

index += w;
}
}

private static void boxBlurVertical(int[] pixels, int w, int h,
int halfRange) {

int[] newColors = new int[h];
int oldPixelOffset = -(halfRange + 1) * w;
int newPixelOffset = (halfRange) * w;

for (int x = 0; x < w; x++) {
int hits = 0;
long r = 0;
long g = 0;
long b = 0;
int index = -halfRange * w + x;
for (int y = -halfRange; y < h; y++) {
int oldPixel = y - halfRange - 1;
if (oldPixel >= 0) {
int color = pixels[index + oldPixelOffset];
if (color != 0) {
r -= Color.red(color);
g -= Color.green(color);
b -= Color.blue(color);
}
hits--;
}

int newPixel = y + halfRange;
if (newPixel < h) {
int color = pixels[index + newPixelOffset];
if (color != 0) {
r += Color.red(color);
g += Color.green(color);
b += Color.blue(color);
}
hits++;
}

if (y >= 0) {
newColors[y] = Color.argb(0xFF, (byte) (r / hits),
(byte) (g / hits), (byte) (b / hits));
}

index += w;
}

for (int y = 0; y < h; y++) {
pixels[y * w + x] = newColors[y];
}
}
}

最佳答案

找到问题了!
线路:
newColors[x] = Color.argb(0xFF, (byte) (r/hits), (byte) (g/hits), (byte) (b/hits));

我将平均值转换为字节,它们应该是整数。
将其更改为:

newColors[x] = Color.argb(0xFF, (int) (r / hits), (int) (g / hits), (int) (b / hits));

关于java - 安卓框模糊算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8218438/

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