在Android中模糊图像(Blurring an image in Android)

编程入门 行业动态 更新时间:2024-10-22 03:04:19
在Android中模糊图像(Blurring an image in Android)

我使用以下代码来模糊android中的图像,但它不起作用。 我得到的最终图像是非常扭曲的颜色,而不是我想要的模糊。 我究竟做错了什么?

public Bitmap blurBitmap(Bitmap bmpOriginal) { int width, height; height = bmpOriginal.getHeight(); width = bmpOriginal.getWidth(); Bitmap bmpBlurred = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); for (int i = 1; i < width - 1; i++) { for (int j = 1; j < height - 1; j++) { int color = (int) getAverageOfPixel(bmpOriginal, i, j); bmpBlurred.setPixel(i, j, Color.argb(Color.alpha(color), Color.red(color), Color.green(color), Color.blue(color))); } } return bmpBlurred; } private double getAverageOfPixel(Bitmap bitmap, int i, int j) { return ( bitmap.getPixel(i-1, j-1) + bitmap.getPixel(i-1, j) + bitmap.getPixel(i-1, j+1) + bitmap.getPixel(i, j-1) + bitmap.getPixel(i, j) + bitmap.getPixel(i, j+1) + bitmap.getPixel(i+1, j-1) + bitmap.getPixel(i+1, j) + bitmap.getPixel(i+1, j+1)) / 9; }

I am using the following code to blur an image in android, but it does not work. The final image that I get is with very distorted color and not a blur which I want. What am I doing wrong?

public Bitmap blurBitmap(Bitmap bmpOriginal) { int width, height; height = bmpOriginal.getHeight(); width = bmpOriginal.getWidth(); Bitmap bmpBlurred = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); for (int i = 1; i < width - 1; i++) { for (int j = 1; j < height - 1; j++) { int color = (int) getAverageOfPixel(bmpOriginal, i, j); bmpBlurred.setPixel(i, j, Color.argb(Color.alpha(color), Color.red(color), Color.green(color), Color.blue(color))); } } return bmpBlurred; } private double getAverageOfPixel(Bitmap bitmap, int i, int j) { return ( bitmap.getPixel(i-1, j-1) + bitmap.getPixel(i-1, j) + bitmap.getPixel(i-1, j+1) + bitmap.getPixel(i, j-1) + bitmap.getPixel(i, j) + bitmap.getPixel(i, j+1) + bitmap.getPixel(i+1, j-1) + bitmap.getPixel(i+1, j) + bitmap.getPixel(i+1, j+1)) / 9; }

最满意答案

您的问题是您正在同时组合所有颜色通道,它们都会相互渗透。 您应该将模糊功能分别应用于红色,绿色和蓝色通道。

创建SurfaceView并使用FX_SURFACE_BLUR标志可能更容易吗?

Your problem is that you are combining all the colour channels at once and they all spill into each other. You should apply your blur function to the red, green, and blue channels separately.

Might it be easier to create a SurfaceView and use the FX_SURFACE_BLUR flag?

更多推荐

本文发布于:2023-08-06 17:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454070.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   模糊   Android   Blurring   image

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!