Java二維碼圖片處理

寫這個篇文章是爲了記錄一下使用Java操作二維碼的一些套路。因爲在做這件事的時候,是遇到了一些問題的,這裏記錄一下,以備不時之需。

需求

根據文字內容生成二維碼,在二維碼中間加入logo圖片,最後將二維碼嵌入外部背景圖中,寫入到指定路徑

效果

測試代碼:

String content = "這是二維碼內容";
String logoPath = "F:/test/qrcode/logo.png";
String backImagePath = "F:/test/qrcode/backImage.jpg";
String outputPath = "F:/test/qrcode/result.jpg";
boolean result = QrCodeUtil.createAndSaveQrCodeImg(content, logoPath, backImagePath, outputPath);

效果:
二維碼圖片處理

maven依賴

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.0</version>
</dependency>

源碼

  1. 文字生成二維碼

此處生成二維碼圖片,使用了自定義的encode方法和resizeAndCreateBufferedImage方法,相關源碼和原因見下方的問題1

	/**
     * 生成二維碼image
     *
     * @param content 二維碼內容
     * @return BufferedImage
     */
    private static BufferedImage createQrCodeImage(String content) {
        return createQrCodeImage(content, 200, 200);
    }

    /**
     * 生成二維碼image
     *
     * @param content 二維碼內容
     * @param width   寬度
     * @param height  長度
     * @return bufferImage
     */
    private static BufferedImage createQrCodeImage(String content, int width, int height) {
        long start = System.currentTimeMillis();
        BufferedImage image = null;
        try {
            Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            hints.put(EncodeHintType.MARGIN, 1);

            // 使用自定義的方法,解決白邊問題
            BitMatrix bitMatrix = encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            // 重新調整大小,滿足輸入寬高
            image = resizeAndCreateBufferedImage(bitMatrix, width, height);
        } catch (WriterException e) {
            logger.error("QRCodeUtil-createQrCodeImage 生成二維碼異常:", e);
        }
        logger.info("QRCodeUtil-createQrCodeImage end. cost:{} ", (System.currentTimeMillis() - start));
        return image;
    }
  1. 讀取圖片信息
	/**
     * 從圖片路徑讀取生成image
     *
     * @param imagePath 圖片文件地址
     * @return bufferedImage
     */
    private static BufferedImage createBufferedImage(String imagePath) {
        long start = System.currentTimeMillis();
        BufferedImage bi = null;
        try {
            BufferedImage tmpImage = ImageIO.read(new File(imagePath));
            // 防止寫入jpg時出現失真異常,這裏new一個新的image包一下
            bi = new BufferedImage(tmpImage.getWidth(), tmpImage.getHeight(), BufferedImage.TYPE_INT_RGB);
            bi.getGraphics().drawImage(tmpImage, 0, 0, null);
        } catch (IOException e) {
            logger.error("讀取文件{} 生成bufferedImage失敗:", imagePath, e);
        }
        logger.info("QRCodeUtil-createBufferedImage end. cost:{}", System.currentTimeMillis() - start);
        return bi;
    }
  1. 二維碼中間填充logo圖片
	/**
     * 二維碼中間插入logo
     *
     * @param codeImage 二維碼image
     * @param logoImage logo image
     * @return 插入結果
     */
    private static boolean combineCodeAndInnerLogo(BufferedImage codeImage, BufferedImage logoImage) {
        return combineCodeAndInnerLogo(codeImage, logoImage, true);
    }

    /**
     * 二維碼中間插入logo
     *
     * @param codeImage    二維碼image
     * @param logoImage    logo image
     * @param needCompress 是否需要壓縮
     * @return 插入結果
     */
    private static boolean combineCodeAndInnerLogo(BufferedImage codeImage, Image logoImage, boolean needCompress) {
        boolean result;
        try {
            int logoWidth = logoImage.getWidth(null);
            int logoHeight = logoImage.getHeight(null);
            // 如果設置了需要壓縮,則進行壓縮
            if (needCompress) {
                logoWidth = logoWidth > LOGO_MAX_HEIGHT ? LOGO_MAX_WIDTH : logoWidth;
                logoHeight = logoHeight > LOGO_MAX_HEIGHT ? LOGO_MAX_HEIGHT : logoHeight;
                Image image = logoImage.getScaledInstance(logoWidth, logoHeight, Image.SCALE_SMOOTH);
                BufferedImage tag = new BufferedImage(logoWidth, logoHeight, BufferedImage.TYPE_INT_RGB);
                Graphics gMaker = tag.getGraphics();
                // 繪製縮小後的圖
                gMaker.drawImage(image, 0, 0, null);
                gMaker.dispose();
                logoImage = image;
            }

            // 在中心位置插入logo
            Graphics2D codeImageGraphics = codeImage.createGraphics();
            int codeWidth = codeImage.getWidth();
            int codeHeight = codeImage.getHeight();
            int x = (codeWidth - logoWidth) / 2;
            int y = (codeHeight - logoHeight) / 2;
            codeImageGraphics.drawImage(logoImage, x, y, logoWidth, logoHeight, null);
            Shape shape = new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, 6, 6);
            codeImageGraphics.setStroke(new BasicStroke(3f));
            codeImageGraphics.draw(shape);
            codeImageGraphics.dispose();
            result = true;
        } catch (Exception e) {
            logger.error("QRCodeUtil-combineCodeAndInnerLogo 二維碼中間插入logo失敗:", e);
            result = false;
        }
        return result;
    }
  1. 將背景圖填充上生成的二維碼
	/**
     * 合成二維碼image和背景圖image
     *
     * @param codeImage 二維碼image
     * @param backImage 背景圖image
     */
    private static BufferedImage combineCodeAndBackImage(BufferedImage codeImage, BufferedImage backImage) {
        return combineCodeAndBackImage(codeImage, backImage, -1, 100);
    }

    /**
     * 合成二維碼image和背景圖image,指定二維碼底部距離背景圖底部的距離
     *
     * @param codeImage    二維碼image
     * @param backImage    背景圖image
     * @param marginLeft   二維碼距離背景圖左邊距離,如果爲-1,則左右居中
     * @param marginBottom 二維碼距離背景圖底部距離
     * @return bufferedImage
     */
    private static BufferedImage combineCodeAndBackImage(BufferedImage codeImage, BufferedImage backImage, int marginLeft, int marginBottom) {
        long start = System.currentTimeMillis();
        Graphics2D backImageGraphics = backImage.createGraphics();
        // 確定二維碼在背景圖的左上角座標
        int x = marginLeft;
        if (marginLeft == -1) {
            x = (backImage.getWidth() - codeImage.getWidth()) / 2;
        }
        int y = backImage.getHeight() - codeImage.getHeight() - marginBottom;
        // 組合繪圖
        backImageGraphics.drawImage(codeImage, x, y, codeImage.getWidth(), codeImage.getHeight(), null);
        backImageGraphics.dispose();
        logger.info("QRCodeUtil-combineCodeAndBackImage end. cost:{}", System.currentTimeMillis() - start);
        return backImage;
    }
  1. 保存圖片文件到指定路徑
	/**
     * 保存圖片文件到指定路徑
     *
     * @param image      圖片image
     * @param outputPath 指定路徑
     * @return 操作結果
     */
    private static boolean imageSaveToFile(BufferedImage image, String outputPath) {
        boolean result;
        try {
            //  爲了保證大圖背景不變色,formatName必須爲"png"
            ImageIO.write(image, "png", new File(outputPath));
            result = true;
        } catch (IOException e) {
            logger.error("QRCodeUtil-imageSaveToFile 保存圖片到{} 失敗:,", outputPath, e);
            result = false;
        }
        return result;
    }

問題

下面列舉一下當時遇到的一些問題

  1. 生成的二維碼白邊很大
    默認使用zxing生成的二維碼可以指定二維碼長寬,但是整個圖片規格是固定的,只能是固定的幾個規格。這就導致如果我們需要指定生成二維碼長寬的話,外邊框會有留白。具體原因可以網上搜索,這裏不贅述。
    解決方法是重寫zxing相應的方法(com.google.zxing.qrcode.QRCodeWriter#encode(java.lang.String,com.google.zxing.BarcodeFormat, int, int, java.util.Map)),重新縮放調整二維碼大小
	/**
     * 修改encode生成邏輯,刪除白邊
     * 源碼見com.google.zxing.qrcode.QRCodeWriter#encode(java.lang.String,
     * com.google.zxing.BarcodeFormat, int, int, java.util.Map)
     *
     * @param contents 二維碼內容
     * @param format   格式
     * @param width    寬度
     * @param height   長度
     * @param hints    hints
     * @return BitMatrix
     * @throws WriterException exception
     */
    private static BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
                                    Hashtable<EncodeHintType, ?> hints) throws WriterException {
        if (contents.isEmpty()) {
            throw new IllegalArgumentException("Found empty contents");
        }

        if (format != BarcodeFormat.QR_CODE) {
            throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
        }

        if (width < 0 || height < 0) {
            throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
                    height);
        }

        ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
        int quietZone = QUIET_ZONE_SIZE;
        if (hints != null) {
            if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
                errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
            }
            if (hints.containsKey(EncodeHintType.MARGIN)) {
                quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
            }
        }

        QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
        return renderResult(code, width, height, quietZone);
    }

    /**
     * 對 zxing 的 QRCodeWriter 進行擴展, 解決白邊過多的問題。去除白邊的主要邏輯
     *
     * @param code      qrcode
     * @param width     期望寬度
     * @param height    期望高度
     * @param quietZone quietZone
     * @return BitMatrix
     */
    private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
        ByteMatrix input = code.getMatrix();
        if (input == null) {
            throw new IllegalStateException();
        }
        // xxx 二維碼寬高相等, 即 qrWidth == qrHeight
        int inputWidth = input.getWidth();
        int inputHeight = input.getHeight();
        int qrWidth = inputWidth + (quietZone * 2);
        int qrHeight = inputHeight + (quietZone * 2);
        // 白邊過多時, 縮放
        int minSize = Math.min(width, height);
        int scale = calculateScale(qrWidth, minSize);
        if (scale > 0) {
            int padding, tmpValue;
            // 計算邊框留白
            padding = (minSize - qrWidth * scale) / QUIET_ZONE_SIZE * quietZone;
            tmpValue = qrWidth * scale + padding;
            if (width == height) {
                width = tmpValue;
                height = tmpValue;
            } else if (width > height) {
                width = width * tmpValue / height;
                height = tmpValue;
            } else {
                height = height * tmpValue / width;
                width = tmpValue;
            }
        }
        int outputWidth = Math.max(width, qrWidth);
        int outputHeight = Math.max(height, qrHeight);
        int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
        int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
        int topPadding = (outputHeight - (inputHeight * multiple)) / 2;

        BitMatrix output = new BitMatrix(outputWidth, outputHeight);
        for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
            // Write the contents of this row of the barcode
            for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
                if (input.get(inputX, inputY) == 1) {
                    output.setRegion(outputX, outputY, multiple, multiple);
                }
            }
        }
        return output;
    }

    /**
     * 如果留白超過15% , 則需要縮放
     * (15% 可以根據實際需要進行修改)
     *
     * @param qrCodeSize 二維碼大小
     * @param expectSize 期望輸出大小
     * @return 返回縮放比例, <= 0 則表示不縮放, 否則指定縮放參數
     */
    private static int calculateScale(int qrCodeSize, int expectSize) {
        if (qrCodeSize >= expectSize) {
            return 0;
        }
        int scale = expectSize / qrCodeSize;
        int abs = expectSize - scale * qrCodeSize;
        if (abs < expectSize * 0.15) {
            return 0;
        }
        return scale;
    }

    /**
     * 縮放調整二維碼大小,使之符合期望大小
     *
     * @param matrix matrix
     * @param width  期望寬度
     * @param height 期望高度
     * @return bufferedImage
     */
    private static BufferedImage resizeAndCreateBufferedImage(BitMatrix matrix, int width, int height) {
        int qrCodeWidth = matrix.getWidth();
        int qrCodeHeight = matrix.getHeight();
        BufferedImage qrCode = new BufferedImage(qrCodeWidth, qrCodeHeight, BufferedImage.TYPE_INT_RGB);

        for (int x = 0; x < qrCodeWidth; x++) {
            for (int y = 0; y < qrCodeHeight; y++) {
                qrCode.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }

        // 若二維碼的實際寬高和預期的寬高不一致, 則縮放
        if (qrCodeWidth != width || qrCodeHeight != height) {
            BufferedImage tmp = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            tmp.getGraphics().drawImage(
                    qrCode.getScaledInstance(width, height,
                            java.awt.Image.SCALE_SMOOTH), 0, 0, null);
            qrCode = tmp;
        }

        return qrCode;
    }

然後在調用生成二維碼時,使用自定義的方法

  1. 生成的圖片有顏色失真現象
    我在使用的時候,如果生成的圖片存儲爲jpg格式時,可能會出現圖片顏色異常的情況。這是因爲jpg格式採用了有損壓縮,會導致圖片失真。

解決方法是在保存到本地時(imageSaveToFile),存儲爲png格式

//  爲了保證大圖背景不變色,formatName必須爲"png"
ImageIO.write(image, "png", new File(outputPath));

這種方法有一個不足,就是如果原圖是png格式的話,圖片一般都比較大,比較佔用本地內存,網絡傳輸時,也會比較慢,影響體驗。

如果我們將原圖轉爲jpg格式,可以有效減少圖片的大小,生成的圖片大小也會相應的減少。但是測試發現也可能出現圖片失真的情況。後來發現,在讀取圖片文件時(createBufferedImage),轉存一下就可以解決這個問題。(具體爲啥會這樣,如果有人知道,歡迎指教)

// 防止寫入jpg時出現失真異常,這裏new一個新的image包一下
bi = new BufferedImage(tmpImage.getWidth(), tmpImage.getHeight(), BufferedImage.TYPE_INT_RGB);
bi.getGraphics().drawImage(tmpImage, 0, 0, null);

源碼地址

https://download.csdn.net/download/somehow1002/12262631

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章