QRCodeUtils.java 7.78 KB
Newer Older
liqin's avatar
liqin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
package cn.wisenergy.chnmuseum.party.common.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 * <p>
 * 二维码工具类(使用ZXingjar包)
 * </p>
 *
 * @author Tom
 * @since 2018/9/29
 */
public class QRCodeUtils {
    // 默认宽为300
    private Integer width = 300;
    // 默认高为300
    private Integer height = 300;
    // 默认二维码图片格式
    private String imageFormat = "png";
    // 默认二维码字符编码
    private String charType = "utf-8";
    // 默认二维码的容错级别
    private ErrorCorrectionLevel corretionLevel = ErrorCorrectionLevel.H;
    // 二维码与图片的边缘
    private Integer margin = 1;
    // 二维码参数
    private Map<EncodeHintType, Object> encodeHits = new HashMap<>();

    //main方法测试工具类
    public static void main(String[] args) {
        QRCodeUtils qrCode = new QRCodeUtils(300, 300);
        //设置二维码的边缘为1
        qrCode.setMargin(1);
        //设置输出到桌面
        String path = System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "test.png";
        //创建图片二维码
        qrCode.createQrImage("https://www.baidu.com/", path);
        //识别图片二维码的内容
        System.out.println(qrCode.decodeQrImage(new File(path)));//https://www.baidu.com/
    }

    public QRCodeUtils(Integer width, Integer height, String imageFormat, String charType,
                       ErrorCorrectionLevel corretionLevel, Integer margin) {
        if (width != null) {
            this.width = width;
        }
        if (height != null) {
            this.height = height;
        }
        if (imageFormat != null) {
            this.imageFormat = imageFormat;
        }
        if (charType != null) {
            this.charType = charType;
        }
        if (corretionLevel != null) {
            this.corretionLevel = corretionLevel;
        }
        if (margin != null) {
            this.margin = margin;
        }
    }

    public QRCodeUtils(Integer width, Integer height, String imageFormat, String charType,
                       ErrorCorrectionLevel corretionLevel) {
        this(width, height, imageFormat, charType, corretionLevel, null);
    }

    public QRCodeUtils(Integer width, Integer height, String imageFormat, String charType, Integer margin) {
        this(width, height, imageFormat, charType, null, margin);
    }

    public QRCodeUtils(Integer width, Integer height, String imageFormat, String charType) {
        this(width, height, imageFormat, charType, null, null);
    }

    public QRCodeUtils(Integer width, Integer height, String imageFormat) {
        this(width, height, imageFormat, null, null, null);
    }

    public QRCodeUtils(Integer width, Integer height) {
        this(width, height, null, null, null, null);
    }

    public QRCodeUtils() {
    }

    // 初始化二维码的参数
    private void initialParamers() {
        // 字符编码
        encodeHits.put(EncodeHintType.CHARACTER_SET, this.charType);
        // 容错等级 L、M、Q、H 其中 L 为最低, H 为最高
        encodeHits.put(EncodeHintType.ERROR_CORRECTION, this.corretionLevel);
        // 二维码与图片边距
        encodeHits.put(EncodeHintType.MARGIN, margin);
    }

    // 得到二维码图片
    public BufferedImage getBufferedImage(String content) {
        initialParamers();
        BufferedImage bufferedImage = null;
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, this.width,
                    this.height, this.encodeHits);
            bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
        } catch (WriterException e) {
            e.printStackTrace();
            return null;
        }
        return bufferedImage;
    }

    // 将二维码保存到输出流中
    public void writeToStream(String content, OutputStream os) {
        initialParamers();
        try {
            BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, this.width, this.height,
                    this.encodeHits);
            MatrixToImageWriter.writeToStream(matrix, this.imageFormat, os);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 将二维码图片保存为文件
    public void createQrImage(String content, File file) {
        initialParamers();
        try {
            BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, this.width, this.height, this.encodeHits);
            MatrixToImageWriter.writeToFile(matrix, this.imageFormat, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 将二维码图片保存到指定路径
    public void createQrImage(String content, String path) {
        initialParamers();
        try {
            BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, this.width, this.height, this.encodeHits);
            MatrixToImageWriter.writeToPath(matrix, this.imageFormat, new File(path).toPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //识别图片二维码
    public String decodeQrImage(File file) {
        String content = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(new FileInputStream(file));
            LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap image = new BinaryBitmap(binarizer);
            Map<DecodeHintType, Object> decodeHits = new HashMap<>();
            decodeHits.put(DecodeHintType.CHARACTER_SET, this.charType);
            Result result = new MultiFormatReader().decode(image, decodeHits);
            content = result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content;
    }

    public Integer getWidth() {
        return width;
    }

    public void setWidth(Integer width) {
        this.width = width;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public String getImageFormat() {
        return imageFormat;
    }

    public void setImageFormat(String imageFormat) {
        this.imageFormat = imageFormat;
    }

    public String getCharType() {
        return charType;
    }

    public void setCharType(String charType) {
        this.charType = charType;
    }

    public ErrorCorrectionLevel getCorretionLevel() {
        return corretionLevel;
    }

    public void setCorretionLevel(ErrorCorrectionLevel corretionLevel) {
        this.corretionLevel = corretionLevel;
    }

    public Integer getMargin() {
        return margin;
    }

    public void setMargin(Integer margin) {
        this.margin = margin;
    }

    public Map<EncodeHintType, Object> getHits() {
        return encodeHits;
    }

    public void setHits(Map<EncodeHintType, Object> hits) {
        this.encodeHits = hits;
    }

}