Search in sources :

Example 1 with BitMatrixEx

use of com.github.hui.quick.plugin.qrcode.wrapper.BitMatrixEx in project quick-media by liuyueyi.

the class QrCodeGenerateHelper method encode.

/**
 * 对 zxing 的 QRCodeWriter 进行扩展, 解决白边过多的问题
 * <p/>
 * 源码参考 {@link com.google.zxing.qrcode.QRCodeWriter#encode(String, BarcodeFormat, int, int, Map)}
 */
public static BitMatrixEx encode(QrCodeOptions qrCodeConfig) throws WriterException {
    ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
    int quietZone = 1;
    if (qrCodeConfig.getHints() != null) {
        if (qrCodeConfig.getHints().containsKey(EncodeHintType.ERROR_CORRECTION)) {
            errorCorrectionLevel = ErrorCorrectionLevel.valueOf(qrCodeConfig.getHints().get(EncodeHintType.ERROR_CORRECTION).toString());
        }
        if (qrCodeConfig.getHints().containsKey(EncodeHintType.MARGIN)) {
            quietZone = Integer.parseInt(qrCodeConfig.getHints().get(EncodeHintType.MARGIN).toString());
        }
        if (quietZone > QUIET_ZONE_SIZE) {
            quietZone = QUIET_ZONE_SIZE;
        } else if (quietZone < 0) {
            quietZone = 0;
        }
    }
    QRCode code = Encoder.encode(qrCodeConfig.getMsg(), errorCorrectionLevel, qrCodeConfig.getHints());
    BitMatrixEx bitMatrixEx = renderResult(code, qrCodeConfig.getW(), qrCodeConfig.getH(), quietZone);
    clearLogo(bitMatrixEx, qrCodeConfig.getLogoOptions());
    return bitMatrixEx;
}
Also used : QRCode(com.google.zxing.qrcode.encoder.QRCode) BitMatrixEx(com.github.hui.quick.plugin.qrcode.wrapper.BitMatrixEx) ErrorCorrectionLevel(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel)

Example 2 with BitMatrixEx

use of com.github.hui.quick.plugin.qrcode.wrapper.BitMatrixEx in project quick-media by liuyueyi.

the class QrCodeGenerateHelper method renderResult.

/**
 * 对 zxing 的 QRCodeWriter 进行扩展, 解决白边过多的问题
 * <p/>
 * 源码参考 {@link com.google.zxing.qrcode.QRCodeWriter#renderResult(QRCode, int, int, int)}
 *
 * @param code
 * @param width
 * @param height
 * @param quietZone 取值 [0, 4]
 * @return
 */
private static BitMatrixEx 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) {
        if (log.isDebugEnabled()) {
            log.debug("qrCode scale enable! scale: {}, qrSize:{}, expectSize:{}x{}", scale, qrWidth, width, height);
        }
        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;
    BitMatrixEx res = new BitMatrixEx();
    res.setByteMatrix(input);
    res.setLeftPadding(leftPadding);
    res.setTopPadding(topPadding);
    res.setMultiple(multiple);
    res.setWidth(outputWidth);
    res.setHeight(outputHeight);
    return res;
}
Also used : BitMatrixEx(com.github.hui.quick.plugin.qrcode.wrapper.BitMatrixEx) ByteMatrix(com.google.zxing.qrcode.encoder.ByteMatrix)

Aggregations

BitMatrixEx (com.github.hui.quick.plugin.qrcode.wrapper.BitMatrixEx)2 ErrorCorrectionLevel (com.google.zxing.qrcode.decoder.ErrorCorrectionLevel)1 ByteMatrix (com.google.zxing.qrcode.encoder.ByteMatrix)1 QRCode (com.google.zxing.qrcode.encoder.QRCode)1