欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现二维码功能的实例代码

程序员文章站 2024-03-07 08:28:14
最近突然想写一些文章,所以就陆陆续续的编写一些自我感觉有用的并且大家也可能用到的一些技术代码。方便互相学习交流。 今天这篇文章,主要是利用java实现二维码: 在写代码...

最近突然想写一些文章,所以就陆陆续续的编写一些自我感觉有用的并且大家也可能用到的一些技术代码。方便互相学习交流。

今天这篇文章,主要是利用java实现二维码:

在写代码之前先讲一下整体思路,以方便更好更快捷的实现功能。

(1).首先要想实现二维码功能需要导入com.google.zxing的核心jar包,我这里导入的是core-3.2.1.jar;

(2).所谓二维码莫过于把需要的内容放入一张图片中,所以这里首先是创建一张带有参数内容的图片,方法如下:

private static bufferedimage createimage(string content, string imgpath, boolean needcompress) throws exception {
hashtable<encodehinttype, object> hints = new hashtable<encodehinttype, object>();
hints.put(encodehinttype.error_correction, errorcorrectionlevel.h);
hints.put(encodehinttype.character_set, charset);
hints.put(encodehinttype.margin, 1);
bitmatrix bitmatrix = new multiformatwriter().encode(content, barcodeformat.qr_code, qrcode_size, qrcode_size,
hints);
int width = bitmatrix.getwidth();
int height = bitmatrix.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, bitmatrix.get(x, y) ? 0xff000000 : 0xffffffff);
}
}
if (imgpath == null || "".equals(imgpath)) {
return image;
}
// 插入图片
qrcodeutil.insertimage(image, imgpath, needcompress);
return image;
}
//插入logo 如不需要logo可以执行此方法
private static void insertimage(bufferedimage source, string imgpath, boolean needcompress) throws exception {
file file = new file(imgpath);
if (!file.exists()) {
system.err.println("" + imgpath + "  该文件不存在!");
return;
}
image src = imageio.read(new file(imgpath));
int width = src.getwidth(null);
int height = src.getheight(null);
if (needcompress) { // 压缩logo
if (width > width) {
width = width;
}
if (height > height) {
height = height;
}
image image = src.getscaledinstance(width, height, image.scale_smooth);
bufferedimage tag = new bufferedimage(width, height, bufferedimage.type_int_rgb);
graphics g = tag.getgraphics();
g.drawimage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
src = image;
}
// 插入logo
graphics2d graph = source.creategraphics();
int x = (qrcode_size - width) / 2;
int y = (qrcode_size - height) / 2;
graph.drawimage(src, x, y, width, height, null);
shape shape = new roundrectangle2d.float(x, y, width, width, 6, 6);
graph.setstroke(new basicstroke(3f));
graph.draw(shape);
graph.dispose();
}

(3).至此一张二维码图片生成方法就写完了,是不是很简单,接下来就是调用此方法。

/**
* 生成二维码(内嵌logo)
* 
* @param content
*      内容
* @param imgpath
*      logo地址
* @param destpath
*      存放目录
* @param needcompress
*      是否压缩logo
* @throws exception
*/
public static void encode(string content, string imgpath, string destpath, boolean needcompress) throws exception {
bufferedimage image = qrcodeutil.createimage(content, imgpath, needcompress);
mkdirs(destpath);
string file = new random().nextint(99999999) + ".jpg";
imageio.write(image, format_name, new file(destpath + "/" + file));
}
/**
* 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
* 
* @author longjin
* @date 2013-12-11 上午10:16:36
* @param destpath
*      存放目录
*/
public static void mkdirs(string destpath) {
file file = new file(destpath);
// 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
if (!file.exists() && !file.isdirectory()) {
file.mkdirs();
}
}

(4).如果不要logo只要在调用时将logo的路径传为null就好。

(5).至此一个二维码生成工具就完成了,当然有人可能需要解析二维码,所以这里就把二维码解析方法也写出,同时也方便我日后查看。

/**
* 解析二维码
* 
* @param file
*      二维码图片
* @return
* @throws exception
*/
public static string decode(file file) throws exception {
bufferedimage image;
image = imageio.read(file);
if (image == null) {
return null;
}
bufferedimageluminancesource source = new bufferedimageluminancesource(image);
binarybitmap bitmap = new binarybitmap(new hybridbinarizer(source));
result result;
hashtable<decodehinttype, object> hints = new hashtable<decodehinttype, object>();
hints.put(decodehinttype.character_set, charset);
result = new multiformatreader().decode(bitmap, hints);
string resultstr = result.gettext();
return resultstr;
}

以上所述是小编给大家介绍的java实现二维码功能的实例代码,希望对大家有所帮助