Java生成二维码

Java生成二维码,第1张

Java生成二维码

pom.xml

    
        com.google.zxing
        core
        3.3.0
    
    
        com.google.zxing
        javase
        3.3.0
    
    
        org.springframework
        spring-test
        5.1.6.RELEASE
    

工具类
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class QRCodeGenerator {

public static String generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
    QRCodeWriter qrCodeWriter = new QRCodeWriter();

    BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);

    Path path = FileSystems.getDefault().getPath(filePath);

    MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    return filePath;
}

}

main方法测试

public static void main(String[] args) {

    String filePath = LimsConfig.getUploadPath();
    
    String url = null;
    try {
        url = QRCodeGenerator.generateQRCodeImage("二维码中扫描出来的文字", 350, 350, UUID.randomUUID().toString().replaceAll("-", "")+".png");
        File file=new File(url);
        FileInputStream fileInputStream = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
        String fileName = FileUploadUtils.upload(filePath, multipartFile);
        System.out.println("写入磁盘返回的路径"+fileName);
    } catch (WriterException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5482223.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存