给图片上画矩形框加上文字,比如做图片算法识别
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);
Graphics2D g2 = (Graphics2D) g.create();
g2.fillRect(x, y - 20, length * 17, 20);
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);
效果结果图片
