美文网首页
生成二维码+base64格式返回

生成二维码+base64格式返回

作者: 冷枫abc | 来源:发表于2019-07-16 14:31 被阅读0次
    package net.parim.spark.common.utils;
    
    import java.awt.image.BufferedImage;
    
    import java.util.HashMap;
    
    import java.util.Map;
    
    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;
    
    public class QRCodeUtils {
    
    private static final MatrixToImageConfig DEFAULT_CONFIG = new MatrixToImageConfig();
    
    /**
    
    * 生成二维码
    
    *
    
    * @param content
    
    * @throws Exception
    
    */
    
    public static BufferedImage getEncode(String content) throws Exception {
    
    int width = 300; // 图像宽度
    
    int height = 300; // 图像高度
    
    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
    
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    
    BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
    
    return MatrixToImageWriter.toBufferedImage(bitMatrix, DEFAULT_CONFIG);
    
    }
    
    }
    

    此文代表以流的形式返回 不生成二维码到本地文件

    image

    Springboot中需要生成二维码

    首先,在pom.xml中添加依赖的包:

    com.google.zxingjavase3.2.1com.google.zxingcore3.2.1

    直接上代码:

    packagecom.chess.utils;importcom.google.zxing.*;importcom.google.zxing.client.j2se.BufferedImageLuminanceSource;importcom.google.zxing.client.j2se.MatrixToImageWriter;importcom.google.zxing.common.BitMatrix;importcom.google.zxing.common.HybridBinarizer;import org.apache.commons.io.FilenameUtils;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Configuration;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.io.IOException;import java.nio.file.Path;import java.nio.file.Paths;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;public class FilepathUtils {/**
    
      * 解析二维码解析
    
      */public static String analyzeEncode(String path) {        String content = null;BufferedImage image;try {            image = ImageIO.read(new File(path));LuminanceSource source = new BufferedImageLuminanceSource(image);Binarizer binarizer = new HybridBinarizer(source);BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);Map hints = new HashMap();hints.put(DecodeHintType.CHARACTER_SET,"UTF-8");Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码System.out.println("图片中内容:  ");System.out.println("author: "+ result.getText());content = result.getText();} catch (IOException e) {            e.printStackTrace();} catch (NotFoundException e) {            e.printStackTrace();}        return content;}/**
    
      * 生成二维码
    
      *
    
      * @param content
    
      * @throws Exception
    
      */public static String getEncode(String content) throws Exception {        String mkdir ="D://qRCode/";File f = new File(mkdir);if (!f.exists())            f.mkdirs();String string = UUIDUtils.getQRCodePath();String path = mkdir + string +".png";int width =300; // 图像宽度int height =300; // 图像高度String format ="png";// 图像类型Map hints = new HashMap();hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");BitMatrix bitMatrix = new MultiFormatWriter().encode(content,                BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵Path path1 = Paths.get(PATH + path);MatrixToImageWriter.writeToPath(bitMatrix, format, path1);// 输出图像return path;}    public static void main(String[] args) throws Exception {        String path = getEncode("我是小球小球---");System.out.println(path);}}
    
    当然了,我们把jar包下载下来直接在普通项目中使用也是可以的,只需要把我们的jar包上传到项目中的就行了!!! 
    
    其中的UUIDUtils代码如下:
    
    packagecom.chess.utils;importjava.util.Date;importjava.util.UUID;/** * 关于UUID的工具类 * * @author球球 */publicclassUUIDUtils{/**    * 随机生成一个ID    * RandomUUID+时间戳    *    * @return*/publicstaticStringrandomId() {returnUUID.randomUUID().toString().replaceAll("-","") +"-"+newDate().getTime();    }/**    * 生成二维码所在路径    * @return*/publicstaticStringgetQRCodePath(){returnUUID.randomUUID().toString().replaceAll("-","") +"-"+newDate().getTime();    }}
    

    为什么要使用UUIDUtils:

    在实际的项目开发过程中,我们可能会遇到要生成很多二维码的情况,所以为了保证二维码的唯一性,使用了UUID!!!

    https://blog.csdn.net/qiuqiu1628480502/article/details/80759102

    相关文章

      网友评论

          本文标题:生成二维码+base64格式返回

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