Java代码实现图片高斯模糊

老金 老金 2022.2.7 21:38 浏览(1079) 评论 (0)

Maven 地址: http://mvnrepository.com/artifact/com.jhlabs/filters

<dependency>
    <groupId>com.jhlabs</groupId>
    <artifactId>filters</artifactId>
    <version>2.0.235-1</version>
</dependency>

 

Java代码例子一:

public static byte[] blur(byte[] data) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    BufferedImage img = ImageIO.read(bais);
    GaussianFilter gaussianFilter = new GaussianFilter();
    gaussianFilter.filter(img, img);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(img, "jpg", baos);
    return baos.toByteArray();
} 

 

Java代码例子二:

// blur a target picture and save to destination picture 
public static void gaussianBlur(String targetPath, String destinationPath) throws IOException {
	GaussianFilter gaussianFilter = new GaussianFilter();
	BufferedImage fromImage = ImageIO.read(new File(targetPath));
	BufferedImage toImage = new BufferedImage(fromImage.getWidth(), fromImage.getHeight(), BufferedImage.TYPE_INT_RGB);
	gaussianFilter.setRadius(150);
	gaussianFilter.filter(fromImage, toImage);
	ImageIO.write(toImage, "webp", new File(destinationPath));
}

 

效果如下:

本文链接 https://www.mangoxo.com/blog/Lo7nbe5G 版权所有,转载请保留地址链接,感谢!

老金 关注
善良的程序宅
  • 91 动态
  • 0 相册
  • 3 粉丝
  • 11 获赞