在此之前首先要了解几个比较重要的知识点BufferedImage,ImageIO,Graphics2D,Color类的操作方法
1.BufferedImage类
BufferedImage是Image是抽象类的实现类,主要作用是将图片加载到内存中,方便我们对其进行处理。
这里介绍一下几个构造方法。都是可以创建一个图像
//指定宽高、图像字节灰度
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY)
//创建一个不带透明色的对象
BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//创建一个带透明色的对象
new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
其中width是图像宽度,height是图像高度,第三个是图像类型
另外介绍一个方法getGraphics,改方法是获取画笔的意思,可以通过获取Graphics2D的对象进行对图片修改,这个下面讲到怎么使用,除了getGraphics方法,还有getWidth获取图片宽getHeight获取图片高度,getMinY获取图片最小高度,即y的值,getMinX为获取图片最小宽度,即x值
getRGB方法,获取图片某一个点,或者区域的颜色,这样返回的是一个int,基本看不懂,这里就要了解一下Color类了,下面会介绍
2.ImageIO总共两个方法要介绍,分别是read和write,都是静态方法
read是可以将一个图片读取为BufferedImage类型的对象,示例
BufferedImage image = ImageIO.read(new FileInputStream(new File("D:\\Desktop\\tu\\tu.jpg")));
有了image我们就能对图像进行操作。
write是可以将BufferedImage的对象输入到一个具体的文件中,如下所示
ImageIO.write(image,"jpg", new FileOutputStream("D:\\Desktop\\tu\\tu.jpg"));
3.Graphics2D类提供了强大的绘图能力
这里介绍几个方法setColor(Color color)设置画笔的颜色,drawRect(int x,int y,int width,int height)方法画矩阵,前面的x,y指定起笔的位置,后面指定宽高。
画圆角矩形:drawRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight),线框,最后两个宽高是圆角弧的横向直径和纵向直径;fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight),颜色填充。
setBackground(Color color)设置背景颜色
setFont(Font font);设置字体
drawString(String str,int x,int y);向图片中写入str字符串,从坐标为x,y的位置开始显示
drawLine(int x1,int y1,int x2,int y2);画直线,从x1,y1到x2,y2,如果相同就是个点
drawPolyline(int[],int[],int),各点的x、y坐标,折线数。
更多方法请上官网查看,这些基本上也就够用了
4.Color类是用来封装颜色的
所谓RGB颜色模式是指使用三种基色:红、绿、蓝,通过三种颜色的调整得出其它各种颜色,这三种基色的值范围为0~255。例如Color c = new Color(255,200,0);定义橙色 ,更多的看下表
Color类有一个构造器
public Color(int red, int green, int blue, int alpha)
其中前面三个是基础颜色的值,后面一个指透明的程度,当为255表示完全不透明
书接上文BufferedImage类的getRGB的方法可以转换为Color类便于我们分析,如下
Color color = new Color(image.getRGB(x, y));
这样就可以获取到一个像素的颜色了
5.实战,我们先画一个图出来
public static void writePhoto(){ try { BufferedImage image=new BufferedImage(640, 512, BufferedImage.TYPE_INT_RGB); Graphics2D g2=(Graphics2D)image.getGraphics(); g2.setColor(Color.WHITE); g2.fillRect(0,0,640,512);//为整个画布天上白色 for (int i=image.getMinY();i<image.getHeight();i++){ for (int j=image.getMinX();j<image.getWidth();j++){ if(i==j){ g2.setColor(new Color(255,0,0));//设置颜色 g2.drawRect(i,j,1,1); } } } ImageIO.write(image,"jpg", new FileOutputStream("D:\\Desktop\\tu\\tu.jpg")); }catch (Exception e){ e.printStackTrace(); } }
上诉方法获得一个分割线的图片
然后我们将这个图片的红色线换位绿色的线,通过遍历实现
public static void writePhoto1(){ try { BufferedImage image=ImageIO.read(new FileInputStream("D:\\Desktop\\tu\\tu.jpg")) ; Graphics2D g2=(Graphics2D)image.getGraphics(); for (int i=image.getMinY();i<image.getHeight();i++){ for (int j=image.getMinX();j<image.getWidth();j++){ Color color = new Color(image.getRGB(j, i)); if(color.getRed()>150&&color.getGreen()<30&&color.getBlue()<30){ g2.setColor(Color.green); g2.drawRect(j,i,1,1); } } } ImageIO.write(image,"jpg", new FileOutputStream("D:\\Desktop\\tu\\tu.jpg")); }catch (Exception e){ e.printStackTrace(); } }
由于上面加了颜色,所以不是特别准确,因此,我们用范围去判断,只要他在红色范围内都让他变为绿色,实现替换。最终效果如下