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();
        }
    }
  • 相关阅读:
    正则表达式匹配整数和小数
    解决任务计划程序未启动任务,因为相同任务的实例正在运行的问题
    ActiveMQ 消息持久化到数据库(Mysql、SQL Server、Oracle、DB2等)
    C# CLR20R3 程序终止的几种解决方案
    彻底消除wine中文乱码,QQ,kugoo等等....
    Fedora如何添加第三方软件源?
    [转]Fedora 下安装NVIDIA显卡驱动(使用后无法进入图形界面)
    向fedora添加rpmfusion源
    [转]Java 8 Optional类深度解析(null处理)
    [转载]深入理解Java 8 Lambda
  • 原文地址:https://www.cnblogs.com/i-tao/p/14048048.html
Copyright © 2011-2022 走看看