在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?
更多推荐
发布评论