1. Prise en charge de la génération et de l'analyse du code QR et du ZXing QR;
2. QRCODE génère des codes QR et prend en charge l'ajout d'images, comme suit:
package commun; Importer java.awt.color; import java.awt.graphics2d; import java.awt.image; image java.awt.image.bufferedImage; import java.io.file; import java.itil.map; import java.util.imageio; import jp.sourceforge.qrcode.qrcodedecoder; import jp.sourceforge.qrcode.exception.decodingFailedException; import com.google.zxing.barcodeformat; import com.google.zxing.binarizer; import com.google.zxing.binarybitmap; import com.google.zxing.encodehinttype; import com.google.zxing.luminancesource; import com.google.zxing.multiformataDer; com.google.zxing.multiformatwriter; import com.google.zxing.notfoundException; import com.google.zxing.writexception; import com.google.zxing.common.bitmatrix; import com.google.zxing.common.hybridbinarizer; import com.swetake.util.qrcode; / ** * QR CODE GÉNÉRATION CLASSE * @Author Cloud * @Data 2016-12-15 * QRCODE * / PUBLIC CLASSE QRCODEUTIL {// CODE QR COLOR PRIVATE STATIQUE FINAL INT BLACK = 0XFF000000; // CODE QR COULEUR PRIVATE STATIQUE FINAL INT WHITE = 0XFFFFFFFF; / ** * <span style = "Font-Size: 18px; Font-Weight: Blod;"> La méthode zxing génère le code QR </span> * @param texte <a href = "javaScript: void ();" rel = "External Nofollow"> QR Code Contenu </a> * @param largeur QR Code QR Largeur * @Param Height QR Code HEAUT * @Param OutproutPath QR Code Génération et Save Path * @param ImageType QR Code Generation Format * / public static void zxingCodeCreate (String Text, int, intyle new hashmap <encodeHintType, String> (); // Définit le caractère codé Set His.put (encodeHintType.Caracter_Set, "UTF-8"); essayez {// 1. Générer le code QR BitMatrix Encode = new MultiformatWriter (). Encode (Texte, BarcodeFormat.QR_CODE, Width, Height, His); // 2. Obtenez la largeur du code QR et la hauteur int codeWidth = Encode.getWidth (); int CodeHeight = Encode.GetHeight (); // 3. Mettez le code QR dans le flux tamponné BufferedImage Image = new BufferedImage (CodeWidth, CodeHeight, BufferedImage.Type_int_rgb); for (int i = 0; i <codewidth; i ++) {for (int j = 0; j <codeHeight; j ++) {// 4. LOOP Pour corriger le contenu du code QR dans l'image image.setrgb (i, j, encode.get (i, j)? Noir: blanc); }} File outputImage = new File (outputPath); // Si l'image n'existe pas, créez l'image if (! OutputImage.exists ()) outputImage.CreateEwFile (); // 5. Écrivez le code QR sur l'image ImageIo.Write (image, ImageType, OutputImage); } catch (writeRexception e) {e.printStackTrace (); System.out.println ("QR Génération de code a échoué"); } catch (ioException e) {e.printStackTrace (); System.out.println ("Modifier l'échec de l'image du code QR"); }} / ** * <span style = "Font-Size: 18px; font-weight: Blod;"> QR Analysis de code </span> * @param analysepath QR Code Path * @return * @throws ioexception * / @SuppressWarnings ({"Rawtypes", "Unchecked"}) Objet statique Public ZxingCodCodanyZE (UNCHEDED "}) Objet statique Public ZxingCodCodaDa Exception {MultiformatReader FormataReader = new MultiformatReader (); Résultat de l'objet = null; try {file file = new File (analysepath); if (! file.exists ()) {return "Le code QR n'existe pas"; } BufferedImage Image = ImageIo.Read (fichier); Luminancesource Source = New Luminancesourceutil (image); Binarizer Binarizer = New Hybridbinarizer (source); BinaryBitmap BinaryBitmap = new BinaryBitmap (binarizer); MAP INSTERS = NOUVEAU HASHMAP (); hint.put (encodeHintType.Character_set, "UTF-8"); result = formatReader.decode (binarybitmap, indices); } catch (notfoundException e) {e.printStackTrace (); } Retour Résultat; } / ** * <span style = "font-size: 18px; font-weight: Blod;"> Qrcode génère le code QR </span> * @param contenu du code qr * @param iMgPath QR Code Generation Path * @param version QR version * @param isflag QRCODecrec IMGPATH, INT VERSION, LOGOPATH STRING) {try {qrcode qrcodeHandler = new qrcode (); // Définissez le taux de décodage d'erreur du code QR, L (7%) M (15%) Q (25%) H (30%). Plus le taux de décodage des erreurs est élevé, moins les informations peuvent être stockées, mais plus les exigences pour la clarté du code QR QRCODEHandler.setQrCodeErrorCorrect («M»); // n représente le nombre, A représente le caractère AZ, et B représente d'autres caractères QrcodeHandler.setQrCodeEncodeMode ('B'); // La version 1 est une matrice 21 * 21, et pour chaque augmentation de 1, les deux côtés du code QR sont augmentés de 4; La version 7 est donc une matrice avec 45 * 45; La version la plus élevée est de 40, ce qui est une matrice avec 177 * 177 qrcodehandler.setqrcodeversion (version); // Calculez la taille selon la version int imgsize = 67 + 12 * (version - 1); Byte [] ContentBytes = content.getBytes ("GB2312"); BufferedImage bufimg = new BufferedImage (imgSize, imgsize, bufferedImage.type_int_rgb); Graphics2d gs = bufimg.creategraphics (); gs.setbackground (color.white); gs.ClearRect (0, 0, imgsize, imgsize); // Définir la couleur de l'image> Black Gs.SetColor (Color.Black); // Définir le décalage à ne pas définir peut provoquer une erreur d'analyse int pixoff = 2; // Sortie Contenu> QR Code if (ContentBytes.Length> 0 && ContentBytes.Length <130) {boolean [] [] codeout = qrcodeHandler.calqrcode (contentBytes); pour (int i = 0; i <codeout.length; i ++) {for (int j = 0; j <codeout.length; j ++) {if (codeout [j] [i]) {gs.fillrect (j * 3 + pixoff, i * 3 + pixoff, 3, 3); }}}} else {System.err.println ("Qrcode Content Bytes Longueur =" + ContentBytes.Length + "Pas dans [0,130]."); } / * Déterminez si vous avez besoin d'ajouter une image de logo * / if (logopath! = Null) {File icon = new File (logoPath); if (icon.exists ()) {int width_4 = imgsize / 4; int width_8 = width_4 / 2; int height_4 = imgsize / 4; int height_8 = height_4 / 2; Image img = imageo.read (icon); gs.DrawImage (IMG, width_4 + width_8, height_4 + height_8, width_4, height_4, null); g.dispose (); bufimg.flush (); } else {System.out.println ("Erreur: l'image de connexion n'existe pas!"); }} gs.dispose (); bufimg.flush (); // Créer un fichier de fichier de code QR imgFile = nouveau fichier (imgpath); if (! imgfile.exists ()) imgfile.CreateEwFile (); // Obtenez l'image de la chaîne d'image générée imgType = imgpath.substring (imgpath.lastIndexof (".") + 1, imgpath.length ()); // Générer le code QR QRCODE Image ImageIo.Write (bufimg, imgType, imgFile); } catch (exception e) {e.printStackTrace (); }} / ** * <span style = "font-size: 18px; font-weight: Blod;"> QRCODE QR Code Analysis </span> * @param codepath QR Code Path * @return Analyse Résultat * / public static String QrcodeanalyZe (String CodePath) {File ImageFile = nouveau fichier (codepath); BufferedImage bufimg = null; String decodedData = null; essayez {if (! ImageFile.exists ()) Renvoie "le code QR n'existe pas"; bufimg = imageo.read (imagefile); QRCODEDECODER DECODER = NOUVEAU QRCODEDECODER (); decodedData = new String (decoder.decode (new imageUtil (bufimg)), "gb2312"); } catch (ioException e) {System.out.println ("erreur:" + e.getMessage ()); e.printStackTrace (); } catch (decodingFailedException dfe) {System.out.println ("error:" + dfe.getMessage ()); dfe.printStackTrace (); } Retour DecodedData; }} 3. Dernier code post-test: test de package; Importer java.awt.image.bufferedImage; import java.io.inputStream; import java.net.url; import javax.imageio.imageio; Import Common.imageutil; Importer Common.qrcodeutil; import jp.sourceforge.qrcode.qrcodeDecoder; / ** * QR de génération de code Classe de test * @author cloud * @data 2016-11-21 * QrCodeTest * / public class qrCodeTest {public static void main (String [] args) lève une exception {/ ** * QRCODE QR Code Generation Test * Qrcodeutil.qrcodecreate ("http://blog.csdn.net/U014266877", "e: //qrcode.jpg", 15, "e: //icon.png"); * / / ** * Qrcode QR Code Analysis Test * String qrcodeanalyze = qrcodeutil.qrcodeanalyze ("e: //qrcode.jpg"); * / / ** * ZXingCode QR Code Génération Test * QrcodeUtil.zxingCodeCreate ("http://blog.csdn.net/U014266877", 300, 300, "e: //zxingcode.jpg", "jpg"); * / / ** * ZXingCode QR Analyse du code * chaîne zxinganalyze = qrcodeutil.zxingcodeanalyze ("e: //zxingcode.jpg") .toString (); * / System.out.println ("Success"); }}