1. 程式人生 > >生成二維碼的三種方式

生成二維碼的三種方式

gb2312 pub barcode stub string dbi mem [] eth

一:二維碼的概念
二維條碼(2-dimemsional bar code)是用某種特定的幾何圖形按一定規律在平面(二維方向上)分布的黑白相間的圖形記錄數據符號信息的圖形

二: 二維碼的分類
通常分為三種類型:
1.0 線性堆疊式二維碼
編碼原理:建立在一維條碼基礎之上,按需要堆積成兩行或者多行
2.0 矩陣式二維碼
在一個矩陣空間通過黑白像素在矩陣中的不同分布進行編碼,用點表示二進制的1,空白表示二進制的0
3.0 郵政碼
通過不同長度的條進行編碼,主要用於郵政編碼,如POSTNET、BPO4-STATE

三: 二維碼的優缺點
1.0 高密度編碼,信息容量大
2.0 編碼範圍廣
3.0 容錯能力強
4.0 譯碼可靠性高


5.0 可引入加密措施
6.0 成本低,易制作,持久耐用

缺點:二維碼成為手機病毒,釣魚網站傳播的新渠道、信息泄露

四:QR Code
1.0 目前流行的三大國際標準
PDF417 : 不支持中文
DM : 專利未公開,需支付專利費用
QR Code : 專利公開,支持中文
2.0 兩種生成方式
01 : 借助第三方的jar包,如zxing和qrcodejar
02 : javascript,如jQuery.qrcode.js

五 :實例講解
1.0: zxing (http://github.com/zxing/)
1.1 : 進入該鏈接下載源碼,新建java項目,導入core、javase包中的com文件,export為jar文件


1.2: public class CreatQRCode {

public static void main(String[] args) {

int width = 300;
int height = 300;
String formart = "png";
String content = "www.gongzifusu.com";

//定義二維碼的參數
HashMap hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); //編碼格式
hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M); //容錯等級


hints.put(EncodeHintType.MARGIN,2); //邊距

//生成二維碼
try {

BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
Path file = new File("C:/Users/Administrator/Desktop/QRCode/img.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, formart, file);

} catch (Exception e) {

e.printStackTrace();

}

}

}
1.3: 解析二維碼
public static void main(String[] args) {

try {
MultiFormatReader multiFormatReader = new MultiFormatReader();
File file = new File("C:/Users/Administrator/Desktop/QRCode/img.png");
BufferedImage bufferedImage = ImageIO.read(file);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));

//定義二維碼的參數
HashMap hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); //編碼格式

Result result = multiFormatReader.decode(binaryBitmap,hints);
System.out.println("解析結果: "+result.toString());
System.out.println("二維碼格式: "+result.getBarcodeFormat());
System.out.println("二維碼文本內容: "+result.getText());
} catch (Exception e) {
e.printStackTrace();
}
}

2.0 QRCode方式生成二維碼(http://www.swetake.com/qrcode/index-e.html)/(https://osdn.jp/projects/qrcode/)
2.1 :下載相關源碼文件,創建java項目,導入jar包,
2.2 : 生成二維碼
public class CreateQRCodeByQC {

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

Qrcode x = new Qrcode();
x.setQrcodeEncodeMode(‘M‘); //糾錯等級
x.setQrcodeEncodeMode(‘B‘); //N代表數字,A代表a_Z,B代表其他符號
x.setQrcodeVersion(7); //版本
String qrDate = "天降大任於斯人也,必先苦其心誌,勞其筋骨,餓其體膚,空乏其身,增益其所不能,所以動心忍性...";

int width = 67 + 12 * (x.getQrcodeVersion()-1);
int height = 67 + 12 * (x.getQrcodeVersion()-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 = qrDate.getBytes("gb2312");
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("C:/Users/Administrator/Desktop/QRCode/qrcode.png"));

}

}
2.3 解析二維碼
public class ReadQRCodeByQR {

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

File file = new File("C:/Users/Administrator/Desktop/QRCode/qrcode.png");

BufferedImage bufferedImage = ImageIO.read(file);
QRCodeDecoder qrCodeDecoder = new QRCodeDecoder();
String result = new String(qrCodeDecoder.decode(new MyQRCodeImage(bufferedImage)),"gb2312");

System.out.println(result);

}

}

public class MyQRCodeImage implements QRCodeImage{

BufferedImage bufferedImage;


public MyQRCodeImage(BufferedImage bufferedImage) {
super();
this.bufferedImage = bufferedImage;
}

@Override
public int getHeight() {

// TODO Auto-generated method stub
return bufferedImage.getHeight();
}

@Override
public int getPixel(int arg0, int arg1) {

// TODO Auto-generated method stub
return bufferedImage.getRGB(arg0, arg1);
}

@Override
public int getWidth() {

// TODO Auto-generated method stub
return bufferedImage.getWidth();
}

}

3.0 jQuery-qrcode(https://github.com/jeromeetienne/jquery-qrcode)
3.1 新建web項目,導入jquery.min.js、jquery.qrcode.min.js
3.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=ISO-8859-1">
<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>
<div id="qrcode"></div>

<script type="text/javascript">
jQuery(‘#qrcode‘).qrcode("www.STK.com");
</script>
</body>
</html>

生成二維碼的三種方式