java实现二维码的生成和内容读取

依赖:


        
            com.google.zxing
            core
            3.3.3
        
        
        
            com.google.zxing
            javase
            3.3.3
        

代码:

package com.demo.demo.QRcode;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
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.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author xxs
 * @Date 2020/6/10 11:05
 */
@Controller
public class QRCodeController {

    @RequestMapping("QRCode")
    @ResponseBody
    public String QRCode(HttpServletRequest req){
        final int width = 300;
        final int height = 300;
        final String format = "png";
        final String content = "我爱你,中国!!!";
        //定义二维码的参数
        HashMap hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 指定编码格式
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 指定纠错等级
        hints.put(EncodeHintType.MARGIN, 2);//设置白边
        //生成二维码
        try{
            //编码的内容,编码的方式(二维码、条形码...),首选的宽度,首选的高度,编码时的额外参数
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            Path file = new File("F:\\ideaWorkSpace\\demo\\src\\main\\resources\\static\\img.png").toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        }catch(Exception e){

        }
        return "success";
    }

    @RequestMapping("getQRCode")
    @ResponseBody
    public String getQRCode() throws Exception {
        MultiFormatReader formatReader = new MultiFormatReader();
        File file = new File("F:\\ideaWorkSpace\\demo\\src\\main\\resources\\static\\img.png");
        BufferedImage image = ImageIO.read(file);
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
        //定义二维码的参数
        HashMap hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 指定编码格式
        Result result = formatReader.decode(binaryBitmap, hints);
        System.out.println("二维码解析结果:" + result.toString());
        System.out.println("二维码的格式:" + result.getBarcodeFormat());
        System.out.println("二维码的文本内容:" + result.getText());
        return result.getText();
    }
}

生成二维码测试:

java实现二维码的生成和内容读取_第1张图片

java实现二维码的生成和内容读取_第2张图片

java实现二维码的生成和内容读取_第3张图片

 

读取二维码测试:

java实现二维码的生成和内容读取_第4张图片

 

java实现二维码的生成和内容读取_第5张图片

 

你可能感兴趣的:(java)