java 给图片上画矩形框加上文字

给图片上画矩形框加上文字,比如做图片算法识别

String info  = "[{"key":"py12","locations":[{"h":130.0,"name":"bicycle","prob":73.5542893409729,"w":62.0,"x":183.0,"y":687.0}],"name":"共享单车"}]";
		    String address = "图片http地址";
		    URL url = new URL(address);
	            BufferedImage image = ImageIO.read(url.openStream());
	            Graphics g = image.getGraphics();
	            Graphics2D g2d = (Graphics2D) g.create();
	            g2d.setColor(Color.RED);// 画笔颜色
	            g2d.setStroke(new BasicStroke(2));
	            List<AlertInfo> jobInfoList = JSONArray.parseArray(info, AlertInfo.class);
	            for (AlertInfo alertInfo : jobInfoList) {
	                List<AlertInfo.Locations> locationsList = alertInfo.getLocations();
	                for (AlertInfo.Locations locations : locationsList) {
	                	int x = locations.getX().intValue();
	                    int y = locations.getY().intValue();
	                    int h = locations.getH().intValue();
	                    int w = locations.getW().intValue();
	                    int length = alertInfo.getName().length();
	                    g2d.drawRect(x, y, w, h);// 矩形框(原点x坐标,原点y坐标,矩形的长,矩形的宽)

	                    Graphics2D g2 = (Graphics2D) g.create();

	                    g2.fillRect(x, y - 20, length * 17, 20);//填充一个矩形 左上角坐标(0,0),宽70,高150;填充整张图片
	                    g2.setColor(Color.red); //填充颜色
	                    g2.fillRect(x, y - 20, length * 17, 20);//填充整张图片(其实就是设置背景颜色)
	                    g2.setColor(Color.black);//边框
	                    g2.drawRect(x, y - 20, length * 17 - 1, 20 - 1); //画边框
	                    g2.setFont(new Font("宋体", Font.BOLD, 15)); //设置字体:字体、字号、大小
	                    g2.setColor(Color.white);//设置背景颜色
	                    g2.drawString(alertInfo.getName(), x + 1, y - 3); //向图片上写字符串
	                }
	            }
	            String  base64Str = Imgutil.toBase64(image);//图片base64

效果结果图片

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值