为数据列表的每条记录生成对应的二维码

效果图:
为数据列表的每条记录生成对应的二维码_第1张图片

一、前端




    


     
    
    

二、后端


        
        
            com.google.zxing
            core
            3.4.1
        
        
            com.google.zxing
            javase
            3.4.1
        
@Controller
@RequestMapping("/biz/gdzcxx")
public class BizGdzcxxController extends BaseController
{
    private String prefix = "biz/gdzcxx";
    /**
     * 创建二维码
     */
    @RequiresPermissions("biz:gdzcxx:createQRCode")
    @PostMapping("/createQRCode")
    public String createQRCode(String gdzcxxid) throws IOException, WriterException {
        
        //根据ID取记录,并为其生成二维码
        BizGdzcxx bizGdzcxx = bizGdzcxxService.selectBizGdzcxxByGdzcxxId(Long.valueOf(gdzcxxid));

        QRCode qrCode = new QRCode();

        //二维码物理存放路径
        String uploadPath = "D:/qrcode";
        qrCode.setFilePath(uploadPath + "/ewm/gdzcxx/" + bizGdzcxx.getZcmc() + ".png");
        
        //二维码内容:http://localhost:8088/prefix/info/1
        qrCode.setQrCodeData("/" + prefix + "/info/" + bizGdzcxx.getGdzcxxId());
        
        //创建二维码
        qrCodeService.createQRCode(qrCode);

        //将二维码转二进制
        FileInputStream fis = new FileInputStream(qrCode.getFilePath());
        byte[] bytes = FileCopyUtils.copyToByteArray(fis);

        //二进制入库
        bizGdzcxx.setEwm(bytes);
        bizGdzcxxService.updateBizGdzcxx(bizGdzcxx);
        
        return prefix + "/gdzcewm";
    }

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.chenf.biz.domain.QRCode;
import com.chenf.biz.service.IQRCodeService;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

@Service
public class QRCodeServiceImpl implements IQRCodeService {
    @Override
    public void createQRCode(QRCode qrCode) throws IOException, WriterException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();

        Map hintMap = new HashMap<>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        hintMap.put(EncodeHintType.CHARACTER_SET, qrCode.getCharset());

        BitMatrix matrix = qrCodeWriter.encode(new String(qrCode.getQrCodeData().getBytes(qrCode.getCharset()), qrCode.getCharset()),
        //BitMatrix matrix = qrCodeWriter.encode(qrCode.getQrCodeData(),
                BarcodeFormat.QR_CODE, qrCode.getQrCodeWidth(), qrCode.getQrCodeHeight(), hintMap);
        Path path = Paths.get(qrCode.getFilePath());
        if (!Files.exists(path)) {
            try {
                Files.createDirectories(path);
                System.out.println("Path created!");
            } catch (Exception e) {
                System.out.println("An error occurred while creating the path: " + e.getMessage());
            }
        } else {
            System.out.println("Path already exists!");
        }
        MatrixToImageWriter.writeToPath(matrix, qrCode.getFilePath().substring(qrCode.getFilePath().lastIndexOf('.') + 1), path);
    }
}
import com.google.zxing.WriterException;
import com.chenf.biz.domain.QRCode;

import java.io.IOException;

public interface IQRCodeService {
    void createQRCode(QRCode qrCode) throws IOException, WriterException;
}

/**
 * 二维码
 */
public class QRCode {
    //二维码内容
    private String qrCodeData;
    //二维码存放路径
    private String filePath;
    private String charset="UTF-8";
    //二维码调度
    private int qrCodeHeight=200;
    //二维码宽度
    private int qrCodeWidth=200;

    public String getQrCodeData(){return this.qrCodeData;}
    public void setQrCodeData(String qrCodeData){ this.qrCodeData = qrCodeData;}
    public String getFilePath(){return this.filePath;}
    public void setFilePath(String filePath){ this.filePath = filePath;}
    public String getCharset(){return this.charset;}
    public void setCharset(String charset){ this.charset = charset;}
    public int getQrCodeHeight(){return this.qrCodeHeight;}
    public void setQrCodeHeight(int qrCodeHeight){ this.qrCodeHeight = qrCodeHeight;}
    public int getQrCodeWidth(){return this.qrCodeWidth;}
    public void setQrCodeWidth(int qrCodeWidth){ this.qrCodeWidth = qrCodeWidth;}
}

《完结》

你可能感兴趣的:(JAVA,sprint,java,二维码,thymeleaf,mysql,maven)