7 changed files with 121 additions and 21 deletions
@ -0,0 +1,45 @@ |
|||
package com.zdxt.knowledge.utils; |
|||
|
|||
import java.io.ByteArrayOutputStream; |
|||
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 QRCodeGeneratorUtil { |
|||
|
|||
/* 高度——height, |
|||
宽度——width, |
|||
二维码内容——text(url需要自动跳转前面加上https://)
|
|||
背景图片的路径——filePath*/ |
|||
public static void 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); |
|||
|
|||
} |
|||
/* 高度——height, |
|||
宽度——width, |
|||
二维码内容——text(url需要自动跳转前面加上https://)
|
|||
*/ |
|||
public static byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException { |
|||
QRCodeWriter qrCodeWriter = new QRCodeWriter(); |
|||
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); |
|||
|
|||
ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream(); |
|||
MatrixToImageWriter.writeToStream(bitMatrix, "JPG", pngOutputStream); |
|||
byte[] pngData = pngOutputStream.toByteArray(); |
|||
return pngData; |
|||
} |
|||
|
|||
} |
|||
|
|||
Loading…
Reference in new issue