幾行代碼搞定java產生解析二維碼功能

來源:互聯網
上載者:User

標籤:二維碼   zxing   java產生解析二維碼   qrcode   multiformatwriter   

最近公司要求掃描二維碼和產生二維碼的功能。而群裡部分網友也提到了。我這裡就寫了一個demo,和大家分享。代碼很簡介,希望大家能夠喜歡。

網友表示在網上搜尋了很多,發現不是代碼不全,就是jar不匹配。

我這裡共用了一個zxing的2.2版本的例子,也提供了。實現代碼如下:

package com.herman.test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.HashMap;import java.util.Map;import javax.imageio.ImageIO;import com.google.zxing.BarcodeFormat;import com.google.zxing.BinaryBitmap;import com.google.zxing.DecodeHintType;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatReader;import com.google.zxing.MultiFormatWriter;import com.google.zxing.NotFoundException;import com.google.zxing.WriterException;import com.google.zxing.client.j2se.BufferedImageLuminanceSource;import com.google.zxing.client.j2se.MatrixToImageWriter;import com.google.zxing.common.HybridBinarizer;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;/** * 二維碼工具類 * @author Herman.Xiong * @date 2015-7-10 上午10:52:15 * @version V3.0 * @since jdk 1.6,tomcat 6.0 */public class ZxingUtil {private static final Map<EncodeHintType, ErrorCorrectionLevel> encodeMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();private static final Map<DecodeHintType, ErrorCorrectionLevel> decodeMap=new HashMap<DecodeHintType, ErrorCorrectionLevel>();private static final String charset="UTF-8",format="png";private static final int size=150;private ZxingUtil() {}/** * 產生二維碼圖片 * @author Herman.Xiong * @date 2015-7-10 上午10:26:33 * @param file * @throws WriterException * @throws IOException */public static void createQRCode(String data, File file)throws WriterException, IOException {MatrixToImageWriter.writeToFile(new MultiFormatWriter().encode(new String(data.getBytes(charset), charset),BarcodeFormat.QR_CODE, size, size, encodeMap), format, file);}/** * 讀取二維碼資訊 * @author Herman.Xiong * @date 2015-7-10 上午10:25:58 * @param filePath 二維碼圖片路徑 * @return * @throws FileNotFoundException * @throws IOException * @throws NotFoundException */public static String readQRCode(String filePath)throws FileNotFoundException, IOException, NotFoundException {return new MultiFormatReader().decode(new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new FileInputStream(filePath))))),decodeMap).getText();}public static void main(String [] arg){try{createQRCode("http://blog.csdn.net/xmtblog", new File("D:\\111.png"));System.out.println(readQRCode("D:\\111.png"));//源碼:http://download.csdn.net/detail/xmt1139057136/8886473//歡迎大家關注我的部落格!如有疑問,請加QQ群:454796847共同學習!}catch(Exception e){e.printStackTrace();}}}
運行效果如下:
歡迎大家關注我的部落格!如有疑問,請加QQ群:454796847共同學習!

源碼:http://download.csdn.net/detail/xmt1139057136/8886473

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

幾行代碼搞定java產生解析二維碼功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.