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

二维码生成工具类

作者: 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);

    }
}

相关文章

  • Android----生成二维码

    引用了 ...... 导入包名 生成二维码 使用代码 工具类

  • java生成二维码

    1.pom文件添加maven依赖: 2.生成二维码工具类:

  • 二维码生成,包含文本,网址,图片等

    二维码所引入的jar包 pom文件配置 二维码生成工具类,可自行修改

  • 项目常用工具类:二维码生成

    有些项目中需要用到二维码,这里我们编写一个二维码生成的工具类。 我们需要用到zxing这个二维码工具类,然后再封装...

  • Java工具类整理

    以下整理常用的java工具类。 工具类所需要的依赖以maven的形式给出 ZXingUtils 用于二维码的生成和...

  • 使用zxing生成二维码去除白边

    生成二维码的时候,我们通常使用google.zxing包,使用google提供的工具类生成二维码的时候通常会带有白...

  • 开发相关网站

    工具类 SQL 美化 二维码生成-草料网 Base64加密、解密 - 站长工具 在线JSON校验格式化工具(Be ...

  • 使用QR Code生成二维码

    一、引入qr code相关jar包 二、生成二维码Util 三、调用工具类encode方法绘制二维码

  • Java生成二维码

    1、在项目的pom.xml中添加依赖 2、编写二维码工具类 QRCodeUtil 3、生成图片二维码并保存

  • Java生成二维码

    1、添加依赖包 2、生成二维码图片的工具类 3、Controller 接口 4、Services 5、最后效果图 ...

网友评论

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

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