激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - java中使用zxing批量生成二維碼立牌

java中使用zxing批量生成二維碼立牌

2020-07-14 17:45Mr_Smile2014 JAVA教程

本篇文章主要介紹了java中使用zxing批量生成二維碼立牌,非常具有實用價值,需要的朋友可以參考下。

使用zxing批量在做好的立牌背景圖的指定位置上,把指定的文本內容(鏈接地址、文本等)生成二維碼并放在該位置,最后加上立牌編號。

步驟:

1).做好背景圖,如下圖:

java中使用zxing批量生成二維碼立牌

2).生成二維碼BufferedImage對象。代碼如下:

?
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
/**
  *
  * @Title: toBufferedImage
  * @Description: 把文本轉化成二維碼圖片對象
  * @param text
  *   二維碼內容
  * @param width
  *   二維碼高度
  * @param height
  *   二位寬度
  * @param
  * @param Exception
  *   設定文件
  * @return BufferedImage 返回類型
  * @throws
  */
 public static BufferedImage toBufferedImage(String text, int width,
   int height) throws Exception {
  int BLACK = 0xFF000000;
  int WHITE = 0xFFFFFFFF;
  Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
  hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 內容所使用字符集編碼
  hints.put(EncodeHintType.MARGIN, 1);
  BitMatrix matrix = new MultiFormatWriter().encode(text,
    BarcodeFormat.QR_CODE, width, height, hints);
  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;
 }

3).在立牌背景圖的指定位置上生成二維碼,代碼如下:

?
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
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
  *
  * @Title: markImageByCode
  * @Description: 向圖片指定位置增加二維碼
  * @param img
  *   二維碼image對象
  * @param srcImgPath
  *   背景圖
  * @param targerPath
  *   目標圖
  * @param positionWidth
  *   位置橫坐標
  * @param positionHeight
  *   位置縱坐標
  * @return void 返回類型
  * @throws
  */
 public static void markImageByCode(Image img, String srcImgPath,
   String targerPath, int positionWidth, int positionHeight) {
  OutputStream os = null;
  try {
 
   Image srcImg = ImageIO.read(new File(srcImgPath));
 
   BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
     srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
 
   // 1、得到畫筆對象
   Graphics2D g = buffImg.createGraphics();
 
   // 2、設置對線段的鋸齒狀邊緣處理
   g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
     RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 
   g.drawImage(
     srcImg.getScaledInstance(srcImg.getWidth(null),
       srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,
     null);
 
   g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
     alpha));
 
   // 3、二維碼位置
   g.drawImage(img, positionWidth, positionHeight, null);
   g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
   // 4、釋放資源
   g.dispose();
 
   // 5、生成圖片(建議生成PNG的,jpg會失真)
   os = new FileOutputStream(targerPath);
   ImageIO.write(buffImg, "PNG", os);
 
   System.out.println("二維碼圖片生成成功");
 
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    if (null != os)
     os.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }

4).在立牌上加上立牌編號

?
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
/**
  *
  * @Title: pressText
  * @Description:向圖片指定位置加上文字
  * @param pressText
  *   文字內容
  * @param srcImageFile
  *   原圖片
  * @param destImageFile
  *   目標圖片
  * @param x
  *   橫坐標
  * @param y
  *   縱坐標
  * @param alpha
  *   透明度
  * @return void 返回類型
  * @throws
  */
 public final static void pressText(String pressText, String srcImageFile,
   String destImageFile, int x, int y, float alpha) {
  try {
   File img = new File(srcImageFile);
   Image src = ImageIO.read(img);
   int width = src.getWidth(null);
   int height = src.getHeight(null);
   BufferedImage image = new BufferedImage(width, height,
     BufferedImage.TYPE_INT_RGB);
   Graphics2D g = image.createGraphics();
   // 開文字抗鋸齒 去文字毛刺
   g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
     RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
   g.drawImage(src, 0, 0, width, height, null);
   // 設置顏色
   g.setColor(new Color(89, 87, 87));
   // 設置 Font
   g.setFont(new Font("方正蘭亭中黑_GBK", Font.BOLD, 14));
   g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
     alpha));
   // 第一參數->設置的內容,后面兩個參數->文字在圖片上的坐標位置(x,y) .
   g.drawString(pressText, x, y);
   g.dispose();
   ImageIO.write((BufferedImage) image, "PNG", new File(destImageFile));// 輸出到文件流
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

示例:

代碼:

測試代碼

?
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
public class codeTest {
 public static void main(String[] args) throws Exception {
  String text = "http://www.xxx.com/"; // 二維碼內容
 
  // 生成二維碼
  //生成圖片二維碼存放目錄
  String targetPath = "f:/qrcode/targetimg/" + Utils.toStr();
  //創建目錄
  Utils.makeDirs(targetPath);
   
  int begin = 100;//code 開始數字
  int end = 101;//code結束數字
  for (int i = begin; i <= end; i++) {
   //生成含日期的16位數字如20161214000001
   String code = Utils.toStr() + Utils.formateNumber(i);
   //獲取二維碼對象
   BufferedImage image = Utils.toBufferedImage(text
     + "?payCode=" + code,240,240);
   //生成含背景圖+二維碼的立牌的圖
   Utils.markImageByCode(image, "f:/qrcode/srcimg/src.png",
     targetPath + "/" + code + ".png", 340, 160);
   //立牌的圖加上code編號
   Utils.pressText(code, targetPath + "/" + code + ".png", targetPath
     + "/" + code + ".png", 390, 417, 0.5f);
  }
  // 生成二維碼
 }
}

效果:

批量生成的圖片效果圖如下

java中使用zxing批量生成二維碼立牌

批量圖:

java中使用zxing批量生成二維碼立牌

utils代碼:

?
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package cn.utils.code;
 
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
 
import javax.imageio.ImageIO;
 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
 
/** 工具類. */
public abstract class Utils {
 
 /** 日期格式:yyyy-MM-dd HH:mm:ss */
 public static String DF_DATETIME = "yyyyMMdd";
 private static float alpha = 1f;
 
 /**
  *
  * @Title: toBufferedImage
  * @Description: 把文本轉化成二維碼圖片對象
  * @param text
  *   二維碼內容
  * @param width
  *   二維碼高度
  * @param height
  *   二位寬度
  * @param
  * @param Exception
  *   設定文件
  * @return BufferedImage 返回類型
  * @throws
  */
 public static BufferedImage toBufferedImage(String text, int width,
   int height) throws Exception {
  int BLACK = 0xFF000000;
  int WHITE = 0xFFFFFFFF;
  Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
  hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 內容所使用字符集編碼
  hints.put(EncodeHintType.MARGIN, 1);
  BitMatrix matrix = new MultiFormatWriter().encode(text,
    BarcodeFormat.QR_CODE, width, height, hints);
  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;
 }
 
 /**
  *
  * @Title: markImageByCode
  * @Description: 向圖片指定位置增加二維碼
  * @param img
  *   二維碼image對象
  * @param srcImgPath
  *   背景圖
  * @param targerPath
  *   目標圖
  * @param positionWidth
  *   位置橫坐標
  * @param positionHeight
  *   位置縱坐標
  * @return void 返回類型
  * @throws
  */
 public static void markImageByCode(Image img, String srcImgPath,
   String targerPath, int positionWidth, int positionHeight) {
  OutputStream os = null;
  try {
 
   Image srcImg = ImageIO.read(new File(srcImgPath));
 
   BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
     srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
 
   // 1、得到畫筆對象
   Graphics2D g = buffImg.createGraphics();
 
   // 2、設置對線段的鋸齒狀邊緣處理
   g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
     RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 
   g.drawImage(
     srcImg.getScaledInstance(srcImg.getWidth(null),
       srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,
     null);
 
   g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
     alpha));
 
   // 3、二維碼位置
   g.drawImage(img, positionWidth, positionHeight, null);
   g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
   // 4、釋放資源
   g.dispose();
 
   // 5、生成圖片(建議生成PNG的,jpg會失真)
   os = new FileOutputStream(targerPath);
   ImageIO.write(buffImg, "PNG", os);
 
   System.out.println("二維碼圖片生成成功");
 
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    if (null != os)
     os.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }
 
 /**
  *
  * @Title: pressText
  * @Description:向圖片指定位置加上文字
  * @param pressText
  *   文字內容
  * @param srcImageFile
  *   原圖片
  * @param destImageFile
  *   目標圖片
  * @param x
  *   橫坐標
  * @param y
  *   縱坐標
  * @param alpha
  *   透明度
  * @return void 返回類型
  * @throws
  */
 public final static void pressText(String pressText, String srcImageFile,
   String destImageFile, int x, int y, float alpha) {
  try {
   File img = new File(srcImageFile);
   Image src = ImageIO.read(img);
   int width = src.getWidth(null);
   int height = src.getHeight(null);
   BufferedImage image = new BufferedImage(width, height,
     BufferedImage.TYPE_INT_RGB);
   Graphics2D g = image.createGraphics();
   // 開文字抗鋸齒 去文字毛刺
   g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
     RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
   g.drawImage(src, 0, 0, width, height, null);
   // 設置顏色
   g.setColor(new Color(89, 87, 87));
   // 設置 Font
   g.setFont(new Font("方正蘭亭中黑_GBK", Font.BOLD, 14));
   g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
     alpha));
   // 第一參數->設置的內容,后面兩個參數->文字在圖片上的坐標位置(x,y) .
   g.drawString(pressText, x, y);
   g.dispose();
   ImageIO.write((BufferedImage) image, "PNG", new File(destImageFile));// 輸出到文件流
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 // 日期轉字符串
 
 /** 將日期格式化為String,默認格式為yyyy-MM-dd HH:mm:ss,默認日期為當前日期. */
 public static String toStr() {
  return toStr(DF_DATETIME);
 }
 
 /** 將日期格式化為String,格式由參數format指定,默認日期為當前日期,format值可使用本類常量或自定義. */
 public static String toStr(String format) {
  return toStr(format, new Date());
 }
 
 /** 將日期格式化為String,默認格式為yyyy-MM-dd HH:mm:ss,日期由參數date指定. */
 public static String toStr(Date date) {
  return toStr(DF_DATETIME, date);
 }
 
 /** 將日期格式化為String,格式由參數format指定,日期由參數date指定,format值可使用本類常量或自定義. */
 public static String toStr(String format, Date date) {
  return new SimpleDateFormat(format).format(date);
 }
 
 public static String formateNumber(int num) {
  DecimalFormat df = new DecimalFormat("000000");
  String str2 = df.format(num);
  return str2;
 }
 
 public static boolean makeDirs(String filePath) {
 
  File folder = new File(filePath);
  return (folder.exists() && folder.isDirectory()) ? true : folder
    .mkdirs();
 }
 
}

使用的技術:

1.使用的zxing生成二維碼工具。

1)下載地址:http://repo1.maven.org/maven2/com/google/zxing/javase/3.1.0/

2).maven配置

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/mr_smile2014/article/details/53641304

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 作爱在线观看 | 国产精品久久久久久久久久大牛 | 久精品国产 | 破处av在线 | 欧美激情精品久久久久久黑人 | 成人网在线观看 | 国产高潮好爽好大受不了了 | 欧美a级在线免费观看 | 欧美成人精品一区 | 一级毛片在线免费观看 | 大学生一级毛片在线视频 | 吾色视频| 免费看国产 | 性高跟鞋xxxxhd4kvideos | 羞羞视频免费网站含羞草 | 中文字幕在线亚洲精品 | 国外成人在线视频网站 | 成人h精品动漫一区二区三区 | 毛片a级毛片免费播放100 | 精品成人av一区二区三区 | 精品小视频 | 久久久一区二区三区精品 | 一级在线 | freexxxhd喷水| 国产成年人在线观看 | 美女一级视频 | 久久久国产精品网站 | 国产韩国精品一区二区三区久久 | 欧美a级一区二区 | 精品久久久久久综合日本 | 羞羞视频入口 | 欧美日韩亚洲国产精品 | 色中色在线视频 | 国产成人免费高清激情视频 | 久久精品视频网址 | 一级黄色免费观看 | 久草视频手机在线观看 | 一级大片久久 | 亚洲成人欧美在线 | 经典三级在线视频 | 久草视频手机在线观看 |