Java js 生成二维码 解析二维码

1.二维码分类

  二维条码也有许多不一样的码制,就码制的编码原理而言,一般分为三种类型。javascript

  1. 线性堆叠式二维码

编码原理:
创建在一维条码基础之上,按须要堆积成两行或多行。html

图示: image 2. 矩阵式二维码前端

最经常使用编码,原理: 在一个矩形空间经过黑白像素在矩阵中的不一样分布进行编码。在矩阵相应的位置上,用点(方点、圆点或其它形状)的出现表示二进制“1”,点的不出现表示二进制的“0”java

图示: image 3. 邮政码jquery

经过不一样长度的条进行编码,主要用于邮政编码。android

2.QR Code

  如今最经常使用的就是这种,我们如今主要介绍的也是这种。为啥这种使用二维码那么受反应呢?主要QR Code这种二维码有以下优势:git

  1. 识读速度快
  2. 数据密度大
  3. 占用空间小

2.1 QR Code介绍

image

2.2 QR Code 结构

image你们能够了解下二维码的结构,知道大概就好了,若是想了解详细信息的话能够自行百度,国家有详细的二维码规范。github

3.后台JAVA代码实现二维码(QR Code)生成

  这里介绍以下两种实现方式:api

  1. Java 后台实现,主要使用zxing和qrcodejar等第三方jar包。
  2. 前端javascript实现,主要使用jquery.qrcode.js

3.1 使用zxing生成二维码

3.1.1 zxing相关网站

zxing的GitHub
zxing的Java文档ide

3.1.2 生成zxing jar包

因为github上没有相关的jar包,因此须要咱们本身生成一下,上面有好多关于android相关的,咱们只须要选取核心包和javase这两部分代码。既下图矩形框内容: image 生成方式我大体说下:首先在ecplise里新建一个java项目zxing,将刚才画框代码拷贝进去,而后导出jar包便可。若是你不想生成也能够在个人github上自行下载。

3.1.3 生成二维码代码

package cn.rivamed.zxing;

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class CreateQRCode {
	public static void main(String[] args) {
		
		int width=300;
		int height=300;
		
		String format="png";
		//这里若是你想自动跳转的话,须要加上https://
		String content="https://github.com/hbbliyong/QRCode.git";
		
		HashMap hits=new HashMap();
		hits.put(EncodeHintType.CHARACTER_SET, "utf-8");//编码
		//纠错等级,纠错等级越高存储信息越少
		hits.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
		//边距
		hits.put(EncodeHintType.MARGIN, 2);
		
		try {
			BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hits);
			//若是作网页版输出能够用输出到流
			//MatrixToImageWriter.writeToStream(matrix, format, stream);
			Path path=new File("D:/zxingQRCode.png").toPath();
			MatrixToImageWriter.writeToPath(bitMatrix, format, path);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("that is all");
	}
}

生成的结果以下:

 image

3.1.4 解析二维码代码

package cn.rivamed.zxing;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitArray;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class ReadQRCode {

	public static void main(String[] args) {
		try {
			MultiFormatReader formatReader=new MultiFormatReader();
			File file=new File("D:/zxingQRCode.png");
			BufferedImage image=ImageIO.read(file);
			BinaryBitmap binaryBitmap=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
			
			HashMap hints=new HashMap();
			hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//编码
			
			Result result=formatReader.decode(binaryBitmap, hints);
			System.out.println("解析结果:"+result.toString());
			System.out.println("二维码格式类型:"+result.getBarcodeFormat());
			System.out.println("二维码文本"+result.getText());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

3.2 使用qrcode生成解析二维码

3.2.1 生成二维码

package cn.rivamed.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.UnsupportedEncodingException;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;

public class CreateQRCode {

	public static void main(String[] args) throws IOException {

		Qrcode x=new Qrcode();
		int version=7;
		x.setQrcodeErrorCorrect('M');//纠错等级
		x.setQrcodeEncodeMode('B');//N表明数字,A表明a-Z,B表明其它(中文等)
		
		x.setQrcodeVersion(version);//版本号
		String qrData="https://github.com/hbbliyong/QRCode.git";
		//int width=300;
		int width=67+12*(version-1);
		//int height=300;
		int height=67+12*(version-1);
		BufferedImage bufferedImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
		
		Graphics2D gs=bufferedImage.createGraphics();
		gs.setBackground(Color.WHITE);
		gs.setColor(Color.BLACK);
		gs.clearRect(0, 0, width, height);
		
		int pixoff=2;//偏移量,若是不加有可能会致使识别不许确
		//若是有汉字须要加上编码
		byte[] d=qrData.getBytes("gb2312");
		//byte[] d=qrData.getBytes();
		if(d.length>0&&d.length<120){
			boolean[][] s=x.calQrcode(d);
			
			for(int i=0;i<s.length;i++){
				for(int j=0;j<s.length;j++){
					if(s[j][i]){
						gs.fillRect(j*3+pixoff, i*3+pixoff, 3, 3);
					}
				}
			}
		}
		gs.dispose();
		bufferedImage.flush();
		
		ImageIO.write(bufferedImage, "png", new File("D:/qrcode.png"));
	}

}

生成的结果以下:

image

这里须要注意的是,二维码长宽不能想zxing之直接定义,须要跟进这个公式生成67+12*(version-1)。好比我直接定义二维码的长宽为300.就会变成以下样子。 这上面空白看的不是太清,你把图片下载下载下来看就比较明显了。

3.2.2 解析二维码

package cn.rivamed.qrcode;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;

public class ReadQRCode {

	public static void main(String[] args) throws IOException {
		File file=new File("D:/qrcode.png");
		BufferedImage bufferedImage=ImageIO.read(file);
		QRCodeDecoder codeDecoder=new QRCodeDecoder();
		String result=new String(codeDecoder.decode(new QRCodeImage() {
			
			@Override
			public int getWidth() {
				// TODO Auto-generated method stub
				return bufferedImage.getWidth();
			}
			
			@Override
			public int getPixel(int arg0, int arg1) {
				// TODO Auto-generated method stub
				return bufferedImage.getRGB(arg0, arg1);
			}
			
			@Override
			public int getHeight() {
				// TODO Auto-generated method stub
				return bufferedImage.getHeight();
			}
		}),"gb2312");
		System.out.println(result);
	}

}

4.前台代码jquery生成二维码

4.1 jquery.qrcode.js 的 GitHub

4.2 相关代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>二维码生成</title>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.qrcode.min.js"></script>
</head>
<body>
生成的二维码以下:<br>
<dir id="qrcode"></dir>
<script type="text/javascript">
jQuery('#qrcode').qrcode('https://github.com/hbbliyong/QRCode.git');
</script>
</body>
</html>
相关文章
相关标签/搜索