#
vincentlu
2 天以前 9cea4833f937cd6dfb49299d0240215581c79188
zy-acs-common/src/main/java/com/zy/acs/common/utils/QrCodeCodecSupport.java
@@ -1,5 +1,7 @@
package com.zy.acs.common.utils;
import com.zy.acs.framework.common.Cools;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
@@ -22,6 +24,16 @@
    public static int qrCodeBytes() {
        return config.bytes;
    }
    public static String normalize(String qrCode) {
        if (Cools.isEmpty(qrCode)) {
            throw new IllegalArgumentException("qrCode must not be empty");
        }
        if (config.mode == Mode.NUMERIC) {
            return normalizeNumericValue(qrCode, config.bytes, config.displayLength);
        }
        return normalizeStringValue(qrCode, config.bytes, config.charset);
    }
    public static String decode(byte[] bytes, int pos) {
@@ -48,10 +60,7 @@
        if (value.isEmpty()) {
            value = "0";
        }
        BigInteger numeric = new BigInteger(value);
        if (numeric.signum() < 0) {
            throw new IllegalArgumentException("qrCode must not be negative: " + qrCode);
        }
        BigInteger numeric = parseUnsignedNumericValue(value, qrCode);
        byte[] raw = numeric.toByteArray();
        if (raw.length > 1 && raw[0] == 0) {
            raw = Arrays.copyOfRange(raw, 1, raw.length);
@@ -65,15 +74,40 @@
    }
    private static byte[] toFixedStringBytes(String qrCode, int size, Charset charset) {
        String value = qrCode == null ? "" : qrCode;
        byte[] raw = value.getBytes(charset);
        String value = normalizeStringValue(qrCode, size, charset);
        return value.getBytes(charset);
    }
    private static String normalizeNumericValue(String qrCode, int size, int displayLength) {
        BigInteger numeric = parseUnsignedNumericValue(qrCode, qrCode);
        byte[] raw = numeric.toByteArray();
        if (raw.length > 1 && raw[0] == 0) {
            raw = Arrays.copyOfRange(raw, 1, raw.length);
        }
        if (raw.length > size) {
            throw new IllegalArgumentException("qrCode exceeds configured byte size: " + qrCode);
        }
        byte[] result = new byte[size];
        Arrays.fill(result, (byte) '0');
        System.arraycopy(raw, 0, result, size - raw.length, raw.length);
        return result;
        return Utils.zeroFill(numeric.toString(), displayLength);
    }
    private static String normalizeStringValue(String qrCode, int size, Charset charset) {
        String value = qrCode == null ? "" : qrCode.trim();
        byte[] raw = value.getBytes(charset);
        if (raw.length != size) {
            throw new IllegalArgumentException("qrCode byte length must be " + size + ": " + qrCode);
        }
        return value;
    }
    private static BigInteger parseUnsignedNumericValue(String value, String originalValue) {
        if (!value.chars().allMatch(Character::isDigit)) {
            throw new IllegalArgumentException("qrCode must be numeric: " + originalValue);
        }
        BigInteger numeric = new BigInteger(value);
        if (numeric.signum() < 0) {
            throw new IllegalArgumentException("qrCode must not be negative: " + originalValue);
        }
        return numeric;
    }
    private static String stripPadding(String value) {