Zxing ist ein Parsing-Tool über Barcodes (One-R-Code, QR-Code) von Google. Es bietet Methoden zur Generierung und Analyse von QR -Codes. Jetzt werde ich kurz die Verwendung von Java zur Generierung und Analyse von QR -Codes mithilfe von ZXing einführen.
1. Erzeugung von QR -Code
1.1 Fügen Sie das Paket von Zxing-Core.jar zu ClassPath hinzu.
1.2 Die Erzeugung von QR -Code erfordert die Hilfe der MatrixtoimageWriter -Klasse. Diese Klasse wird von Google bereitgestellt. Sie können die Klasse in den Quellcode kopieren. Hier füge ich den Quellcode dieser Klasse ein und kann direkt verwendet werden.
import com.google.zxing.common.bitmatrix; import Javax.imageo.imageo; Import Java.io.file; importieren java.io.outputstream; importieren java.io.ioException; Import Java.awt.image.BuffenedImage; public Final Class MatrixtoimageWriter {private statische endgültige int schwarz = 0xff000000; private statische endgültige int weiß = 0xffffffff; private matrixtoimageWriter () {} public static bufferedImage tobedimage (bitmatrix matrix) {int width = matrix.getWidth (); int Höhe = matrix.getheight (); Bufferedimage image = new bufferedImage (width, Höhe, bufferedimage.type_int_rgb); für (int x = 0; x <width; x ++) {für (int y = 0; y <Höhe; y ++) {image.setRGB (x, y, matrix.get (x, y)? schwarz: weiß); }} return Image; } public static void writeToFile (Bitmatrix matrix, String -Format, Dateidatei) löscht IOException {bufferedImage image = tobufedImage (matrix); if (! imageio.write (Bild, Format, Datei)) {Neue IOException ("konnte ein Bild des Formats nicht schreiben" + Format + "to" + Datei); }} public static void writeToStream (Bitmatrix matrix, String -Format, Ausgabestream) löscht IOException {bufferedImage image = tobuffeedImage (matrix); if (! imageio.write (Bild, Format, Stream)) {Neue IOException ("konnte kein Bild des Formats schreiben" + Format); }}}1.3 Schreiben Sie den Implementierungscode, der QR -Code generiert
try {string content = "120605181003; http: //www.cnblogs.com/jtmjx"; String path = "c:/user/administrator/desktop/zertimage"; MultiformatWriter multiformatWriter = neuer MultiformatWriter (); MAP Hins = new HashMap (); Hints.put (EncodehintType.character_set, "utf-8"); Bitmatrix bitmatrix = multiformatwriter.encode (Inhalt, BarcodeFormat.qr_code, 400, 400, Hinweise); Dateidatei1 = neue Datei (Pfad, "Nambegernner.jpg"); MatrixtoimageWriter.WritetoFile (Bitmatrix, "JPG", Datei1); } catch (Ausnahme e) {e.printstacktrace (); }Nach dem Laufen können Sie ein QR -Codebild generieren. Ist es nicht sehr einfach? Schauen wir uns als nächstes an, wie Sie den QR -Code analysieren können
2. Analyse des QR -Codes
2.1 Fügen Sie das Paket von Zxing-Core.jar zu ClassPath hinzu.
2.2 Wie bei der Generation benötigen wir eine Hilfsklasse (bufferedImageluminancesource), die ebenfalls von Google bereitgestellt wird. Hier füge ich auch den Quellcode dieser Klasse ein und kann ihn direkt kopieren und verwenden, um die Probleme der Suche zu speichern.
BufferedImageluminancesource import com.google.zxing.luminancesource; Import Java.awt.Graphics2d; import Java.awt.geom.Affinetransform; Import Java.awt.image.BuffenedImage; Public Final Class BufferedImageluminancesource erweitert Luminancesource {Private Final BufferedImage Image; privates Finale int gelassen; Private Final INT Top; public bufferedImageluminancesource (bufferedimage Bild) {this (Bild, 0, 0, Image.getWidth (), Image.Getheight ()); } public bufferedImageluminancesource (bufferedImage -Bild, int links, int oben, int Breite, int Höhe) {Super (Breite, Höhe); int sourcewidth = image.getWidth (); int SourceHeight = Image.Getheight (); if (links + width> SourceWidth || Top + Höhe> SourceHeight) {Wirf neu illegalArgumentException ("Crop -Rechteck passt nicht in Bilddaten."); } für (int y = top; y <top+Höhe; y ++) {für (int x = links; x <links+width; x ++) {if ((image.getRGB (x, y) & 0xff0000000000000000) {image.setrgb (x, y, 0xffffffff); // = weiß}}} this.image = new bufferedImage (SourceWidth, SourceHeight, BufferedImage.Type_Byte_Gray); this.image.getGraphics (). DrawImage (Bild, 0, 0, null); this.left = links; this.top = top; } @Override public byte [] getRow (int y, byte [] row) {if (y <0 || y> = getheight ()) {neue IllegalArgumentException ("angeforderte Zeile ist außerhalb des Bildes:" + y); } int width = getwidth (); if (row == null || row.length <width) {row = new byte [width]; } image.getRaster (). getDataelements (links, oberst + y, width, 1, row); Rückreihe; } @Override public byte [] getMatrix () {int width = getWidth (); int Höhe = getheight (); int Bereich = Breite * Höhe; byte [] matrix = new Byte [Bereich]; Image.getRaster (). getDataElements (links, oben, Breite, Höhe, Matrix); MATRIX RETURN; } @Override public boolean iscropsupported () {return true; } @Override public luminancesource crop (int links, int top, int width, int lhohe) {return New bufferedImageluminancesource (Bild, this.left + links, this.top + top, width, Höhe); } @Override public boolean isRotateSupported () {return true; } @Override public luminancesource rotateCounterClockwise () {int SourceWidth = image.getWidth (); int SourceHeight = Image.Getheight (); Affinetransform -Transformation = Neue Affinetransform (0,0, -1,0, 1,0, 0,0, 0,0, Quellbreite); BufferedImage rotatedImage = new bufferedImage (SourceHeight, SourceWidth, BufferedImage.Type_Byte_Gray); Graphics2d g = rotatedImage.createGraphics (); G.Drawimage (Bild, Transformation, NULL); g.disponse (); int width = getwidth (); Rückgabe neuer BufferedImageluminancesource (RotatedImage, oben, Quellbreite - (links + Breite), Getheight (), Breite); }}2.3 Schreiben Sie den Implementierungscode, der den QR -Code analysiert
try {multiformatReader formatReader = new multiformatReader (); String filepath = "c: /Users/administrator/desktop/testimage/test.jpg"; Datei Datei = neue Datei (FilePath); Bufferedimage image = imageio.read (Datei) ;; Luminancesource Source = New BufferedImageluminancesource (Bild); Binarizer Binarizer = neuer Hybridbinarisator (Quelle); Binarybitmap binarybitmap = new Binarybitmap (Binarizer); MAP Hins = new HashMap (); Hints.put (EncodehintType.character_set, "utf-8"); Ergebnis result = formatReader.decode (Binarybitmap, Hinweise); System.out.println ("result ="+ result.toString ()); System.out.println ("resultFormat ="+ result.getBarcodeFormat ()); System.out.println ("resultText ="+ result.getText ()); } catch (Ausnahme e) {e.printstacktrace (); }Nach dem Laufen können Sie sehen, dass die Konsole den Inhalt des QR -Codes druckt.
Bisher verwendete Zxing Zxing, um den QR -Code zu generieren und zu analysieren, und die Demonstration wurde abgeschlossen, hauptsächlich Memos für sich selbst und gleichzeitig für die Bedürftigen bequem. hehe!
Das obige einfache Beispiel für Java verwendet Zxing, um QR -Codes zu generieren, ist der gesamte Inhalt, den ich mit Ihnen teile. Ich hoffe, Sie können Ihnen eine Referenz geben und ich hoffe, Sie können wulin.com mehr unterstützen.