ZXing 是一個開源 Java 類庫用于解析多種格局的 1D/2D 條形碼。方針是可以或許對QR編碼、Data Matrix、UPC的1D條形碼進行解碼。
第一步調:建立springboot項目。
1、利用IDEA建立springboot項目
2、利用eclipse建立springboot項目
3、添加依靠zxing依靠:
<!--java zxing二維碼(可帶logo)、條形碼生當作-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<!--解析需要依靠的架包-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
2利用建立項目
2利用建立項目
第二步調:編纂生當作二維碼和條形碼的java類
1、具體代碼如下所示:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import com.google.zxing.common.BitMatrix;
/**
* 二維碼的生當作需要借助MatrixToImageWriter類,該類是由Google供給的,可以將該類直接拷貝到源碼中利用,當然你也可以本身寫個
* 出產條形碼的基類
*/
public class WriteBitMatricToFile {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private static BufferedImage toBufferedImage(BitMatrix bm) {
int width = bm.getWidth();
int height = bm.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
image.setRGB(i, j, bm.get(i, j) ? BLACK : WHITE);
}
}
return image;
}
public static void writeBitMatricToFile(BitMatrix bm, String format,
File file) throws IOException {
// 生當作二維碼或者條形碼
BufferedImage image = toBufferedImage(bm);
// 設置logo圖標
// 在圖片上添加log,若是不需要則不消執行此步調
LogoConfig logoConfig = new LogoConfig();
image = logoConfig.LogoMatrix(image);
try {
if (!ImageIO.write(image, format, file)) {
throw new RuntimeException("Can not write an image to file"
+ file);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 生當作不帶log的二維碼或者條形碼
*
* @param matrix
* @param format
* @param stream
* @throws IOException
*/
public static void writeToStream(BitMatrix matrix, String format,
OutputStream stream) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format "
+ format);
}
}
}
第三步調:給圖片(二維碼和條形碼)添加log的java類。
1、具體代碼如下所示:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
//二維碼 添加 logo圖標 處置的方式, 仿照微信主動生當作二維碼結果,有圓角邊框,logo和二維碼間有空白區,logo帶有灰色邊框
public class LogoConfig {
/**
* 設置 logo
*
* @param matrixImage
* 源二維碼圖片
* @return 返回帶有logo的二維碼圖片
* @throws IOException
* @author Administrator sangwenhao
*/
public BufferedImage LogoMatrix(BufferedImage matrixImage)
throws IOException {
/**
* 讀取二維碼圖片,并構建畫圖對象
*/
Graphics2D g2 = matrixImage.createGraphics();
int matrixWidth = matrixImage.getWidth();
int matrixHeigh = matrixImage.getHeight();
/**
* 讀取Logo圖片
*/
BufferedImage logo = ImageIO.read(new File("E:/file/log.jpg"));
// 起頭繪制圖片
g2.drawImage(logo, matrixWidth / 5 * 2, matrixHeigh / 5 * 2,
matrixWidth / 5, matrixHeigh / 5, null);// 繪制
BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
g2.setStroke(stroke);// 設置筆畫對象
// 指心猿意馬弧度的圓角矩形
RoundRectangle2D.Float round = new RoundRectangle2D.Float(
matrixWidth / 5 * 2, matrixHeigh / 5 * 2, matrixWidth / 5,
matrixHeigh / 5, 20, 20);
g2.setColor(Color.white);
g2.draw(round);// 繪制圓弧矩形
// 設置logo 有一道灰色邊框
BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
g2.setStroke(stroke2);// 設置筆畫對象
RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(
matrixWidth / 5 * 2 + 2, matrixHeigh / 5 * 2 + 2,
matrixWidth / 5 - 4, matrixHeigh / 5 - 4, 20, 20);
g2.setColor(new Color(128, 128, 128));
g2.draw(round2);// 繪制圓弧矩形
g2.dispose();
matrixImage.flush();
return matrixImage;
}
}
第四步調:生當作二維碼和條形碼測試代碼。
1、具體代碼如下所示:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
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 EncodeTest {
public static void main(String[] args) throws Exception {
EncodeTest test = new EncodeTest();
// 生當作帶log的二維碼
test.qr_code();
// 生當作不帶log的條形碼
test.ean_code();
}
/**
* 二維碼
*
* @throws IOException
*/
public void qr_code() throws WriterException, IOException {
int width = 300;
int height = 300;
String text = "https://jingyan.baidu.com/user/npublic?uid=be683ee17853d6d6a312287b";
String format = "png";
HashMap<EncodeHintType, Object> hints = new HashMap<>();
// 指心猿意馬糾錯品級,糾錯級別(L 7%、M 15%、Q 25%、H 30%)
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 內容所利用字符集編碼
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 二維碼的格局是BarcodeFormat.QR_CODE
BitMatrix bm = new MultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE, width, height, hints);
// 生當作二維碼圖片
// File out = new File("qr_code.png"); //默認項目根目次里
File out = new File("E:" + File.separator + "file/qr_code.png"); // 指心猿意馬輸出路徑 File.separator解決跨平臺
WriteBitMatricToFile.writeBitMatricToFile(bm, format, out);
}
/**
* 條形碼
*
* @throws IOException
*/
public void ean_code() throws WriterException, IOException {
int width = 200;
int height = 100;
String text = "6923450657713";
String format = "png";
HashMap<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 條形碼的格局是 BarcodeFormat.EAN_13
BitMatrix bm = new MultiFormatWriter().encode(text,
BarcodeFormat.EAN_13, width, height, hints);
// 生當作條形碼圖片
File out = new File("E:" + File.separator + "file/ean3.png");// 指心猿意馬輸出路徑 // File.separator解決跨平臺
//不需要條形碼的log
WriteBitMatricToFile.writeToStream(bm, format, new FileOutputStream(out));
//需要條形碼的log
//WriteBitMatricToFile.writeBitMatricToFile(bm, format, out);
}
}
第五步調:讀取二維碼和條形碼測試類。
1、具體代碼如下所示:
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.GlobalHistogramBinarizer;
import com.google.zxing.common.HybridBinarizer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
public class DecodeTest {
public static void main(String[] args) throws Exception {
// 這是二維碼圖片
BufferedImage bi = ImageIO.read(new FileInputStream(new File("E:" + File.separator + "file/qr_code.png")));
if (bi != null) {
Map<DecodeHintType, String> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
LuminanceSource source = new BufferedImageLuminanceSource(bi);
// 這里還可所以
//BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
Result res = new MultiFormatReader().decode(bitmap, hints);
System.out.println("圖片中內容: "+ res.getText());
System.out.println("圖片中格局: " + res.getBarcodeFormat());
}
// 這是條形碼圖片
BufferedImage rs = ImageIO.read(new FileInputStream(new File("E:" + File.separator + "file/ean3.png")));
if (bi != null) {
Map<DecodeHintType, String> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
LuminanceSource source = new BufferedImageLuminanceSource(rs);
// 這里還可所以
//BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
Result res = new MultiFormatReader().decode(bitmap, hints);
System.out.println("圖片中內容: "+ res.getText());
System.out.println("圖片中格局: " + res.getBarcodeFormat());
}
}
}
第一步調:生當作二維碼和條形碼。
1、生當作含有log的二維碼和條形碼
2、生當作沒有log的條形碼
第二步調:讀取二維碼和條形碼。
1、執行DecodeTest本家兒函數main方式
2、輸出讀取內容
0 篇文章
如果覺得我的文章對您有用,請隨意打賞。你的支持將鼓勵我繼續創作!