Java使用Zxing二維碼生成的簡(jiǎn)單示例
1、二維碼簡(jiǎn)介
二維條形碼是用某種特定的幾何圖形按一定規(guī)律在平面(二維方向上)分布的黑白相間的圖形記錄數(shù)據(jù)符號(hào)信息的,在代碼編制上巧妙地利用構(gòu)成計(jì)算機(jī)內(nèi)部邏輯基礎(chǔ)的“0”、“1”比特流的概念,使用若干個(gè)與二進(jìn)制相對(duì)應(yīng)的幾何形體來表示文字?jǐn)?shù)值內(nèi)容信息,通過圖象輸入設(shè)備或光電掃描設(shè)備自動(dòng)識(shí)讀以實(shí)現(xiàn)信息自動(dòng)處理。二維碼具有條碼技術(shù)的一些共性:每種碼制有其特定的字符集;每個(gè)字符占有一定的寬度;具有一定的校驗(yàn)功能等。同時(shí)還具有對(duì)不同行的信息自動(dòng)識(shí)別功能、及處理圖形旋轉(zhuǎn)變化等特點(diǎn)。
二維碼糾錯(cuò)級(jí)別
二維碼糾錯(cuò)級(jí)別指的是在識(shí)別二維碼時(shí),對(duì)于損壞或模糊的二維碼的容錯(cuò)能力。
一般來說,二維碼有四個(gè)糾錯(cuò)級(jí)別:
- L (低):可以糾正7%左右的錯(cuò)誤。
- M (中):可以糾正15%左右的錯(cuò)誤。
- Q (高):可以糾正25%左右的錯(cuò)誤。
- H (高):可以糾正30%左右的錯(cuò)誤。
總結(jié):一般來說,使用較高的糾錯(cuò)級(jí)別會(huì)導(dǎo)致生成的二維碼更大,但是它的容錯(cuò)能力也會(huì)更強(qiáng)。
2、ZXing簡(jiǎn)介
ZXing(Zebra Crossing)是Google開發(fā)的一個(gè)二維碼解析和生成的開源庫。
官網(wǎng)地址:http://code.google.com/p/zxing/
3、示例
通過Java調(diào)用Zxing實(shí)現(xiàn)二維碼的生成
3.1 搭建一個(gè)maven項(xiàng)目,引入Zxing依賴包
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>3.2 創(chuàng)建QrCodeUtil.java 類
具體實(shí)現(xiàn)代碼如下:
package QrCodeUtil;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.alibaba.druid.util.StringUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* 生成二維碼
*/
public class QrCodeUtil {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private static final int margin = 0;
private static final int LogoPart = 4;
public static void main(String[] args) throws WriterException {
//二維碼內(nèi)容
String content = "IT技術(shù)分享社區(qū),一個(gè)有態(tài)度的互聯(lián)網(wǎng)社區(qū)交流平臺(tái)";
String logoPath = "D:\\logo.png"; // 二維碼中間的logo信息 非必須
String format = "jpg";
int width = 120; // 二維碼寬度
int height = 120;// 二維碼高度
// 設(shè)置二維碼矩陣的信息
BitMatrix bitMatrix = setBitMatrix(content, width, height);
// 設(shè)置輸出流
OutputStream outStream = null;
String path = "d:/Code" + new Date().getTime() + ".png";//設(shè)置二維碼的文件名
try {
outStream = new FileOutputStream(new File(path));
// 目前 針對(duì)容錯(cuò)等級(jí)為H reduceWhiteArea 二維碼空白區(qū)域的大小 根據(jù)實(shí)際情況設(shè)置,如果二維碼內(nèi)容長(zhǎng)度不固定的話 需要自己根據(jù)實(shí)際情況計(jì)算reduceWhiteArea的大小
writeToFile(bitMatrix, format, outStream, logoPath, 5);
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 設(shè)置生成二維碼矩陣信息
* @param content 二維碼圖片內(nèi)容
* @param width 二維碼圖片寬度
* @param height 二維碼圖片高度
* @throws WriterException
*/
private static BitMatrix setBitMatrix(String content, int width, int height) throws WriterException {
BitMatrix bitMatrix = null;
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 指定編碼方式,避免中文亂碼
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 指定糾錯(cuò)等級(jí) 如果二維碼里面的內(nèi)容比較多的話推薦使用H 容錯(cuò)率30%, 這樣可以避免一些掃描不出來的問題
hints.put(EncodeHintType.MARGIN, margin); // 指定二維碼四周白色區(qū)域大小 官方的這個(gè)方法目前沒有沒有作用默認(rèn)設(shè)置為0
bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
return bitMatrix;
}
/**
* @param matrix
* @param format
* @param outStream
* @param logoPath logo圖片
* @param reduceWhiteArea 二維碼空白區(qū)域設(shè)置
* @throws IOException
*/
private static void writeToFile(BitMatrix matrix, String format, OutputStream outStream, String logoPath, int reduceWhiteArea) throws IOException {
BufferedImage image = toBufferedImage(matrix, reduceWhiteArea);
// 如果設(shè)置了二維碼里面的logo 加入LOGO水印
if (!StringUtils.isEmpty(logoPath)) {
image = addLogo(image, logoPath);
}
ImageIO.write(image, format, outStream);
}
/**
*
* @param matrix
* @param reduceWhiteArea
* @return
*/
private static BufferedImage toBufferedImage(BitMatrix matrix, int reduceWhiteArea) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width - 2 * reduceWhiteArea, height - 2 * reduceWhiteArea, BufferedImage.TYPE_3BYTE_BGR);
for (int x = reduceWhiteArea; x < width - reduceWhiteArea; x++) {
for (int y = reduceWhiteArea; y < height - reduceWhiteArea; y++) {
image.setRGB(x - reduceWhiteArea, y - reduceWhiteArea, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
/**
* 給二維碼圖片中繪制logo信息 非必須
* @param image 二維碼圖片
* @param logoPath logo圖片路徑
*/
private static BufferedImage addLogo(BufferedImage image, String logoPath) throws IOException {
Graphics2D g = image.createGraphics();
BufferedImage logoImage = ImageIO.read(new File(logoPath));
// 計(jì)算logo圖片大小,可適應(yīng)長(zhǎng)方形圖片,根據(jù)較短邊生成正方形
int width = image.getWidth() < image.getHeight() ? image.getWidth() / LogoPart : image.getHeight() / LogoPart;
int height = width;
// 計(jì)算logo圖片放置位置
int x = (image.getWidth() - width) / 2;
int y = (image.getHeight() - height) / 2;
// 在二維碼圖片上繪制中間的logo
g.drawImage(logoImage, x, y, width, height, null);
// 繪制logo邊框,可選
g.setStroke(new BasicStroke(2)); // 畫筆粗細(xì)
g.setColor(Color.WHITE); // 邊框顏色
g.drawRect(x, y, width, height); // 矩形邊框
logoImage.flush();
g.dispose();
return image;
}
}總結(jié)
到此這篇關(guān)于Java使用Zxing二維碼生成的文章就介紹到這了,更多相關(guān)Java用Zxing二維碼生成內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jmeter 中 CSV 如何參數(shù)化測(cè)試數(shù)據(jù)并實(shí)現(xiàn)自動(dòng)斷言示例詳解
這篇文章主要介紹了Jmeter 中 CSV 如何參數(shù)化測(cè)試數(shù)據(jù)并實(shí)現(xiàn)自動(dòng)斷言,本文通過示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
java多線程消息隊(duì)列的實(shí)現(xiàn)代碼
本篇文章主要介紹了java多線程消息隊(duì)列的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
解決Mybatis出現(xiàn)報(bào)錯(cuò)Error querying database.Cause: j
這篇文章主要介紹了解決Mybatis出現(xiàn)報(bào)錯(cuò)Error querying database.Cause: java.lang.IndexOutOfBoundsException: Index 9 out of,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Intellj Idea中的maven工程Java文件顏色不對(duì),未被識(shí)別的解決
這篇文章主要介紹了Intellj Idea中的maven工程Java文件顏色不對(duì),未被識(shí)別的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08

