栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java生成二维码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/490096.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号