Java语言实现二维码的生成
众所周知,现在生活中二维码已经是无处不见。走在街道上,随处可见广告标语旁有二维码,手机上QQ,微信加个好友都能通过二维码的方式,我不知道是什么时候兴起的二维码浪潮,但是我知道,这在我小时候可是见不到的。小小的一个方形图片,却能存储着大量的信息,是不是不可思议。当然路边的二维码希望大家还是不要贪小便宜,随便扫二维码很可能给你手机内置了病毒,导致个人信息的泄露,切记切记!
话不多说,直接上代码。
生成带logo的二维码代码:
1 package com.lsd.barcode;
2
3 import java.awt.BasicStroke;
4 import java.awt.Graphics;
5 import java.awt.Graphics2D;
6 import java.awt.Image;
7 import java.awt.Shape;
8 import java.awt.geom.RoundRectangle2D;
9 import java.awt.image.BufferedImage;
10 import java.io.File;
11 import java.io.OutputStream;
12 import java.util.Hashtable;
13 import java.util.Random;
14 import javax.imageio.ImageIO;
15 import com.google.zxing.BarcodeFormat;
16 import com.google.zxing.BinaryBitmap;
17 import com.google.zxing.DecodeHintType;
18 import com.google.zxing.EncodeHintType;
19 import com.google.zxing.MultiFormatReader;
20 import com.google.zxing.MultiFormatWriter;
21 import com.google.zxing.Result;
22 import com.google.zxing.common.BitMatrix;
23 import com.google.zxing.common.HybridBinarizer;
24 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
25
26 /**
27 * 二维码工具类
28 *
29 */
30 public class QRCodeUtil {
31
32 private static final String CHARSET = "utf-8";
33 private static final String FORMAT_NAME = "JPG";
34 // 二维码尺寸
35 private static final int QRCODE_SIZE = 300;
36 // LOGO宽度
37 private static final int WIDTH = 60;
38 // LOGO高度
39 private static final int HEIGHT = 60;
40
41 private static BufferedImage createImage(String content, String imgPath,
42 boolean needCompress) throws Exception {
43 Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
44 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
45 hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
46 hints.put(EncodeHintType.MARGIN, 1);
47 BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
48 BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
49 int width = bitMatrix.getWidth();
50 int height = bitMatrix.getHeight();
51 BufferedImage image = new BufferedImage(width, height,
52 BufferedImage.TYPE_INT_RGB);
53 for (int x = 0; x < width; x++) {
54 for (int y = 0; y < height; y++) {
55 image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
56 : 0xFFFFFFFF);
57 }
58 }
59 if (imgPath == null || "".equals(imgPath)) {
60 return image;
61 }
62 // 插入图片
63 QRCodeUtil.insertImage(image, imgPath, needCompress);
64 return image;
65 }
66
67 /**
68 * 插入LOGO
69 *
70 * @param source
71 * 二维码图片
72 * @param imgPath
73 * LOGO图片地址
74 * @param needCompress
75 * 是否压缩
76 * @throws Exception
77 */
78 private static void insertImage(BufferedImage source, String imgPath,
79 boolean needCompress) throws Exception {
80 File file = new File(imgPath);
81 if (!file.exists()) {
82 System.err.println(""+imgPath+" 该文件不存在!");
83 return;
84 }
85 Image src = ImageIO.read(new File(imgPath));
86 int width = src.getWidth(null);
87 int height = src.getHeight(null);
88 if (needCompress) { // 压缩LOGO
89 if (width > WIDTH) {
90 width = WIDTH;
91 }
92 if (height > HEIGHT) {
93 height = HEIGHT;
94 }
95 Image image = src.getScaledInstance(width, height,
96 Image.SCALE_SMOOTH);
97 BufferedImage tag = new BufferedImage(width, height,
98 BufferedImage.TYPE_INT_RGB);
99 Graphics g = tag.getGraphics();
100 g.drawImage(image, 0, 0, null); // 绘制缩小后的图
101 g.dispose();
102 src = image;
103 }
104 // 插入LOGO
105 Graphics2D graph = source.createGraphics();
106 int x = (QRCODE_SIZE - width) / 2;
107 int y = (QRCODE_SIZE - height) / 2;
108 graph.drawImage(src, x, y, width, height, null);
109 Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
110 graph.setStroke(new BasicStroke(3f));
111 graph.draw(shape);
112 graph.dispose();
113 }
114
115 /**
116 * 生成二维码(内嵌LOGO)
117 *
118 * @param content
119 * 内容
120 * @param imgPath
121 * LOGO地址
122 * @param destPath
123 * 存放目录
124 * @param needCompress
125 * 是否压缩LOGO
126 * @throws Exception
127 */
128 public static void encode(String content, String imgPath, String destPath,
129 boolean needCompress) throws Exception {
130 BufferedImage image = QRCodeUtil.createImage(content, imgPath,
131 needCompress);
132 mkdirs(destPath);
133 String file = new Random().nextInt(99999999)+".jpg";
134 ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));
135 }
136
137 /**
138 * 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
139 * @param destPath 存放目录
140 */
141 public static void mkdirs(String destPath) {
142 File file =new File(destPath);
143 //当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
144 if (!file.exists() && !file.isDirectory()) {
145 file.mkdirs();
146 }
147 }
148
149 /**
150 * 生成二维码(内嵌LOGO)
151 *
152 * @param content
153 * 内容
154 * @param imgPath
155 * LOGO地址
156 * @param destPath
157 * 存储地址
158 * @throws Exception
159 */
160 public static void encode(String content, String imgPath, String destPath)
161 throws Exception {
162 QRCodeUtil.encode(content, imgPath, destPath, false);
163 }
164
165 /**
166 * 生成二维码
167 *
168 * @param content
169 * 内容
170 * @param destPath
171 * 存储地址
172 * @param needCompress
173 * 是否压缩LOGO
174 * @throws Exception
175 */
176 public static void encode(String content, String destPath,
177 boolean needCompress) throws Exception {
178 QRCodeUtil.encode(content, null, destPath, needCompress);
179 }
180
181 /**
182 * 生成二维码
183 *
184 * @param content
185 * 内容
186 * @param destPath
187 * 存储地址
188 * @throws Exception
189 */
190 public static void encode(String content, String destPath) throws Exception {
191 QRCodeUtil.encode(content, null, destPath, false);
192 }
193
194 /**
195 * 生成二维码(内嵌LOGO)
196 *
197 * @param content
198 * 内容
199 * @param imgPath
200 * LOGO地址
201 * @param output
202 * 输出流
203 * @param needCompress
204 * 是否压缩LOGO
205 * @throws Exception
206 */
207 public static void encode(String content, String imgPath,
208 OutputStream output, boolean needCompress) throws Exception {
209 BufferedImage image = QRCodeUtil.createImage(content, imgPath,
210 needCompress);
211 ImageIO.write(image, FORMAT_NAME, output);
212 }
213
214 /**
215 * 生成二维码
216 *
217 * @param content
218 * 内容
219 * @param output
220 * 输出流
221 * @throws Exception
222 */
223 public static void encode(String content, OutputStream output)
224 throws Exception {
225 QRCodeUtil.encode(content, null, output, false);
226 }
227
228 /**
229 * 解析二维码
230 *
231 * @param file
232 * 二维码图片
233 * @return
234 * @throws Exception
235 */
236 public static String decode(File file) throws Exception {
237 BufferedImage image;
238 image = ImageIO.read(file);
239 if (image == null) {
240 return null;
241 }
242 BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(
243 image);
244 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
245 Result result;
246 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
247 hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
248 result = new MultiFormatReader().decode(bitmap, hints);
249 String resultStr = result.getText();
250 return resultStr;
251 }
252
253 /**
254 * 解析二维码
255 *
256 * @param path
257 * 二维码图片地址
258 * @return
259 * @throws Exception
260 */
261 public static String decode(String path) throws Exception {
262 return QRCodeUtil.decode(new File(path));
263 }
264
265 public static void createCode(String text,String picturepath,String location) throws Exception {
266 QRCodeUtil.encode(text, picturepath, location, true);
267 }
268 }
package com.lsd.barcode; import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import com.google.zxing.LuminanceSource; public class BufferedImageLuminanceSource extends LuminanceSource {
private final BufferedImage image;
private final int left;
private final int top; public BufferedImageLuminanceSource(BufferedImage image) {
this(image, 0, 0, image.getWidth(), image.getHeight());
} public BufferedImageLuminanceSource(BufferedImage image, int left,
int top, int width, int height) {
super(width, height); int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
if (left + width > sourceWidth || top + height > sourceHeight) {
throw new IllegalArgumentException(
"Crop rectangle does not fit within image data.");
} for (int y = top; y < top + height; y++) {
for (int x = left; x < left + width; x++) {
if ((image.getRGB(x, y) & 0xFF000000) == 0) {
image.setRGB(x, y, 0xFFFFFFFF); // = white
}
}
} this.image = new BufferedImage(sourceWidth, sourceHeight,
BufferedImage.TYPE_BYTE_GRAY);
this.image.getGraphics().drawImage(image, 0, 0, null);
this.left = left;
this.top = top;
} public byte[] getRow(int y, byte[] row) {
if (y < 0 || y >= getHeight()) {
throw new IllegalArgumentException(
"Requested row is outside the image: " + y);
}
int width = getWidth();
if (row == null || row.length < width) {
row = new byte[width];
}
image.getRaster().getDataElements(left, top + y, width, 1, row);
return row;
} public byte[] getMatrix() {
int width = getWidth();
int height = getHeight();
int area = width * height;
byte[] matrix = new byte[area];
image.getRaster().getDataElements(left, top, width, height, matrix);
return matrix;
} public boolean isCropSupported() {
return true;
} public LuminanceSource crop(int left, int top, int width, int height) {
return new BufferedImageLuminanceSource(image, this.left + left,
this.top + top, width, height);
} public boolean isRotateSupported() {
return true;
} public LuminanceSource rotateCounterClockwise() {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0,
0.0, 0.0, sourceWidth);
BufferedImage rotatedImage = new BufferedImage(sourceHeight,
sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = rotatedImage.createGraphics();
g.drawImage(image, transform, null);
g.dispose();
int width = getWidth();
return new BufferedImageLuminanceSource(rotatedImage, top,
sourceWidth - (left + width), getHeight(), width);
}
}
分别保存在两个java类文件中,然后打成jar包,直接调用包中的createCode方法即可,参数别分为二维码内容,图片路径,存放地址,图片是否需要压缩。
Java语言实现二维码的生成的更多相关文章
- java 二维码原理以及用java实现的二维码的生成、解码(转)
http://blog.csdn.net/songylwq/article/details/8643948 http://sjsky.iteye.com/blog/1136934 http://bbs ...
- Java 条形码 二维码 的生成与解析
Barcode简介 Barcode是由一组按一定编码规则排列的条,空符号,用以表示一定的字符,数字及符号组成的,一种机器可读的数据表示方式. Barcode的形式多种多样,按照它们的外观分类: Lin ...
- java zxing实现二维码生成和解析zxing实现二维码生成和解析
原文:https://www.cnblogs.com/zhangzhen894095789/p/6623041.html zxing实现二维码生成和解析 二维码 zxing 二维码的生成与解析 ...
- 关于java的二维码的生成与解析
本文说的是通过zxing实现二维码的生成与解析,看着很简单,直接上代码 import java.io.File; import java.io.IOException; import java.nio ...
- Android zxing 解析二维码,生成二维码极简demo
zxing 官方的代码很多,看起来很费劲,此demo只抽取了有用的部分,实现了相机预览解码,解析本地二维码,生成二维码三个功能. 简化后的结构如下: 废话少说直接上代码: BaseDecodeHand ...
- ZXing二维码的生成和解析
Zxing是Google提供的关于条码(一维码.二维码)的解析工具,提供了二维码的生成与解析的方法, 现在我简单介绍一下使用Java利用Zxing生成与解析二维码 注意: 二维码的生成需要借助辅助类( ...
- Android实例-实现扫描二维码并生成二维码(XE8+小米5)
相关资料: 第三方资料太大没法写在博文上,请下载CSDN的程序包. 程序包下载: http://download.csdn.net/detail/zhujianqiangqq/9657186 注意事项 ...
- zxing二维码的生成与解码(C#)
ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括:J2ME.J2SE和An ...
- java画海报二维码
package cn.com.yitong.ares.qrcode; import java.awt.BasicStroke;import java.awt.Color;import java.awt ...
随机推荐
- 常用加密算法学习总结之数字证书与TLS/SSL
数字证书 对于一个安全的通信,应该有以下特征: 完整性:消息在传输过程中未被篡改 身份验证:确认消息发送者的身份 不可否认:消息的发送者无法否认自己发送了信息 显然,数字签名和消息认证码是不符合要求的 ...
- [刷题] 455 Assign Cookies
要求 贪心算法的关键:判断问题是否可以用贪心算法解决 给小朋友们分饼干,每个小朋友"贪心指数"为g(i),饼干大小值s(i) g(i):小朋友需要的饼干大小的最小值 若s(j)&g ...
- sosreport -a --report
sosreport -a --report 时间:2019-09-28 本文章向大家介绍sosreport -a --report,主要包括sosreport -a --report使用实例.应用技巧 ...
- tar解压某个目录 tar解压某个指定的文件或者文件夹
tar解压某个目录 tar解压某个指定的文件或者文件夹 发布时间:2017-05-30 来源:服务器之家 1. 先查看压缩文档中有那些文件,如果都不清楚文件内容,然后就直接解压,这个是不可能的 使 ...
- 服务器硬件必须支持M2 或PCIE才能支持NVME
兆芯服务器不支持NVME. 服务器硬件必须支持M2 或PCIE才能支持NVME.1 因为物理接口只有M2 SATA 和PCIE这三中但是NVME只支持M2 和PCIE这2种2所以 NVME不支持SAT ...
- 使用 dd 命令进行硬盘 I/O 性能检测
使用 dd 命令进行硬盘 I/O 性能检测 作者: Vivek Gite 译者: LCTT DongShuaike | 2015-08-28 07:30 评论: 1 收藏: 6 如何使用dd命令测 ...
- 支持边云协同终身学习特性,KubeEdge子项目Sedna 0.3.0版本发布!
摘要:随着边缘设备数量指数级增长以及设备性能的提升,边云协同机器学习应运而生,以期打通机器学习的最后一公里. 本文分享自华为云社区<支持边云协同终身学习特性,KubeEdge子项目Sedna 0 ...
- 使用ubuntu charmed kubernetes 部署一套生产环境的集群
官方文档: https://ubuntu.com/kubernetes/docs 搭建一个基本的集群 集群ip规划 hostname ip ubuntu-1 10.0.0.10 juju-contro ...
- 摄像头PVD和CVD薄膜
摄像头PVD和CVD薄膜 在FDP 的生产中,在制作无机薄膜时,可以采用的方法有两种:PVD 和CVD (将VE 和VS 归于PVD ,而ALD 归于CVD). Physical Vapor Depo ...
- ARM系统架构
ARM系统架构 一.ARM概要 ARM架构,曾称进阶精简指令集机器(Advanced RISC Machine)更早称作Acorn RISC Machine,是一个32位精简指令集(RISC)处理器架 ...