要求:在原图片上,根据坐标点,框出指定位置
原图:
已知坐标位置:
"location": {
"width": 440,
"top": 3,
"height": 716,
"left": 2289
}
绘制后图片:
代码示例:
package com.test.demo;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
public class DrawImage {
public static void main(String[] args) {
int width = 440;
int height = 716;
int y = 3;//top
int x = 2289;//left
String oriImagePath = "F:\\image\\hjsb44";
String imagetype = ".jpg";
String oriImage = oriImagePath + imagetype;
String copyImageName = "_copy";
String drawImage = oriImagePath + copyImageName + imagetype;
BufferedImage bufferedImage = null;
//读取图片文件,得到BufferedImage对象
try {
bufferedImage = ImageIO.read(new FileInputStream(oriImage));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//得到Graphics2D 对象
Graphics2D g2D=(Graphics2D)bufferedImage.getGraphics();
//设置颜色、画笔粗细
g2D.setColor(Color.RED);
g2D.setStroke(new BasicStroke(5));
//绘制矩形
g2D.drawRect(x, y, width, height);
g2D.setFont(new Font("",Font.BOLD,40));
//绘制文字
g2D.drawString("XXX洗衣液", x+10, y+60);
try {
//保存绘制后的新图片
ImageIO.write(bufferedImage, "JPG",new FileOutputStream(drawImage));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}