One, MAVEN relies on
<Dependencies> <Dependency> <groupId>Junit</groupId> <Artifactid>Junit</Artifactid> <version>4.12</version> <Scope>Test</Scope> </Dependency> <!--Https://mvnrepository.com/artifact/com.google.zxing/core - <Dependency> <groupId>Com.google.zxing</groupId> <Artifactid>Core</Artifactid> <version>3.3.2</version> </Dependency> <!--Https://mvnrepository.com/artifact/com.google.zxing/javase - <Dependency> <groupId>Com.google.zxing</groupId> <Artifactid>Javase</Artifactid> <version>3.3.2</version> </Dependency> <!--Https://mvnrepository.com/artifact/com.alibaba/fastjson - <Dependency> <groupId>Com.alibaba</groupId> <Artifactid>Fastjson</Artifactid> <version>1.2.46</version> </Dependency> </Dependencies>
Second, the use of the entity class
Public classfiveelements {PrivateString name; PrivateString Gender; PrivateString Idtype; PrivateString Idno; PrivateString Mobile; Publicfiveelements () {} PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString Getgender () {returngender; } Public voidSetgender (String gender) { This. Gender =gender; } PublicString Getidtype () {returnIdtype; } Public voidSetidtype (String idtype) { This. Idtype =Idtype; } PublicString Getidno () {returnIdno; } Public voidSetidno (String idno) { This. Idno =Idno; } PublicString Getmobile () {returnMobile; } Public voidSetmobile (String mobile) { This. Mobile =Mobile; } @Override PublicString toString () {return"Fiveelements [name=" + name + ", gender=" + Gender + ", idtype=" + Idtype + ", idno=" +Idno+ ", mobile=" + Mobile + "]"; } }
View Code
Third, the tool class
ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;ImportJava.nio.file.Path;ImportJava.util.HashMap;ImportJavax.imageio.ImageIO;ImportCom.alibaba.fastjson.JSON;ImportCom.alibaba.fastjson.JSONObject;ImportCom.google.zxing.BarcodeFormat;ImportCom.google.zxing.BinaryBitmap;ImportCom.google.zxing.EncodeHintType;ImportCom.google.zxing.MultiFormatReader;ImportCom.google.zxing.MultiFormatWriter;Importcom.google.zxing.NotFoundException;ImportCom.google.zxing.Result;ImportCom.google.zxing.client.j2se.BufferedImageLuminanceSource;ImportCom.google.zxing.client.j2se.MatrixToImageWriter;ImportCom.google.zxing.common.BitMatrix;ImportCom.google.zxing.common.HybridBinarizer;ImportCom.google.zxing.qrcode.decoder.ErrorCorrectionLevel;/*** * @ClassName: Qrcodeutils * @Description: Create and read QR code *@authorDoubledumbao * @ Modified: February 26, 2018 9:47:00 * Modify content: Create*/ Public classqrcodeutils{ Public Static Final intWIDTH = 300; Public Static Final intHEIGHT = 300; Public Static FinalString FORMAT = "png"; Public Static FinalString chartset = "Utf-8"; Public Static voidMain (string[] args) {String FilePath= "/users/doubledumbao/zhao.png"; Createqrcode (FilePath); Testreadqrcode (FilePath); } Private Static voidTestreadqrcode (String filePath) {result result=Getqrresult (FilePath); if(Result! =NULL) {System.out.println ("Two-dimensional code content:" +Result.gettext ()); if(Result.gettext ()! =NULL) {fiveelements fiveelements= Jsonobject.parseobject (Result.gettext (), fiveelements.class); System.out.println (fiveelements); } System.out.println ("Two-dimensional code format:" +Result.getbarcodeformat ()); } } /*** * @Title: Createqrcode * @Description: Create QR code *@paramFilePath *@authorDoubledumbao * @ Modified: February 26, 2018 9:44:45 * Modify content: Create*/ Public Static voidCreateqrcode (String filePath) {/*** If the JDK used is 1.9, you need to configure the following line. */ //system.setproperty ("Java.specification.version", "1.9");Fiveelements fiveelements =Newfiveelements (); Fiveelements.setname ("Zhao"); Fiveelements.setgender (M); Fiveelements.setidtype ("I"); Fiveelements.setidno ("370983"); Fiveelements.setmobile ("1805310"); String Contents=json.tojsonstring (fiveelements); HashMap<encodehinttype, object> hints =NewHashmap<>(); Hints.put (Encodehinttype.character_set, Chartset); Hints.put (Encodehinttype.error_correction, ERRORCORRECTIONLEVEL.M); Hints.put (Encodehinttype.margin,2); Try{Bitmatrix Bitmatrix=NewMultiformatwriter (). Encode (contents, Barcodeformat.qr_code, WIDTH, HEIGHT, hints); Path file=NewFile (FilePath). Topath (); Matrixtoimagewriter.writetopath (Bitmatrix, FORMAT, file); System.out.println ("Create QR Code complete"); } Catch(Exception e) {e.printstacktrace (); } } /*** * @Title: Getqrresult * @Description: Read QR code *@paramFilePath *@return * @authorDoubledumbao * @ Modified: February 26, 2018 9:45:19 * Modify content: Create*/ Public StaticResult Getqrresult (String filePath) {/*** If the JDK used is 1.9, you need to configure the following line. */ //system.setproperty ("Java.specification.version", "1.9");Result result =NULL; Try{File File=NewFile (FilePath); BufferedImage BufferedImage=imageio.read (file); Binarybitmap Bitmap=NewBinarybitmap (NewHybridbinarizer (NewBufferedimageluminancesource (bufferedimage)); HashMap hints=NewHashmap<>(); Hints.put (Encodehinttype.character_set, Chartset); Result=NewMultiformatreader (). Decode (bitmap, hints); } Catch(notfoundexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } returnresult; }}
View Code
Java uses zxing to create and read two-dimensional code