31 lines
1.1 KiB
Java
31 lines
1.1 KiB
Java
package vvpkassistant.Tools;
|
|
|
|
import com.google.zxing.BarcodeFormat;
|
|
import com.google.zxing.EncodeHintType;
|
|
import com.google.zxing.MultiFormatWriter;
|
|
import com.google.zxing.WriterException;
|
|
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
|
import com.google.zxing.common.BitMatrix;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.util.Base64;
|
|
import java.util.Hashtable;
|
|
|
|
public class QRCodeUtil {
|
|
|
|
public static String generateQRCode(String content, int width, int height)
|
|
throws WriterException, IOException {
|
|
|
|
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
|
|
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
|
|
|
BitMatrix matrix = new MultiFormatWriter().encode(
|
|
content, BarcodeFormat.QR_CODE, width, height, hints);
|
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
MatrixToImageWriter.writeToStream(matrix, "PNG", outputStream);
|
|
|
|
return Base64.getEncoder().encodeToString(outputStream.toByteArray());
|
|
}
|
|
} |