-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleQRcode.java
More file actions
52 lines (46 loc) · 1.67 KB
/
ConsoleQRcode.java
File metadata and controls
52 lines (46 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.util.HashMap;
/**
* @Author 薄荷你玩
* @Date 2023/04/16
* @Website www.bhshare.cn
*/
public class ConsoleQRcode {
/**
* 打印二维码 -> console
* @param content 二维码内容
*/
public static void printQRcode(String content) {
int width = 1; // 二维码宽度
int height = 1; // 二维码高度
// 定义二维码的参数
HashMap<EncodeHintType, java.io.Serializable> hints = new HashMap<EncodeHintType, java.io.Serializable>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//编码方式
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//纠错等级
// 打印二维码
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
for (int j = 0; j < bitMatrix.getHeight(); j++) {
for (int i = 0; i < bitMatrix.getWidth(); i++) {
if (bitMatrix.get(i, j)) {
System.out.print("■");
} else {
System.out.print(" ");
}
}
System.out.println();
}
} catch (WriterException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
String str = "http://www.bhshare.cn";
printQRcode(str);
}
}