亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

簡介

ZXing 是一個開源 JAVA 類庫用于解析多種格式的 1D/2D 條形碼。目標是能夠?qū)R編碼、Data Matrix、UPC的1D條形碼進行解碼。其提供了多種平臺下的客戶端包括:J2ME、J2SE和Android。

官網(wǎng):ZXing github倉庫

實戰(zhàn)

本例演示如何在一個非 android 的 Java 項目中使用 ZXing 來生成、解析二維碼圖片。

安裝

maven項目只需引入依賴:

<dependency>
  <groupId>com.google.zxing</groupId>
  <artifactId>core</artifactId>
  <version>3.3.0</version>
</dependency>
<dependency>
  <groupId>com.google.zxing</groupId>
  <artifactId>javase</artifactId>
  <version>3.3.0</version>
</dependency>

如果非maven項目,就去官網(wǎng)下載發(fā)布版本:下載地址

生成二維碼圖片

ZXing 生成二維碼圖片有以下步驟:


1.com.google.zxing.MultiFormatWriter 根據(jù)內(nèi)容以及圖像編碼參數(shù)生成圖像2D矩陣。


2.com.google.zxing.client.j2se.MatrixToImageWriter 根據(jù)圖像矩陣生成圖片文件或圖片緩存 BufferedImage 。

public void encode(String content, String filepath) throws WriterException, IOException {
  int width = 100;
  int height = 100;
  Map<EncodeHintType, Object> encodeHints = new HashMap<EncodeHintType, Object>();
  encodeHints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
  BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, encodeHints);
  Path path = FileSystems.getDefault().getPath(filepath);
  MatrixToImageWriter.writeToPath(bitMatrix, "png", path);
}

解析二維碼圖片

ZXing 解析二維碼圖片有以下步驟:

1.使用 javax.imageio.ImageIO 讀取圖片文件,并存為一個
java.awt.image.BufferedImage對象。

2.將
java.awt.image.BufferedImage 轉(zhuǎn)換為 ZXing 能識別的com.google.zxing.BinaryBitmap 對象。


3.com.google.zxing.MultiFormatReader 根據(jù)圖像解碼參數(shù)來解析com.google.zxing.BinaryBitmap 。

public String decode(String filepath) throws IOException, NotFoundException {
  BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filepath));
  LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
  Binarizer binarizer = new HybridBinarizer(source);
  BinaryBitmap bitmap = new BinaryBitmap(binarizer);
  HashMap<DecodeHintType, Object> decodeHints = new HashMap<DecodeHintType, Object>();
  decodeHints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
  Result result = new MultiFormatReader().decode(bitmap, decodeHints);
  return result.getText();
}

分享到:
標簽:二維碼
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定