您的位置:首页 > 财经 > 金融 > Java Graphics2D绘画公司印章

Java Graphics2D绘画公司印章

2024/11/16 15:48:33 来源:https://blog.csdn.net/m0_46608027/article/details/141460619  浏览:    关键词:Java Graphics2D绘画公司印章

最近学习了一下绘画公章,看到一篇文章非常好,照着做了一下文章链接很感谢这位佬把代码开源,并很仔细点的讲解
。但其实存在一个问题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();}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com