欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA生成图形验证码或图片的draw方式

程序员文章站 2022-07-10 18:48:46
第一步,生成本地图片再进行操作,经常用于图片验证码,或者加水印,加盖章之类 public void drawImage() throws FileNotFoundException, IOException { int width = 350; int height = 450; int fontSize = 20; Prescription prescriptionResult = prescriptionRepository.g.....

第一步, 生成本地图片 再进行操作,经常用于图片验证码,或者加水印,加盖章之类

 public void drawImage() throws FileNotFoundException, IOException {
        int width = 350;
        int height = 450;
        int fontSize = 20;
        Prescription prescriptionResult = prescriptionRepository.getById(837982537646080L);
        File file = new File("D:/draw/java-draw.jpg");
        //   //字体大小   Font.PLAIN,  Font.BOLD
        Font font = new Font("Serif", Font.BOLD, fontSize);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setBackground(Color.WHITE);
        // g2.clearRect(0, 0, width, height);
        // 背景色填充,这里设置为全部
        g2.fillRect(0, 0, width, height);
        g2.setFont(font);

        List<String> list = new ArrayList();
        list.add("药品名称: " + prescriptionResult.getDrugName());
        list.add(
            "总数量: " + prescriptionResult.getQuantity() + "(" + prescriptionResult.getUnit() + ")");
        list.add("用法: " + prescriptionResult.getUseage());
        list.add(
            "单次剂量: " + prescriptionResult.getSingle() + "(" + prescriptionResult.getSingleUnit()
                + ")");
        list.add("用药频次: " + prescriptionResult.getUseFrequency());
        list.add("疗程: " + prescriptionResult.getCourseTreatment());

        double median = list.size() / 2.0 + 0.5;
        int rowSpacing = 30;
        for (int i = 0; i < list.size(); i++) {
            g2.setPaint(Color.black);
            String text = list.get(i);
            FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
            int strWidth = metrics.stringWidth(text);
            int strHeight = metrics.getHeight();
            //左边位置
            int left = (width - strWidth) / 2;
            // 顶边位置+上升距离(原本字体基线位置对准画布的y坐标导致字体偏上ascent距离,加上ascent后下移刚好顶边吻合)
            int top = (height - strHeight) / 2 + metrics.getAscent();
            top = (int) (top + (Double.parseDouble(String.valueOf(i + 1)) - median) * fontSize);
            g2.drawString(text, left, top);

        }
        ImageIO.write(bi, "jpg", file);
    }


2 下面是换行的参考方法,叠加有些坐标问题需要调试

 //drawText(g2, text, 500, font, 100, 0, rowSpacing, Color.black, Color.black);
    static void drawText(Graphics2D g, String text, int srcImgWidth, Font font, int beginX,
                         int beginY, int rowSpacing, Color backGroundColor, Color fontColor) {
        if (backGroundColor != null) {
            g.setColor(backGroundColor);
            g.fillRect(beginX - 40, beginY - 5, font.getSize() * 4 + 50 * 2,
                font.getSize() + 15 * 2);
        }
        if (fontColor != null) {
            g.setColor(fontColor);
        } else {
            g.setColor(Color.black);
        }
        g.setFont(font);
        int fontSize = font.getSize();
        //文字叠加,自动换行叠加
        int tempX = beginX + 2;
        int tempY = beginY + fontSize;
        int tempCharLen = 0;//单字符长度
        int tempLineLen = 0;//单行字符总长度临时计算
        StringBuffer sb = new StringBuffer();
        int textMaxWidth = srcImgWidth - 10;

        for (int i = 0; i < text.length(); i++) {
            char tempChar = text.charAt(i);
            tempCharLen = getCharLen(tempChar, g);
            tempLineLen += tempCharLen;
            if (tempLineLen >= textMaxWidth) {
                //长度已经满一行,进行文字叠加
                g.drawString(sb.toString(), tempX, tempY);
                sb.delete(0, sb.length());//清空内容,重新追加
                tempY += (fontSize + rowSpacing);
                tempLineLen = 0;
            }
            sb.append(tempChar);//追加字符
        }
        g.drawString(sb.toString(), tempX, tempY);//最后叠加余下的文字
    }

    public static int getCharLen(char c, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charWidth(c);

    }

本文地址:https://blog.csdn.net/limingcai168/article/details/108236058

相关标签: java 图片 draw