java根据坐标在图片上绘图

要求:在原图片上,根据坐标点,框出指定位置
原图:
在这里插入图片描述
已知坐标位置:

"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();
		}
		
	}
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值