Java 二维码生成与解析

525 阅读3分钟

前言

二维码是一种能够将大量信息编码在小空间的编码器。它是一种由黑白方块构成的图片,可以将文本、链接、图像等不同类型的信息编码到其中。二维码已经广泛应用于不同的领域,如电子支付、电子门票、商品销售等,这种快捷、高效的信息处理方式已成为现代生活中不可或缺的一部分。

所需依赖

	<!-- 二维码生成 -->
	<dependency>
	    <groupId>com.google.zxing</groupId>
	    <artifactId>core</artifactId>
	    <version>3.5.1</version>
	</dependency>
	<dependency>
	    <groupId>com.google.zxing</groupId>
	    <artifactId>javase</artifactId>
	    <version>3.5.1</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-mock -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-mock</artifactId>
	    <version>2.0.8</version>
	    <scope>compile</scope>
	</dependency>

主要代码

需要用到的一些常量

	/** 字符编码 */
	private static final String CHARSET = "utf-8";

	/** 图片格式 */
    private static final String FORMAT_NAME = "JPG";
    
    /** 二维码尺寸 */
    private static final Integer QRCODE_SIZE = 380;
    
    /** LOGO宽度 */
    private static final Integer WIDTH = 100;
    
    /** LOGO高度 */
    private static final Integer HEIGHT = 100;

生成二维码


	/**
     * 生成二维码,得到 MultipartFile--通用
     * @param jsonString 二维码内容
     * @param logoPath logo路径
     * @param bottomText 底部文字
     * @return {@link MultipartFile}
     * @ver v1.0.0
     */
    public static MultipartFile createQrCode(String jsonString, String logoPath, String bottomText) throws Exception {

        return initMultipartFile(jsonString, logoPath, bottomText);
    }

    /**
     * 生成二维码,得到 MultipartFile-有logo、无底部文字
     * @param jsonString 二维码内容
     * @param logoPath logo路径
     * @return {@link MultipartFile}
     * @ver v1.0.0
     */
    public static MultipartFile createQrCode(String jsonString, String logoPath) throws Exception {

        return initMultipartFile(jsonString, logoPath, null);
    }

    /**
     * 生成二维码,得到 MultipartFile-无logo、无底部文字
     * @param jsonString 二维码内容
     * @return {@link MultipartFile}
     * @ver v1.0.0
     */
    public static MultipartFile createQrCode(String jsonString) throws Exception {

        return initMultipartFile(jsonString, null, null);
    }

    /**
     * 生成二维码
     * @param content 二维码内容
     * @param logoPath logo路径
     * @param bottomText 底部文字
     * @return {@link MultipartFile}
     * @ver v1.0.0
     */
    private static MultipartFile initMultipartFile(String content, String logoPath, String bottomText) throws Exception {
        //二维码名称
        String qrName = "qr" + System.currentTimeMillis() + ".jpg";

        int height = 0;
        if (StringUtils.hasText(bottomText)) {
            height = 10;
        }

        //得到BufferedImage对象
        BufferedImage bufferedImage = createImage(content, logoPath, true, height);
        // 判断是否添加底部文字
        if (StringUtils.hasText(bottomText)) {
            addBottomText(bufferedImage, bottomText);
        }

        //创建一个ByteArrayOutputStream
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        //把BufferedImage写入ByteArrayOutputStream
        ImageIO.write(bufferedImage, "jpg", os);
        //ByteArrayOutputStream转成InputStream
        InputStream input = new ByteArrayInputStream(os.toByteArray());
        //InputStream转成MultipartFile
        return new MockMultipartFile("qrFile", qrName, "text/plain", input);
    }

    /**
     * 生成 BufferedImage
     * @param content 存放在二维码中的内容
     * @param imgPath logo的路径及名称
     * @param needCompress 是否需要压缩
     * @param height 额外高度(有底部文字时)
     * @return {@link BufferedImage}
     * @ver v1.0.0
     */
    private static BufferedImage createImage(String content, String imgPath, boolean needCompress, int height) throws Exception {
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter()
                .encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
        int bitWidth = bitMatrix.getWidth();
        int bitHeight = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(bitWidth, bitHeight + height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < bitWidth; x++) {
            for (int y = 0; y < bitHeight; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        if (imgPath == null || "".equals(imgPath)) {
            return image;
        }
        // 插入logo图片
        insertImage(image, imgPath, needCompress);
        return image;
    }

    /**
     * 插入logo图片
     * @param source 二维码
     * @param logoPath logo的路径及名称
     * @param needCompress 是否需要压缩
     * @ver v1.0.0
     */
    private static void insertImage(BufferedImage source, String logoPath, boolean needCompress) throws Exception {
        File file = new File(logoPath);
        if (!file.exists()) {
            log.error(logoPath + "   该文件不存在!");
            return;
        }
        Image src = ImageIO.read(new File(logoPath));
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        // 压缩LOGO
        if (needCompress) {
            if (width > WIDTH) {
                width = WIDTH;
            }
            if (height > HEIGHT) {
                height = HEIGHT;
            }
            Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            // 绘制缩小后的图
            g.drawImage(image, 0, 0, null);
            g.dispose();
            src = image;
        }
        // 插入LOGO
        Graphics2D graph = source.createGraphics();
        int x = (QRCODE_SIZE - width) / 2;
        int y = (QRCODE_SIZE - height) / 2;
        graph.drawImage(src, x, y, width, height, null);
        Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
        graph.setStroke(new BasicStroke(3f));
        graph.draw(shape);
        graph.dispose();
    }

    /**
     * 二维码底部添加文字
     * @param source 二维码
     * @param bottomText 底部文字
     * @ver v1.0.0
     */
    private static void addBottomText(BufferedImage source, String bottomText) {
        //生成image
        int defineWidth = QRCODE_SIZE;
        int defineHeight = 20;
        BufferedImage textImage = new BufferedImage(defineWidth, defineHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) textImage.getGraphics();
        //开启文字抗锯齿
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.setBackground(Color.WHITE);
        g2.clearRect(0, 0, defineWidth, defineHeight);
        g2.setPaint(Color.BLACK);
        FontRenderContext context = g2.getFontRenderContext();
        //部署linux需要注意 linux无此字体会显示方块
        Font font = new Font("微软雅黑", Font.BOLD, 15);
        g2.setFont(font);
        LineMetrics lineMetrics = font.getLineMetrics(bottomText, context);

        FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true);
        Rectangle rec = font.getStringBounds(bottomText, frc).getBounds();
        double fontWidth = rec.getWidth();
        double offset = (defineWidth - fontWidth) / 2;

        float y = (defineHeight + lineMetrics.getAscent() - lineMetrics.getDescent() - lineMetrics.getLeading()) / 2;
        g2.drawString(bottomText, (int) offset, (int) y);

        Graphics2D graph = source.createGraphics();
        //开启文字抗锯齿
        graph.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //添加image
        int width = textImage.getWidth(null);
        int height = textImage.getHeight(null);

        graph.drawImage(textImage, 0, QRCODE_SIZE - 8, width, height, Color.WHITE, null);
        graph.dispose();
    }

二维码解析


	/**
     * 根据二维码路径解析二维码
     * @param path 二维码路径
     * @return {@link String}
     * @ver v1.0.0
     */
    public static String decode(String path) throws Exception {
        return decode(new File(path));
    }
    
    /**
     * 根据二维码文件解析二维码
     * @param file 二维码文件
     * @return {@link String}
     * @ver v1.0.0
     */
    public static String decode(File file) throws Exception {
        BufferedImage image = ImageIO.read(file);
        if (image == null) {
            return null;
        }
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result;
        Hashtable hints = new Hashtable();
        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
        result = new MultiFormatReader().decode(bitmap, hints);
        return result.getText();
    }

是不是很简单0.0 好了,到这就结束啦!!!

---------------------------------------------------------THE END---------------------------------------------------