Die Verwendung von Zxing zur Implementierung eingebetteter Bilder von QR -Codegeneratoren hat einen bestimmten Referenzwert wie folgt:
Die Grundidee besteht darin, zuerst das von ZXing generierte QR -Codebild zu verwenden, dann das Bild zu lesen, das Symbol darin einzulegen und dann das gesamte Bild auszugeben.
In jüngsten Projekten müssen wir QR -Codes generieren. Nachdem wir einige Beispiele gefunden hatten, haben wir den endgültigen Effekt erreicht. Der QR -Code kann ein Bildformat (JPG usw.) generieren oder auf der Webseite angezeigt werden. Dieser Artikel wird nur aufgezeichnet, und es gibt viele Ähnlichkeiten, also möchte ich ihn vergessen. . . .
HINWEIS: Für Tools, bei denen ZXing -Verpackung erforderlich ist, besteht der allgemeine Prozess darin, eingebettete Bilder zu lesen, den Inhalt in den QR -Code umzuwandeln, die Bilder in QR -Code einzubetten und Bilder zu produzieren.
Hier ist der vollständige Code:
importieren java.awt.basicstroke; import Java.awt.color; import Java.awt.graphics2d; Import Java.awt.image; Bild java.awt.shape; java.awt.image.buffenedimage; import java.io.file; import Java.io.ioException; import Java.util.hashMap; import Java.util.map; importieren javax.imageio.image.imageo; importecodin.zxing. com.google.zxing.multiformatwriter; import com.google.zxing.writerexception; import com.google.zxing.common.bitmatrix; 0xffffffff; // Allgemein private statische endgültige int image_width = 80; private statische endgültige int image_half_width = image_width / 2; private statische endgültige int Frame_width = 2; // QR -Code schreiben private statische Multiformatwriter mutiwriter = new Multiformatwriter (); public static void main (String [] args) {try {// bitmatrix bitmatrix = multiformatwriter.encode (Inhalt, barcodeFormat.qr_code, 400, 400, Hinweise); String content = "13400000000"; // qr Code Inhalt buffen. if (! imageio.write (Bild, "jpg", neue Datei ("f: //2122.jpg")))) {Neue IOException ("konnte kein Bild des Formats schreiben"); } /** // tauschen Sie den obigen Code hier aus und verwenden Sie den Stream, um ihn in die Seite auszuführen, die stream os = response.getOutputStream () in die Seite Ausgabestream lesen; if (! imageio.write (Bild, "jpg", os)) {neue ioException ("konnte kein Bild des Formats schreiben"); } **/} catch (writeRexception e) {e.printstacktrace ();} catch (ioException e) {// Todo automatisch generierter Catch blocke.printstacktrace ();}} private bufferedimage tobuffedimage (bitmatrix matrix) {int width = matrix.getWidth (); matrix.getheight (); bufferedImage bild = new bufferedImage (width, Höhe, buffertermage BufferedImage Genbarcode (String -Inhalt, int Breite, inthohe, String srcimagePath) löst WriteRexception, ioException {// das Quellbild BufferedImage ScaleImage = Skala (srcimagePath, Image_width, Image_Height, True) lesen; int [] [] srcpixels = new int [image_width] [image_height]; für (int i = 0; i <scaleImage.getWidth (); i ++) {für (int j = 0; j <scaleImage.getheight (); j ++) {srcpixels [i] [j] = scaleImage.getrgb (i, j); }} Map <encodehintType, Object> fIDt = new HashMap <enCodehintType, Object> (); Hint.put (EncodeHintType.character_set, "utf-8"); // Inhaltscodierung dint.put (EncodehintType.ERROR_Korrection, Fehlerkreuzelevel.h); // Fehlerstufe Hint.put (EncodintType.Margin, 1); // Setzen Sie die Breite des leeren Bereichs des äußeren Randes des QR -Codes // Generieren Sie den QR -Code bitmatrix matrix = mutiwriter.encode (Inhalt, barcodeFormat.qr_code, Breite, Höhe, Hinweis); // Die 2D-Matrix in ein eindimensionales Pixelarray int halfw = matrix.getWidth () / 2 konvertieren; int halfh = matrix.getheight () / 2; int [] pixels = new int [width * Höhe]; für (int y = 0; y <matrix.getheight (); y ++) {für (int x = 0; x <matrix.getWidth (); x ++) {// das Bild if (x> halfw - image_half_width && x <Halfw+image_half_width && yhalf - halbhalf - yhalf_half_widh && ywidth & s y> halfh - yhalf_widh && ywidth & s image_half_half_widh && ywidh & sgr; yl Image_Half_width) {pixels [y * width + x] = srcpixels [x - halbw + image_half_width] [y - half + image_half_width]; } // Bilden Sie einen Rand um das Bild sonst if ((x> half - Image_Half_width - Frame_Width && x <halbw - Image_Half_width + Frame_Width && y> half - Image_Half_width - Frame_width && y <halfes Image_Half_width + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | uage_half_width + - Frame_Width && x <Halfw + Image_Half_width + Frame_Width && y> Halfh - Image_Half_width - Frame_width && y <Halfh + Image_Half_Width + Frame_width) || && y> half - Image_Half_width - Frame_Width && y <Halfh - Image_Half_width + Frame_Width) || + Image_half_width + Frame_width)) {Pixels [y * width + x] = 0xffffffff; } else {// Hier können Sie die Farbe des QR -Codes ändern und die Farben des QR -Codes bzw. des Hintergrunds formulieren. pixel [y * width + x] = matrix.get (x, y)? 0xff000000: 0xffffffff; }}} Bufferedimage image = new bufferedImage (width, Höhe, bufferedimage.type_int_rgb); Image.getRaster (). setDataElements (0, 0, Breite, Höhe, Pixel); Return Image; } /** * Skalieren Sie das übergebene Originalbild nach Höhe und Breite, um ein Symbol zu generieren, das den Anforderungen entspricht Falsch ist nicht ausfüllt; * @throws ioException */ private statische BufferedImage -Skala (String srcimageFile, inthohe, int width, boolean hasfiller) löst IOException {Double -Verhältnis = 0,0 aus; // skalieren Datei Datei = neue Datei (srcimageFile); Bufferedimage srcimage = imageio.read (Datei); Bilddestimage = srcimage.getScaledInstance (Breite, Höhe, bufferedimage.scale_smooth); // Berechnen Sie die Skala if ((srcimage.getheight ()> Höhe) || (srcimage.getWidth ()> width) {if (srcimage.getheight ()> srcimage.getWidth ()) {ratio = (New Integer (). } else {Ratio = (New Integer (Breite)). DoubleValue () / srcimage.getWidth (); } Affinetransformop op = new Affinetransformop (affinetransform.getScaleinstance (Verhältnis, Verhältnis), NULL); destimage = op.filter (srcimage, null); } if (hasfiller) {// füllen buffenedImage image = new bufferedImage (width, Höhe, bufferedImage.type_int_rgb); Graphics2d Graphic = Image.CreateGraphics (); graphic.setColor (color.pink); grafisch.FillRect (10, 10, Breite, Höhe); Grafik.Drawrect (100, 360, Breite, Höhe); if (width == destimage.getwidth (null)) {graphic.drawimage (destimage, 0, (Höhe - destimage.getheight (null)) / 2, destimage.getwidth (null), destimage.getheight (null), color.white, null); Formform = neuer RoundRectangle2D.Float (0, (Höhe - Destimage.Getheight (null)) / 2, Breite, Breite, 20, 20); Graphic.SetStroke (neuer Grundlagen (5F)); Grafik.Draw (Form); } else {graphic.drawimage (destimage, (width - destimage.getwidth (null)) / 2, 0, destimage.getwidth (null), destimage.getheightheightheight (null), color.white, null); Formform = neuer RoundRectangle2d.float ((Breite - Destimage.Getwidth (null)) / 2, 0, Breite, Breite, 20, 20); Graphic.SetStroke (neuer Grundlagen (5F)); Grafik.Draw (Form); } graphic.dispose (); Destimage = Bild; } return (bufferedImage) destimage; }}Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.