您现在的位置是:主页 > Web前端技术 > Web前端技术
JAVA灰度化和二值化图片的方法是什么编程语言
IDCBT2022-01-11【服务器技术】人已围观
简介这篇文章主要讲解了“JAVA灰度化和二值化图片的方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“JAVA灰度化和二值
这篇文章主要讲解了“JAVA灰度化和二值化图片的方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“JAVA灰度化和二值化图片的方法是什么”吧!
package image; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageDemo { public void binaryImage() throws IOException{ File file = new File(System.getProperty("user.dir")+"/src/2722425974762424026.jpg"); BufferedImage image = ImageIO.read(file); int width = image.getWidth(); int height = image.getHeight(); BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY); for(int i= 0 ; i < width ; i++){ for(int j = 0 ; j < height; j++){ int rgb = image.getRGB(i, j); grayImage.setRGB(i, j, rgb); } } File newFile = new File(System.getProperty("user.dir")+"/src/2722425974762424028.jpg"); ImageIO.write(grayImage, "jpg", newFile); } public void grayImage() throws IOException{ File file = new File(System.getProperty("user.dir")+"/src/2722425974762424026.jpg"); BufferedImage image = ImageIO.read(file); int width = image.getWidth(); int height = image.getHeight(); BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); for(int i= 0 ; i < width ; i++){ for(int j = 0 ; j < height; j++){ int rgb = image.getRGB(i, j); grayImage.setRGB(i, j, rgb); } } File newFile = new File(System.getProperty("user.dir")+"/src/2722425974762424027.jpg"); ImageIO.write(grayImage, "jpg", newFile); } public static void main(String[] args) throws IOException { ImageDemo demo = new ImageDemo(); demo.binaryImage(); demo.grayImage(); } }标签:很赞哦! ()