|
1
2
3
4
|
package com.huaheng.common.utils;
import com.google.zxing.BarcodeFormat;
|
|
5
|
import com.google.zxing.EncodeHintType;
|
|
6
7
8
9
|
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
|
|
10
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
11
12
13
|
import java.io.ByteArrayOutputStream;
import java.io.IOException;
|
|
14
15
|
import java.util.HashMap;
import java.util.Map;
|
|
16
17
18
19
20
21
22
23
24
25
|
/**
* 生成二维码
* @author Enzo Cotter
* @date 2020/2/21
*/
public class QRCodeGenerator {
public static byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
|
|
26
27
28
29
30
31
32
|
Map<EncodeHintType, Object> hints = new HashMap<>(3);
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 0);
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints);
|
|
33
34
35
36
37
38
39
40
|
ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
byte[] pngData = pngOutputStream.toByteArray();
return pngData;
}
}
|