#java.awt绘制多边形
核心API
drawPolygon(int xPoints[], int yPoints[], int nPoints)
示例
/**
* 根据图像Base64字符串、摄像机id、算法id画多边形(支持同一画面画多个多边形)
* @param base64 图像base64字符串
* @param cameraId 坐标json字符传 已绘制的坐标框坐标信息
* 坐标如 [{"x":800,"y":600},{"x":1000,"y":850},{"x":950"y":1050},{"x":850,"y":1200},{"x":820,"y":600}]
* @param algId 算法id
* @return 图像Base64字符串
*/
public static String drawPolygon(String cameraId,String base64,String algId){
LambdaQueryWrapper<CameraAlgorithmRelation> queryWrapper = new LambdaQueryWrapper();
queryWrapper.eq(CameraAlgorithmRelation::getCameraId,cameraId)
.eq(CameraAlgorithmRelation::getAlgorithmInfoId,algId);
CameraAlgorithmRelation relation = cameraAlgorithmRelationMapper.selectOne(queryWrapper);
if (!Objects.isNull(relation)){
String points = relation.getAlgArea();
List<List<PointDTO>> pointList = new ArrayList<>();
if (StringUtils.isNotEmpty(points)){
pointList = JSON.parseObject(points,List.class);
byte[] byteArray = Base64.decode(base64);
String fileSuffix = getImageBase64Format(byteArray);
if (StringUtils.isEmpty(fileSuffix)) {
throw new BusinessException("获取文件格式异常");
}
BufferedImage bufferedImage = null;
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray);
try {
bufferedImage = ImageIO.read(inputStream);
} catch (IOException e) {
log.error("读取图片异常:.......{}",e.getLocalizedMessage());
e.printStackTrace();
}
Graphics2D graphics2D = bufferedImage.createGraphics();
for (List<PointDTO> pointDTOList : pointList) {
JSONArray array = JSONObject.parseArray(JSON.toJSONString(pointDTOList));
List<PointDTO> pointDTOS = array.toJavaList(PointDTO.class);
List<Integer> xList = pointDTOS.stream().map(PointDTO::getX).collect(Collectors.toList());
List<Integer> yList = pointDTOS.stream().map(PointDTO::getY).collect(Collectors.toList());
int[] x = xList.stream().mapToInt(Integer::valueOf).toArray();
int[] y = yList.stream().mapToInt(Integer::valueOf).toArray();
graphics2D.setColor(Color.GREEN);
graphics2D.drawPolygon(x, y, pointDTOS.size());
}
return bufferImageToBase64(bufferedImage);
}
}
return null;
}
/**
* 验证图片格式
* @param path
* @return java.lang.String
*/
private static String getImageBase64Format(byte[] b) {
String type = "";
int temp = (b[0] & 255) << 8 | b[1] & 255;
if (16973 == temp) {
type = "bmp";
} else if (35152 == temp) {
type = "png";
} else if (65496 == temp) {
type = "jpg";
} else if (18249 == temp) {
type = "gif";
} else if (21065 == temp) {
type = "webp";
}
return type;
}
/**
* 图片路径读取转Base64字符串
* @param path
* @return java.lang.String
*/
public static String convertBase64(String path){
String base64 = "";
try {
URL url = ResourceUtil.getResource(path);
BufferedImage bufferedImage = cn.hutool.core.img.ImgUtil.read(url);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage,"jpg",stream);
base64 = Base64.encode(stream.toByteArray());
} catch (IOException e){
throw new RuntimeException("图片转换Base64失败");
}
return base64;
}
/**
* BufferedImage转Base64字符串
* @param bufferedImage
* @return java.lang.String
*/
public static String bufferImageToBase64(BufferedImage bufferedImage) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
ImageIO.write(bufferedImage,"jpg",stream);
} catch (IOException e) {
log.error("BufferedImage转Base64字符串失败",e.getLocalizedMessage());
}
return Base64.encode(stream.toByteArray());
}