项目ITP(一) 二维码
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
前言
系列文章:[傳送門]
上幾周碌碌無(wú)為,不行啊不行啊。博客園,不知道你幾時(shí)改版。老家了,我不會(huì)忘記你呢。呵呵,我也會(huì)在os,csdn更新的。每天一搏,不管有用沒(méi)用。
?
正文?
正文先有項(xiàng)目起步,項(xiàng)目中的需求很明確。
利用二維碼掃描,然后實(shí)現(xiàn)簽到功能。
自然和app掛鉤。?沒(méi)事,一步一步來(lái)。
二維碼
? 二維碼(QR(Quick Response)code),又稱二維條碼,它是用特定的幾何圖形按一定規(guī)律在平面(二維方向)上分布的黑白相間的圖形,是所有信息數(shù)據(jù)的一把鑰匙。
利用工具-zxing
ZXing是一個(gè)開(kāi)放源碼的,用Java實(shí)現(xiàn)的多種格式的1D/2D條碼圖像處理庫(kù),它包含了聯(lián)系到其他語(yǔ)言的端口。Zxing可以實(shí)現(xiàn)使用手機(jī)的內(nèi)置的攝像頭完成條形碼的掃描及解碼。該項(xiàng)目可實(shí)現(xiàn)的條形碼編碼和解碼
大家可以去了解
https://github.com/zxing/zxing/wiki/Getting-Started-Developing
二維碼(QRCode)的生成
/***?生成二維碼圖片*?@param?content????????內(nèi)容*?@param?width??????????寬度*?@param?height?????????高度*?@param?imgPath????????存儲(chǔ)圖片路徑*/package?sedion.jeffli.wmuitp.util.zxing;import?java.io.File;import?java.util.Hashtable;import?com.google.zxing.BarcodeFormat;import?com.google.zxing.EncodeHintType;import?com.google.zxing.MultiFormatWriter;import?com.google.zxing.client.j2se.MatrixToImageWriter;import?com.google.zxing.common.BitMatrix;import?com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;/***?@author?Jeff?Lee?*/public?class?ZxingEncoderHelper? {????/***?生成二維碼圖片*?@param?content????????內(nèi)容*?@param?width??????????寬度*?@param?height?????????高度*?@param?imgPath????????存儲(chǔ)圖片路徑?????*/public?void?encode(String?content,?int?width,?int?height,?String?imgPath)?{Hashtable<EncodeHintType,?Object>?hts?=?new?Hashtable<EncodeHintType,?Object>();hts.put(EncodeHintType.ERROR_CORRECTION,?ErrorCorrectionLevel.L);????????????????//?糾錯(cuò)等級(jí)hts.put(EncodeHintType.CHARACTER_SET,?"utf-8");??????????????????????????????????//?指定編碼格式為UTF-8try{BitMatrix?bitMatrix?=?new?MultiFormatWriter().encode(content,????????????????//編碼內(nèi)容,編碼類型(這里指定為二維碼),BarcodeFormat.QR_CODE,?width,?height,?hts);??????????????????????????//圖片寬度,圖片高度,設(shè)置參數(shù)?MatrixToImageWriter.writeToFile(bitMatrix,?"png",?new?File(imgPath));????????????????????//生成的二維碼圖片}????????catch?(Exception?e)?{e.printStackTrace();}}????public?static?void?main(String[]?args)?{String?imgPath?=?"d:/33.png";String?contents?=?"你好!我的博客:http://www.cnblogs.com/Alandre/";????????int?width?=?300,?height?=?300;ZxingEncoderHelper?handler?=?new?ZxingEncoderHelper();handler.encode(contents,?width,?height,?imgPath);?} }
?#BitMatrix ?設(shè)置參數(shù)順序分別為:編碼內(nèi)容,編碼類型,生成圖片寬度,生成圖片高度,設(shè)置參數(shù)??
?#MatrixToImageWriter?生成所需要的文件
?
?你會(huì)找到這個(gè)圖片
二維碼(QRCode)的解碼
/***?解碼*?@param?imgPath????????二維碼圖片路徑*?@return*/
package?sedion.jeffli.wmuitp.util.zxing;import?java.awt.image.BufferedImage;import?java.io.File;import?java.util.Hashtable;import?javax.imageio.ImageIO;import?com.google.zxing.BinaryBitmap;import?com.google.zxing.DecodeHintType;import?com.google.zxing.LuminanceSource;import?com.google.zxing.MultiFormatReader;import?com.google.zxing.Result;import?com.google.zxing.client.j2se.BufferedImageLuminanceSource;import?com.google.zxing.common.HybridBinarizer;/***?@author?Jeff?Lee?*/public?class?ZxingDecoderHandler? {????/***?解碼*?@param?imgPath????????二維碼圖片路徑*?@return*/public?String?decode(String?imgPath)?{BufferedImage?image?=?null;Result?result?=?null;????????try?{image?=?ImageIO.read(new?File(imgPath));????????????if?(image?==?null)?{System.out.println("文件不存在!");????????????????????????????????//應(yīng)該拋個(gè)異常的????????????}LuminanceSource?source?=?new?BufferedImageLuminanceSource(image);BinaryBitmap?bitmap?=?new?BinaryBitmap(new?HybridBinarizer(source));Hashtable<DecodeHintType,?Object>?hints?=?new?Hashtable<DecodeHintType,?Object>();hints.put(DecodeHintType.CHARACTER_SET,?"utf-8");result?=?new?MultiFormatReader().decode(bitmap,?hints);????????????return?result.getText();}????????catch?(Exception?e)?{e.printStackTrace();}????????return?null;}????public?static?void?main(String[]?args)?{String?imgPath?=?"d:/33.png";ZxingDecoderHandler?handler?=?new?ZxingDecoderHandler();String?content?=?handler.decode(imgPath);System.out.println("內(nèi)容如下:");System.out.println(content);??} }
?
#和生成的相反
總結(jié)
二維碼生成
二維碼解碼
?
感謝及資源共享
路上走來(lái)一步一個(gè)腳印,希望大家和我一起。
感謝讀者!很喜歡你們給我的支持。如果支持,點(diǎn)個(gè)贊。
知識(shí)來(lái)源: https://github.com/zxing/zxing/wiki/Getting-Started-Developing
轉(zhuǎn)載于:https://my.oschina.net/jeffli1993/blog/227186
總結(jié)
以上是生活随笔為你收集整理的项目ITP(一) 二维码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: exchange2013-Databas
- 下一篇: android binder与hand