最近学习了一下绘画公章,看到一篇文章非常好,照着做了一下文章链接很感谢这位佬把代码开源,并很仔细点的讲解
。但其实存在一个问题Graphics2D绘画的会很模糊 所以后续打算学习一下使用SVG进行绘画。。。后续会继续更新。
public static void main(String[] args) throws IOException {int imageSize = 150;BufferedImage bufferedImage = new BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_ARGB);Graphics2D g2d = bufferedImage.createGraphics();// 防止出现锯齿g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);g2d.setComposite(AlphaComposite.Src);g2d.setColor(Color.red);g2d.setStroke(new BasicStroke(3));// 绘画起点xy坐标并非圆心坐标g2d.drawOval(2, 2, imageSize - 4, imageSize - 4);// 绘画五角星int r = 20;int centerPoint = imageSize / 2;Map<String, List<Integer>> pointersMap = new HashMap<>();// 五角星6点坐标 O为圆心点pointersMap.put("O", Arrays.asList(centerPoint, centerPoint));pointersMap.put("A", Arrays.asList(centerPoint, centerPoint - r));pointersMap.put("B", Arrays.asList((int)(centerPoint + Math.sin(0.4 * Math.PI) * r), (int)(centerPoint - Math.cos(0.4 * Math.PI) * r)));pointersMap.put("C", Arrays.asList((int)(centerPoint + Math.sin(0.2 * Math.PI) * r), (int)(centerPoint + Math.cos(0.2 * Math.PI) * r)));pointersMap.put("D", Arrays.asList((int)(centerPoint - Math.sin(0.2 * Math.PI) * r), (int)(centerPoint + Math.cos(0.2 * Math.PI) * r)));pointersMap.put("E", Arrays.asList((int)(centerPoint - Math.sin(0.4 * Math.PI) * r), (int)(centerPoint - Math.cos(0.4 * Math.PI) * r)));// 6点组合绘画图像List<String> pointerComposes = Arrays.asList("A,D,O,C", "B,E,O,D", "C,A,O,E", "D,A,O,B", "E,C,O,B");// 查找绘画点进行绘画for (String pointerCompose : pointerComposes) {String[] pointerKeys = pointerCompose.split(",");Polygon polygon = new Polygon();for (String pointerKey : pointerKeys) {polygon.addPoint(pointersMap.get(pointerKey).get(0), pointersMap.get(pointerKey).get(1));}g2d.fillPolygon(polygon);}int whiteSpace = 75;double padding = 1.5;String companyName = "XXXXXXXXXXXXXXXXXX";// 根据名称长度 动态设置字体大小 防止文字过多发生文字碰撞int fontSize = (60 - companyName.length()) / 3;// 平移画布使环形文字在圆圈内g2d.translate(centerPoint - fontSize / 2, 0);// 文字开始位置 大概在左下角g2d.rotate(((double) whiteSpace / 360) * Math.PI + Math.PI, fontSize / 2, centerPoint);Font font = new Font("FangSong_GB2312", Font.ROMAN_BASELINE, fontSize);g2d.setFont(font);// 在文字开始位置 第二个字符开始进行按计算角度进行旋转 从而生成环形文字double rotateAngle = (double) (360 - whiteSpace) * Math.PI / (180 * (companyName.length() - 1));for (int i = 0; i < companyName.toCharArray().length; i++) {if (i != 0) {g2d.rotate(rotateAngle, fontSize/2, centerPoint);}g2d.drawString(String.valueOf(companyName.toCharArray()[i]), 0, (int) (fontSize * padding));}ByteArrayOutputStream baos = new ByteArrayOutputStream();ImageIO.write(bufferedImage, "png", baos);String path = String.format("D:/data/seal_image/seal.%s.png",DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS").format(LocalDateTime.now()));File file = new File(path);FileOutputStream fileOutputStream = new FileOutputStream(file);fileOutputStream.write(baos.toByteArray());baos.close();fileOutputStream.close();}