自定义生成二维码,解析指定内容编码格式的二维码。

  1. 引入依赖
     <!--二维码解析-->
     <dependency>
         <groupId>com.google.zxing</groupId>
         <artifactId>core</artifactId>
         <version>3.3.3</version>
     </dependency>
     <dependency>
         <groupId>com.google.zxing</groupId>
         <artifactId>javase</artifactId>
         <version>3.3.3</version>
     </dependency>
  1. 编写工具类
    其中包含了 生成二维码解析上传二维码内容(直解析utf-8和GBK编码的二维码)解析本地二维码内容,自定义编码格式
package com.meng.weixin.util;

import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;
import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
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.util.HashMap;

/**
 * 二维码的解析,生成
 * @author lenovo
 */
public class QRCodeUtil {

    /**
     * 生成二维码
     * @param contents 内容
     * @param format 二维码图片后缀
     * @param filepath //生成二维码路径(当包含二维码名称和后缀,忽略format)
     * @throws IOException
     * @throws NotFoundException
     */
    public static void getQRCode(String contents,String format,String filepath) throws IOException, NotFoundException {
        getQRCode(contents, format, filepath,"utf-8");
    }
    /**
     * 生成二维码
     * @param contents 内容
     * @param format 二维码图片后缀
     * @param filepath //生成二维码路径(当包含二维码名称和后缀,忽略format)
     * @param character 二维码内容编码方式
     * @throws IOException
     * @throws NotFoundException
     */
    public static void getQRCode(String contents,String format,String filepath,String character) throws IOException, NotFoundException {
       getQRCode(300,300,contents,format,filepath,character);
    }
    /**
     * 生成二维码
     * @param width 宽
     * @param height 高
     * @param contents 内容
     * @param format 二维码图片后缀
     * @param filepath //生成二维码路径(当包含二维码名称和后缀,忽略format)
     * @param character 二维码内容编码方式
     * @throws IOException
     * @throws NotFoundException
     */
    public static void getQRCode(int width,int height,String contents,String format,String filepath,String character) throws IOException, NotFoundException {
        HashMap map = new HashMap();
        map.put(EncodeHintType.CHARACTER_SET,character);
        map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        map.put(EncodeHintType.MARGIN, 0);
        try {
            BitMatrix bm = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height,map);
            Path file = new File(filepath).toPath();
            MatrixToImageWriter.writeToPath(bm, format, file);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 解析上传二维码内容(直解析utf-8和GBK编码的二维码)
     * @param QRImg 二维码图片
     * @throws IOException
     * @throws NotFoundException
     */
    public static String analysis(File QRImg) throws IOException, NotFoundException {
        BufferedImage image = ImageIO.read(QRImg);
        BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
        HashMap map = new HashMap();
        map.put(DecodeHintType.CHARACTER_SET, "utf-8");
        Result result = new MultiFormatReader().decode(bb, map);
        String QRContents=result.getText();
        if (result.getText().contains("�")){
            //乱码了,更换编码方式
            System.out.println("乱码了,更换编码格式(GBK)");
            map.put(DecodeHintType.CHARACTER_SET, "GBK");
            result = new MultiFormatReader().decode(bb, map);
            QRContents=result.getText();
        }
        System.out.println("二维码文本内容:"+QRContents);
        return QRContents;
    }
    /**
     * 解析本地二维码内容(直解析utf-8和GBK编码的二维码)
     * @param filePath 二维码图片地址
     * @throws IOException
     * @throws NotFoundException
     */
    public static String analysis(String filePath) throws IOException, NotFoundException {
        BufferedImage image = ImageIO.read(new File(filePath));
        BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
        HashMap map = new HashMap();
        map.put(DecodeHintType.CHARACTER_SET, "utf-8");
        Result result = new MultiFormatReader().decode(bb, map);
        String QRContents=result.getText();
        if (result.getText().contains("�")){
            //乱码了,更换编码方式
            System.out.println("乱码了,更换编码格式(GBK)");
            map.put(DecodeHintType.CHARACTER_SET, "GBK");
            result = new MultiFormatReader().decode(bb, map);
            QRContents=result.getText();
        }
        System.out.println("二维码文本内容:"+QRContents);
        return QRContents;
    }
    /**
     * 解析本地二维码内容,自定义编码格式
     * @param filePath 二维码图片地址
     * @param character 二维码内容编码格式
     * @throws IOException
     * @throws NotFoundException
     */
    public static String analysis(String filePath,String character) throws IOException, NotFoundException {
        BufferedImage image = ImageIO.read(new File(filePath));
        BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
        HashMap map = new HashMap();
        map.put(DecodeHintType.CHARACTER_SET,character);
        Result result = new MultiFormatReader().decode(bb, map);
        String QRContents=result.getText();
        return QRContents;
    }


}

  1. 测试
    写一个接口测试
/**
     * 解析上传的二维码
     * @param QRImg(勿用中文名称的二维码)
     * @return
     * @throws IOException
     * @throws NotFoundException
     */
    @PostMapping("/QRanalysis")
    public Result QRanalysis(@RequestParam("QRImg") MultipartFile QRImg) throws IOException, NotFoundException {
//        String decode = QrCodeUtil.decode();
        String analysis = QRCodeUtil.analysis(MultipartFileToFile(QRImg));
        return Result.ok(analysis);
    }
    
	/**
     * 将MultipartFile转换为File
     * @param multiFile
     * @return
     */
    public static File MultipartFileToFile(MultipartFile multiFile) {
        // 获取文件名
        String fileName = multiFile.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 若须要防止生成的临时文件重复,能够在文件名后添加随机码

        try {
            File file = File.createTempFile(fileName, prefix);
            multiFile.transferTo(file);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
  1. 结果
    在这里插入图片描述
    生成的二维码在这里:
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XuDream

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值