当前位置: 移动技术网 > IT编程>开发语言>Java > java 二维码的生成与解析示例代码

java 二维码的生成与解析示例代码

2019年07月22日  | 移动技术网IT编程  | 我要评论

二维码,是一种采用黑白相间的平面几何图形通过相应的编码算法来记录文字、图片、网址等信息的条码图片。如下图

二维码的特点:

1.  高密度编码,信息容量大

可容纳多达1850个大写字母或2710个数字或1108个字节,或500多个汉字,比普通条码信息容量约高几十倍。

2.  编码范围广

该条码可以把图片、声音、文字、签字、指纹等可以数字化的信息进行编码,用条码表示出来;可以表示多种语言文字;可表示图像数据。

3.  容错能力强,具有纠错功能

这使得二维条码因穿孔、污损等引起局部损坏时,照样可以正确得到识读,损毁面积达50%仍可恢复信息。

4.  译码可靠性高

它比普通条码译码错误率百万分之二要低得多,误码率不超过千万分之一。

5.  可引入加密措施

保密性、防伪性好。

6.  成本低,易制作,持久耐用

正因为以上这些特点,二维码现在越来越流行,应用也是越来越广(详细了解请见百度百科,介绍不是本篇重点),所以掌握如何开发二维码是非常不错的知识储备,因此本篇文章将为大家讲解如何生成、解析二维码。

 一、java

所需jar包:qrcode.jar

http://sourceforge.jp/projects/qrcode/

twodimensioncode类:二维码操作核心类

package qrcode;

import java.awt.color;
import java.awt.graphics2d;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;

import javax.imageio.imageio;

import jp.sourceforge.qrcode.qrcodedecoder;
import jp.sourceforge.qrcode.exception.decodingfailedexception;

import com.swetake.util.qrcode;

public class twodimensioncode {
 
 /**
 * 生成二维码(qrcode)图片
 * @param content 存储内容
 * @param imgpath 图片路径
 */
 public void encoderqrcode(string content, string imgpath) {
 this.encoderqrcode(content, imgpath, "png", 7);
 }
 
 /**
 * 生成二维码(qrcode)图片
 * @param content 存储内容
 * @param output 输出流
 */
 public void encoderqrcode(string content, outputstream output) {
 this.encoderqrcode(content, output, "png", 7);
 }
 
 /**
 * 生成二维码(qrcode)图片
 * @param content 存储内容
 * @param imgpath 图片路径
 * @param imgtype 图片类型
 */
 public void encoderqrcode(string content, string imgpath, string imgtype) {
 this.encoderqrcode(content, imgpath, imgtype, 7);
 }
 
 /**
 * 生成二维码(qrcode)图片
 * @param content 存储内容
 * @param output 输出流
 * @param imgtype 图片类型
 */
 public void encoderqrcode(string content, outputstream output, string imgtype) {
 this.encoderqrcode(content, output, imgtype, 7);
 }

 /**
 * 生成二维码(qrcode)图片
 * @param content 存储内容
 * @param imgpath 图片路径
 * @param imgtype 图片类型
 * @param size 二维码尺寸
 */
 public void encoderqrcode(string content, string imgpath, string imgtype, int size) {
 try {
 bufferedimage bufimg = this.qrcodecommon(content, imgtype, size);
 
 file imgfile = new file(imgpath);
 // 生成二维码qrcode图片
 imageio.write(bufimg, imgtype, imgfile);
 } catch (exception e) {
 e.printstacktrace();
 }
 }

 /**
 * 生成二维码(qrcode)图片
 * @param content 存储内容
 * @param output 输出流
 * @param imgtype 图片类型
 * @param size 二维码尺寸
 */
 public void encoderqrcode(string content, outputstream output, string imgtype, int size) {
 try {
 bufferedimage bufimg = this.qrcodecommon(content, imgtype, size);
 // 生成二维码qrcode图片
 imageio.write(bufimg, imgtype, output);
 } catch (exception e) {
 e.printstacktrace();
 }
 }
 
 /**
 * 生成二维码(qrcode)图片的公共方法
 * @param content 存储内容
 * @param imgtype 图片类型
 * @param size 二维码尺寸
 * @return
 */
 private bufferedimage qrcodecommon(string content, string imgtype, int size) {
 bufferedimage bufimg = null;
 try {
 qrcode qrcodehandler = new qrcode();
 // 设置二维码排错率,可选l(7%)、m(15%)、q(25%)、h(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
 qrcodehandler.setqrcodeerrorcorrect('m');
 qrcodehandler.setqrcodeencodemode('b');
 // 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
 qrcodehandler.setqrcodeversion(size);
 // 获得内容的字节数组,设置编码格式
 byte[] contentbytes = content.getbytes("utf-8");
 // 图片尺寸
 int imgsize = 67 + 12 * (size - 1);
 bufimg = new bufferedimage(imgsize, imgsize, bufferedimage.type_int_rgb);
 graphics2d gs = bufimg.creategraphics();
 // 设置背景颜色
 gs.setbackground(color.white);
 gs.clearrect(0, 0, imgsize, imgsize);

 // 设定图像颜色> black
 gs.setcolor(color.black);
 // 设置偏移量,不设置可能导致解析出错
 int pixoff = 2;
 // 输出内容> 二维码
 if (contentbytes.length > 0 && contentbytes.length < 800) {
 boolean[][] codeout = qrcodehandler.calqrcode(contentbytes);
 for (int i = 0; i < codeout.length; i++) {
 for (int j = 0; j < codeout.length; j++) {
 if (codeout[j][i]) {
 gs.fillrect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
 }
 }
 }
 } else {
 throw new exception("qrcode content bytes length = " + contentbytes.length + " not in [0, 800].");
 }
 gs.dispose();
 bufimg.flush();
 } catch (exception e) {
 e.printstacktrace();
 }
 return bufimg;
 }
 
 /**
 * 解析二维码(qrcode)
 * @param imgpath 图片路径
 * @return
 */
 public string decoderqrcode(string imgpath) {
 // qrcode 二维码图片的文件
 file imagefile = new file(imgpath);
 bufferedimage bufimg = null;
 string content = null;
 try {
 bufimg = imageio.read(imagefile);
 qrcodedecoder decoder = new qrcodedecoder();
 content = new string(decoder.decode(new twodimensioncodeimage(bufimg)), "utf-8"); 
 } catch (ioexception e) {
 system.out.println("error: " + e.getmessage());
 e.printstacktrace();
 } catch (decodingfailedexception dfe) {
 system.out.println("error: " + dfe.getmessage());
 dfe.printstacktrace();
 }
 return content;
 }
 
 /**
 * 解析二维码(qrcode)
 * @param input 输入流
 * @return
 */
 public string decoderqrcode(inputstream input) {
 bufferedimage bufimg = null;
 string content = null;
 try {
 bufimg = imageio.read(input);
 qrcodedecoder decoder = new qrcodedecoder();
 content = new string(decoder.decode(new twodimensioncodeimage(bufimg)), "utf-8"); 
 } catch (ioexception e) {
 system.out.println("error: " + e.getmessage());
 e.printstacktrace();
 } catch (decodingfailedexception dfe) {
 system.out.println("error: " + dfe.getmessage());
 dfe.printstacktrace();
 }
 return content;
 }

 public static void main(string[] args) {
 string imgpath = "g:/tddownload/michael_qrcode.png";
 string encodercontent = "hello 大大、小小,welcome to qrcode!" + "\nmyblog [ http://sjsky.iteye.com ]" + "\nemail [ sjsky007@gmail.com ]";
 twodimensioncode handler = new twodimensioncode();
 handler.encoderqrcode(encodercontent, imgpath, "png");
// try {
// outputstream output = new fileoutputstream(imgpath);
// handler.encoderqrcode(content, output);
// } catch (exception e) {
// e.printstacktrace();
// }
 system.out.println("========encoder success");
 
 
 string decodercontent = handler.decoderqrcode(imgpath);
 system.out.println("解析结果如下:");
 system.out.println(decodercontent);
 system.out.println("========decoder success!!!");
 }
}

 

 twodimensioncodeimage 类:二维码图片对象

package qrcode;

import java.awt.image.bufferedimage;

import jp.sourceforge.qrcode.data.qrcodeimage;

public class twodimensioncodeimage implements qrcodeimage {

 bufferedimage bufimg;
 
 public twodimensioncodeimage(bufferedimage bufimg) {
 this.bufimg = bufimg;
 }
 
 @override
 public int getheight() {
 return bufimg.getheight();
 }

 @override
 public int getpixel(int x, int y) {
 return bufimg.getrgb(x, y);
 }

 @override
 public int getwidth() {
 return bufimg.getwidth();
 }

}

 以上就是对java二维码生成与解析的资料整理,后续继续补充,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网