java图片高斯模糊

使用ImageEditor.jar包进行高斯模糊

ImageEditor.jar 提供很多强大的滤镜和图片处理效果

GaussianFilter gaussianFilter = new GaussianFilter();
BufferedImage fromImage = ImageIO.read(new File("d://a.jpg"));
BufferedImage toImage = new BufferedImage(fromImage.getWidth(), fromImage.getHeight(),BufferedImage.TYPE_INT_RGB);
gaussianFilter.setRadius(15);
gaussianFilter.filter(fromImage, toImage);
ImageIO.write(toImage, "jpg", new File("d:\\overwrite3.jpg"));

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是Java中使用高斯模糊算法对Bitmap进行处理的代码示例: ```java public static Bitmap gaussianBlur(Bitmap src, float radius) { if (src == null) { return null; } int width = src.getWidth(); int height = src.getHeight(); int[] pixels = new int[width * height]; src.getPixels(pixels, 0, width, 0, 0, width, height); int[] resultPixels = new int[width * height]; System.arraycopy(pixels, 0, resultPixels, 0, pixels.length); int[] alpha = new int[256]; for (int i = 0; i < 256; i++) { alpha[i] = (int) (Math.exp(-((float) i * i) / (2 * radius * radius)) / (Math.sqrt(2 * Math.PI) * radius) * 255); } int[] box = new int[(int) (2 * radius)]; int boxWidth = (int) (radius / Math.sqrt(2 * Math.log(255))); int boxSize = boxWidth * 2 + 1; for (int i = 0; i < boxSize; i++) { box[i] = alpha[(int) (255 * i / boxSize)]; } for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int red = 0; int green = 0; int blue = 0; int alphaSum = 0; for (int i = -boxWidth; i <= boxWidth; i++) { int pixelIndex = x + clamp(i, 0, width - 1) + y * width; int pixel = pixels[pixelIndex]; int coeff = box[Math.abs(i)]; red += Color.red(pixel) * coeff; green += Color.green(pixel) * coeff; blue += Color.blue(pixel) * coeff; alphaSum += Color.alpha(pixel) * coeff; } int resultPixelIndex = x + y * width; resultPixels[resultPixelIndex] = Color.argb(alphaSum >> 8, red >> 8, green >> 8, blue >> 8); } } Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); result.setPixels(resultPixels, 0, width, 0, 0, width, height); return result; } private static int clamp(int value, int min, int max) { return Math.max(min, Math.min(value, max)); } ``` 其中,使用了以下公式计算高斯函数的系数: ``` alpha[i] = exp(-i^2 / (2 * radius^2)) / (sqrt(2 * PI) * radius) * 255 ``` 对于每个像素点,都会计算出一个以该像素点为中心的正方形框,框内像素点的加权平均值作为该像素点的值。每个像素点的加权系数使用上述公式计算得到。在处理边缘像素点时,使用了clamp函数将超出边界的像素点坐标限制在合法范围内。最后,将处理后的像素数组生成为一个新的Bitmap并返回。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值