1. Unterstützung von QRCode und ZXing QR -Codegenerierung und -analyse;
2. QRCode generiert QR -Codes und unterstützt das Hinzufügen von Bildern wie folgt:
Paket gemeinsam; Import Java.awt.Color; Import Java.awt.graphics2d; Import Java.awt.image; Bild Java.awt.image.BuffeedImage; Import Java.io.file; Import Java.io.ioxception; Import Java.util.hasmap; 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.gxing.luminancesource; com.google.zxing.multiformatwriter; import com.google.zxing.notfoundException; import com.google.zxing.writerexception; import com.google.zxing.common.bitmatrix; /** * QR-Code-Erzeugungs-Toolklasse * @Author Cloud * @data 2016-12-15 * QRCode */public class QrCodeUtil {// QR-Code-Farbe private statische endgültige int schwarz = 0xff000000; // QR -Code Farbe private statische endgültige int white = 0xffffffff; /** * <span style = "Schriftgröße: 18px; Schriftgewicht: Blod;"> ZXing-Methode Generiert QR-Code </span> * @param text <a href = "javascript: void ();" rel="external nofollow" >QR code content</a> * @param width QR code width* @param height QR code height* @param outPutPath QR code generation and save path* @param imageType QR code generation format*/ public static void zxingCodeCreate(String text, int width, int height, String outPutPath, String imageType){ Map<EncodeHintType, String> his = new HashMap <encodeHintType, String> (); // Setzen Sie den codierten Zeichen set his.put (EncodeHintType.character_set, "utf-8"); Versuchen Sie {// 1. Generieren Sie den QR -Code -Bitmatrix code = new MultiformatWriter (). CODE (text, barcodeFormat.qr_code, Breite, Höhe, His); // 2. Holen Sie sich die QR -Code -Breite und die Höhe int codewidth = codode.getWidth (); int codeheigh = codode.getheight (); // 3. Legen Sie den QR -Code in das gepufferte Stream BufferedImage Image = New BufferedImage (Codewidth, Codeheight, BufferedImage.Type_int_RGB); für (int i = 0; i <codewidth; i ++) {für (int j = 0; j <codeheigh; j ++) {// 4. Schleife, um den QR -Code -Inhalt in das Image Image.setRGB (I, J, Encode.get (I, J)? Black: White) zu beheben; }} File outputImage = new Datei (AusgabePath); // Wenn das Bild nicht vorhanden ist, erstellen Sie das Bild if (! OutputImage.exists ()) outputImage.createNewFile (); // 5. Schreiben Sie den QR -Code in das Image Imageio.write (Bild, Imagetype, Ausgabe); } catch (writeRexception e) {e.printstacktrace (); System.out.println ("QR -Code -Erzeugung fehlgeschlagen"); } catch (ioException e) {e.printstacktrace (); System.out.println ("QR -Code -Bild ändern"); } } /** * <span style="font-size:18px;font-weight:blod;">QR code analysis</span> * @param analyzePath QR code path* @return * @throws IOException */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static Object zxingCodeAnalyze(String analyzePath) throws Exception{ MultiformatReader formatReader = new MultiformatReader (); Objektergebnis = null; try {Datei file = new Datei (analyzepath); if (! file.exists ()) {return "Der QR -Code existiert nicht"; } BufferedImage image = imageio.read (Datei); Luminancesource Source = New Luminancesourceutil (Bild); Binarizer Binarizer = neuer Hybridbinarisator (Quelle); Binarybitmap binarybitmap = new Binarybitmap (Binarizer); MAP Hins = new HashMap (); Hints.put (EncodehintType.character_set, "utf-8"); result = formatReader.decode (Binarybitmap, Hinweise); } catch (NotFoundException e) {e.printstacktrace (); } Rückgabeergebnis; }/*** <span style = "Schriftgröße: 18px; Schriftgewicht: Blod;"> QRCode generiert QR-Code </span>* @param Inhalt QR-Code-Inhalt* @param imgpath QR-Code-Erzeugungspfad* @param Version QR-Code-Version* @param iflag, ob ein Logo-Imaging-Sting-Streich-Streich-Streich-und-String-Auftrag erzeugt. int Version, String logopath) {try {qrCode qrCodeHandler = new QrCode (); // Setzen Sie die QR-Code-Fehler-Dekodierungsrate, optional L (7%) M (15%) Q (25%) H (30%). Je höher die Fehlerableitungsrate, desto weniger Informationen können gespeichert werden. Je kleiner die Anforderung für die Klarheit des QR-Code QRCodeHandler.SetQrCodeErrorCorrect ('M'); // n repräsentiert die Zahl, a repräsentiert das Zeichen AZ und B repräsentiert andere Zeichen QRCodeHandler.setQrCodeEneCodemode ('B'); // Version 1 ist eine 21*21 -Matrix, und für jede 1 Erhöhung der Version werden die beiden Seiten des QR -Codes um 4 erhöht. Version 7 ist also eine Matrix mit 45*45; Die höchste Version ist 40, eine Matrix mit 177*177 qrcodeHandler.setQrCodeVersion (Version); // Berechnen Sie die Größe gemäß der 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); // Bildfarbe festlegen> schwarz gs.setColor (color.black); // SET -Offset für nicht festgelegt werden kann zu einem Parsing -Fehler int pixoff = 2; // Inhalt ausgeben> QR -Code if (contentBytes.length> 0 && contentBytes.length <130) {boolean [] [] codeout = qrCodeHandler.calqrCode (contentBytes); für (int i = 0; i <codeout.length; i ++) {für (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 -Inhalts -Bytes Länge =" + contentBytes.length + "Nicht in [0,130]."). } /* Bestimmen Sie, ob Sie ein Logo -Bild hinzufügen müssen* / if (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; Bild img = imageio.read (Symbol); gs.drawimage (IMG, width_4 + width_8, height_4 + height_8, width_4, height_4, null); gs.spots (); bufimg.flush (); } else {System.out.println ("Fehler: Anmeldebild existiert nicht!"); }} gs.disponse (); bufimg.flush (); // QR -Code -Datei Datei erstellen imgFile = new Datei (IMGPath); if (! imgfile.exists ()) imgFile.createNewFile (); // das Bild von der generierten Bildzeichenfolge imgType = imgpath.substring (imgpath.lastindexof (".") + 1, imgpath.length ()) abrufen; // QR -Code QRCode Image ImageIo.Write (bufimg, imgType, imgFile) generieren; } catch (Ausnahme e) {e.printstacktrace (); }}/*** <span style = "font-size: 18px; font-weight: blod;"> QRCode QR-Code-Analyse </span>* @param codepath qr code path* @return analyse result*/public static String qrCodeanAnalyze (String codepath) {codepile = new Datei (new Datei); Bufferedimage bufimg = null; String decodedData = null; Versuchen Sie {if (! ImageFile.exists ()) return "Der QR -Code existiert nicht"; bufimg = imageio.read (ImageFile); QrCodEDeCoder decoder = new QrCodEDeCoder (); decodedData = new String (decoder.decode (new ImageUtil (bufimg)), "gb2312"); } catch (ioException e) {System.out.println ("Fehler:" + e.getMessage ()); E. printstacktrace (); } catch (decodingFailedException dfe) {System.out.println ("Fehler:" + dfe.getMessage ()); dfe.printstacktrace (); } return decodedData; }} 3.. Letzter Post -Test -Code: Pakettest; Import Java.awt.image.BuffeedImage; Import Java.io.inputStream; Import Java.net.url; import Javax.imageo.imageo; Import Common.imageUtil; Import Common.qrcodeUtil; Importieren Sie JP.SourceForge.qrcode.qrcodeDeCoder; /*** QR-Code-Erzeugungstestklasse* @author Cloud* @Data 2016-11-21* QrCodetest* /public class QrCodetest {public static void main (String [] args) löst Ausnahme { /*** QRCode-QR-Code-Generierungstest aus* QrcodeUtil.qrcodecreate ("http://blog.csdn.net/u014266877", "e: //qrcode.jpg", 15, "e: //icon.png"); *//*** QRCode QR -Code -Analyse -Test* String qrCodeAnalyze = qrCodeUtil.qrCodeanalyze ("e: //qrcode.jpg"); *//*** ZxingCode QR -Code -Erzeugungstest* qrCodeUtil.zxingCodecreate ("http://blog.csdn.net/U014266877", 300, 300, "E: //zxingcode.jpg", "JPG"); *//*** Zxingcode QR -Code -Analyse* string zxinganalyze = qrcodeUtil.zxingCodeanalyze ("e: //zxingcode.jpg") .toString (); */ System.out.println ("Erfolg"); }}