钢镚儿冲刺一周期第一天
一、说在前面
今天的目标是实现使用java完成对图片像素点的操作进而实现图片的褪色处理,作为之后梦想清单的原型;
二、大体思路
由于有一点PS的基础,所以明白褪色相应的算法原理,根据RGB的数量对颜色进行判断进而通过代码遍历图片的每个像素点并更形成相应的黑白色。并且Android和java的关系非常近,使用java写出来之后那迁移到Android会简单很多。
三、代码
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URLDecoder; import javax.imageio.ImageIO; public class Image { private static String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); public void binaryImage() throws IOException { File file = new File("C:\\Users\\26218\\Desktop\\c.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("d.jpg"); ImageIO.write(grayImage, "jpg", newFile); } public void grayImage() throws IOException { File file = new File("c.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("e.jpg"); ImageIO.write(grayImage, "jpg", newFile); } public static void main(String[] args) throws IOException { Image demo = new Image(); basePath=URLDecoder.decode(basePath,"utf-8"); demo.binaryImage(); demo.grayImage(); } }