zoukankan      html  css  js  c++  java
  • zxing生成和解析二维码工具类

    1.依赖

            <!--QRCode-->
            <dependency>
                <groupId>com.google.zxing</groupId>
                <artifactId>core</artifactId>
                <version>3.1.0</version>
            </dependency>

    2.工具类

    import com.google.zxing.*;
    import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
    import com.google.zxing.common.BitMatrix;
    import com.google.zxing.common.HybridBinarizer;
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    import org.springframework.web.multipart.MultipartFile;
    
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.util.HashMap;
    import java.util.Hashtable;
    import java.util.UUID;
    
    @Component
    @Slf4j
    public class QRCodeUtil {
        private static final String CHARSET = "utf-8";
        private static final String FORMAT_NAME = "jpg";
        // 二维码尺寸
        private static final int QRCODE_SIZE = 300;
    
        // 二维码保存地址
        private static String path;
    
        public static String getPath() {
            return path;
        }
    
        @Value("${file.linux.path}")
        public void setPath(String path) {
            QRCodeUtil.path = path;
        }
        /**
         * 生成二维码
         * @param content
         * @return
         * @throws Exception
         */
        public static String createImage(String content) throws Exception {
            Hashtable<EncodeHintType, Object> 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 width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
                }
            }
            File files = new File(path);
            log.debug("文件夹{}",files);
            if (!files.exists()) {//目录不存在就创建
                files.mkdirs();
            }
            String newName = UUID.randomUUID().toString();
            ImageIO.write(image, FORMAT_NAME, new File(files + File.separator + newName + "." + FORMAT_NAME));
            return path + newName + "." + FORMAT_NAME;
        }
        public static String readImage(MultipartFile file) throws Exception {
            MultiFormatReader formatReader = new MultiFormatReader();
            BufferedImage image = ImageIO.read(file.getInputStream());
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
            HashMap hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            hints.put(EncodeHintType.MARGIN, 1);
            Result result = formatReader.decode(binaryBitmap,hints);
            return result.getText();
        }
    }
  • 相关阅读:
    Java 实现常见内排序
    markdown基本语法
    HashMap (JDK1.8) 分析
    jQuery总结
    JS 数组 常用方法
    CSS样式 解决文字过长显示省略号问题
    Python中的 __name__属性的含义和作用
    http协议详细介绍
    Http和Https的区别
    爬虫——requests库使用方法
  • 原文地址:https://www.cnblogs.com/i-tao/p/14048048.html
Copyright © 2011-2022 走看看