美文网首页
二维码生成工具类

二维码生成工具类

作者: LegendaryTao | 来源:发表于2019-12-20 13:45 被阅读0次
    package com.mydataway.common.utils;
    
    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.MultiFormatWriter;
    import com.google.zxing.client.j2se.MatrixToImageConfig;
    import com.google.zxing.client.j2se.MatrixToImageWriter;
    import com.google.zxing.common.BitMatrix;
    import sun.misc.BASE64Encoder;
    
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * 生成二维码
     * Date:     2019/12/2 10:35
     * @author hetao
     */
    public class QrCodeUtil {
    
        private static final int BLACK = 0xFF000000;
        private static final int WHITE = 0xFFFFFFFF;
    
        private static final MatrixToImageConfig DEFAULT_CONFIG = new MatrixToImageConfig();
    
        /**
         * 生成二维码
         * 经过base64编码
         *
         * @param url
         * @return
         */
        public static String createQrCodeBase64(String url) {
            String base64String = "";
            try {
                Map<EncodeHintType, String> hints = new HashMap<>(4);
                hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    
                BufferedImage bufferedImage = toBufferedImage(bitMatrix);
                ImageIO.write(bufferedImage, "jpg", outputStream);
    
                BASE64Encoder encoder = new BASE64Encoder();
                base64String = encoder.encode(outputStream.toByteArray());
                System.out.println();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "data:image/jpg;base64," + base64String;
        }
    
        /**
         * 生成二维码
         * 生成流
         *
         * @param url
         * @return
         */
        public static String createQrCodeStream(String url) {
            String base64String = "";
            try {
                Map<EncodeHintType, String> hints = new HashMap<>(4);
                hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
                MatrixToImageWriter.toBufferedImage(bitMatrix, DEFAULT_CONFIG);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return base64String;
        }
    
        /**
         * 生成二维码
         * 直接生成图片文件
         * @param url
         * @param path
         * @param fileName
         * @return
         */
        public static String createQrCodeFile(String url, String path, String fileName) {
            try {
                Map<EncodeHintType, String> hints = new HashMap<>(4);
                hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
                File file = new File(path, fileName);
                boolean isTrue = file.exists() || ((file.getParentFile().exists() || file.getParentFile().mkdirs()) && file.createNewFile());
                if (isTrue) {
                    writeToFile(bitMatrix, "jpg", file);
                    System.out.println("成功:" + file);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
            BufferedImage image = toBufferedImage(matrix);
            if (!ImageIO.write(image, format, file)) {
                throw new IOException("Could not write an image of format " + format + " to " + file);
            }
        }
    
        static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
            BufferedImage image = toBufferedImage(matrix);
            if (!ImageIO.write(image, format, stream)) {
                throw new IOException("Could not write an image of format " + format);
            }
        }
    
        private static BufferedImage toBufferedImage(BitMatrix matrix) {
            int width = matrix.getWidth();
            int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE);
                }
            }
            return image;
        }
    
        public static void main(String[] args) {
            //生成链接
            String url = "https://www.baidu.com";
            //配置生成路径
            String path = "C:users/photo/";
            //生成文件名称
            String fileName = "二维码.png";
            String qrCodeBase64 = createQrCodeBase64(url);
            System.out.println(qrCodeBase64);
    
        }
    }
    

    相关文章

      网友评论

          本文标题:二维码生成工具类

          本文链接:https://www.haomeiwen.com/subject/fotinctx.html