pom.xml
com.google.zxing core3.3.0 com.google.zxing javase3.3.0 org.springframework spring-test5.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(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)