一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 利用Java生成帶有文字的二維碼

利用Java生成帶有文字的二維碼

2020-06-19 11:41daisy JAVA教程

二維碼在我們現在的生活中可謂是隨處可見,這篇文章主要是介紹如何利用Java生成帶有文字的二維碼,對大家學習Java具有一定的參考借鑒價值。有需要的朋友們下面來一起看看吧。

介紹

主要使用了goole的zxing包,下面給出了示例代碼,很方便大家的理解和學習,代碼都屬于初步框架,功能有了,需要根據實際使用情況完善優化。

第一步、maven導入zxing

?
1
2
3
4
5
<dependency>
 <groupId>com.google.zxing</groupId>
 <artifactId>core</artifactId>
 <version>3.2.1</version>
</dependency>

第二步、開始生成二維碼

?
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
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
/**
 * 把生成的二維碼存入到圖片
 * @param matrix zxing包下的二維碼類
 * @return
 */
public static BufferedImage toBufferedImage(BitMatrix matrix) {
 int width = matrix.getWidth();
 int height = matrix.getHeight();
 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
 for (int x = 0; x < width; x++) {
  for (int y = 0; y < height; y++) {
   image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
  }
 }
 return image;
}
/**
 * 生成二維碼并寫入文件
 * @param content 掃描二維碼的內容
 * @param format 圖片格式 jpg
 * @param file  文件
 * @throws Exception
 */
public static void writeToFile(String content, String format, File file)
  throws Exception {
 MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
 @SuppressWarnings("rawtypes")
 Map hints = new HashMap();
 //設置UTF-8, 防止中文亂碼
 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
 //設置二維碼四周白色區域的大小
 hints.put(EncodeHintType.MARGIN,1);
 //設置二維碼的容錯性
 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
 //畫二維碼
 BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
 BufferedImage image = toBufferedImage(bitMatrix);
 if (!ImageIO.write(image, format, file)) {
  throw new IOException("Could not write an image of format " + format + " to " + file);
 }
}

第三步、把文字寫入二維碼圖片中:

?
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
/**
 * 給二維碼圖片加上文字
 * @param pressText 文字
 * @param qrFile  二維碼文件
 * @param fontStyle
 * @param color
 * @param fontSize
 */
public static void pressText(String pressText, File qrFile, int fontStyle, Color color, int fontSize) throws Exception {
 pressText = new String(pressText.getBytes(), "utf-8");
 Image src = ImageIO.read(qrFile);
 int imageW = src.getWidth(null);
 int imageH = src.getHeight(null);
 BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB);
 Graphics g = image.createGraphics();
 g.drawImage(src, 0, 0, imageW, imageH, null);
 //設置畫筆的顏色
 g.setColor(color);
 //設置字體
 Font font = new Font("宋體", fontStyle, fontSize);
 FontMetrics metrics = g.getFontMetrics(font);
 //文字在圖片中的坐標 這里設置在中間
 int startX = (WIDTH - metrics.stringWidth(pressText)) / 2;
 int startY = HEIGHT/2;
 g.setFont(font);
 g.drawString(pressText, startX, startY);
 g.dispose();
 FileOutputStream out = new FileOutputStream(qrFile);
 ImageIO.write(image, "JPEG", out);
 out.close();
 System.out.println("image press success");
}

第四步、在main方法中測試一下,一個中間帶文字的二維碼就生成了

?
1
2
3
4
5
public static void main(String[] args) throws Exception {
 File qrcFile = new File("/Users/deweixu/","google.jpg");
 writeToFile("www.google.com.hk", "jpg", qrcFile);
 pressText("谷歌", qrcFile, 5, Color.RED, 32);
}

總結

以上就是這篇文章《利用Java生成帶有文字的二維碼》的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 经典千人斩一区二区视频 | 日韩亚洲欧美一区二区三区 | 欧美在线高清 | 太深了 太粗h1v1 | sxx免费看观看美女 sss亚洲国产欧美一区二区 | 红杏网| 青青热久久综合网伊人 | 日韩风月片 | 国产极品久久 | a看片 | 日韩毛片免费 | 亚洲高清免费在线观看 | 成人网免费视频 | 欧洲第一页 | 久久精品中文字幕 | 精品国产麻豆AV无码 | 久久久影院亚洲精品 | 香蕉精品国产高清自在自线 | 亚洲AV蜜桃永久无码精品无码网 | 99久久国产综合精品网成人影院 | 国产探花视频在线观看 | 欧美一区精品二区三区 | 脱jk裙的美女露小内内无遮挡 | 蜜月aⅴ免费一区二区三区 蜜桃影像传媒推广 | 农村老少伦小说 | 国内精品91最新在线观看 | 日韩成人影视 | 精品国产精品人妻久久无码五月天 | 精品久久久久久久国产潘金莲 | 国产大片线上免费观看 | 91手机看片国产永久免费 | 日日爽日日操 | 超兴奋朋友的中文字幕下 | 帅小伙和警官同性3p | 亚洲精品久久玖玖玖玖 | 暖暖的视频完整视频韩国免费 | 国产免费久久精品 | 精品国产乱码久久久久久软件 | 国产99热99 | bl动漫在线观看 | 西施打开双腿下面好紧 |