java為圖片上面畫線
package com.bootdo.testDemo;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Stroke;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
public class TestDemo {
@Test
public void test() throws IOException {
// 56 64 136 144
File imgFile=new File("D:\\1.jpg");
File imgFile2=new File("D:\\2.jpg");
BufferedImage srcimg=ImageIO.read(imgFile);
Graphics2D g2d= srcimg.createGraphics();
g2d.drawImage(srcimg,0,0,null);
Stroke dash = new BasicStroke(2.5f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_ROUND, 3.5f, new float[] { 15, 10, },
0f);
//虛線
g2d.setStroke(dash);
g2d.setColor(Color.RED);
g2d.drawRect(56,64,80,80);
g2d.dispose();
ImageIO.write(srcimg, "JPG", imgFile2);
}
;
}