Es gibt viele Entwicklungs -JAR -Pakete für die Java -Generation von QR -Codes. Zum Beispiel wird Zxing von Google entwickelt. Hier verwende ich das Entwicklungspaket von Zxing, um es zu implementieren. In vielen Projekten müssen wir dynamisch QR -Codes generieren, um sie den Benutzern zur Verfügung zu stellen, damit mehr Menschen ihre Anwendungen durch QR -Codes gut erleben können.
Der Code ist unten veröffentlicht und wurde getestet und Sie können den Code direkt kopieren, um sie zu verwenden:
Endlich Ergebnisse erzielt:
Java generiert QR -Code
Der Code ist wie folgt:
Import Java.awt.Color; Import Java.awt.Graphics2d; Import Java.awt.image; Import Java.awt.Geom.Affinetransform; java.io.unsupportedenCodingException; import Java.util.hashMap; Import Java.util.map; Import Javax.imageo.imageio; com.google.zxing.barcodeFormat; import com.google.zxing.codeHinttype; com.google.zxing.multiformatwriter; import com.google.zxing.WriteRexception; importieren com.google.zxing.common.bitmatrix; com.google.zxing.qrcode.decoder.ErrorcorrectionLevel; public class matrixtoimageWriter {private statische endgültige int image_width = 100; private statische endgültige int image_height = 100; private statische endgültige int image_half_width = image_width / 2; private statische endgültige int Frame_width = 2; privat statischer Multiformatwriter Mutiwriter = neuer Multiformatwriter (); public static void encodes (String -Inhalt, int Breite, inthohe, String srcimagePath, String destimagePath) {try {imageio.write (Genbarcode (Inhalt, Breite, Höhe, SrcimagePath), "JPG", neue Datei (DestimagePath)); } catch (ioException e) {e.printstacktrace (); } catch (writeRexception e) {e.printstacktrace (); }} private static bufferedImage genbarcode (String -Inhalt, int width, int height, String srcimagePath) löst WriteRexception, ioException {bufferedImage scalage = scale (srcimagePath, mager_width, image_height, true) aus; 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"); Hint.put (EncodeHintType.ERROR_Korrection, Fehlerkorrektur -Level.h); // QR -Code -Bitmatrix matrix = mutiwriter.encode (Inhalt, BarcodeFormat.qr_code, Breite, Höhe, Hinweis) erzeugen; // Die zweidimensionale 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 ++) {// die Farbe in der oberen linken Ecke, passen Sie den Farbbereich und die Farbe nach Ihren Bedürfnissen an, wenn (x> 0 && x <170 && y> 0 && y <170) {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{color = New Color = New Color) (231, 231), an. int colorint = color.getRGB (); pixel [y * width + x] = matrix.get (x, y)? Colorint: 16777215; } // Lesen Sie das Bild else if (x> half - image_half_width && x <halfw + mager_half_width && y> half - image_half_width && y <half + mager_half_width) {pixels [y * width + x] Image_Half_Width]; } else if ((x> halfw - image_half_width - Frame_width && x <halbw - image_half_width + Frame_width && y> half - image_half_width - Frame_width && y <half + image_half_width - Frame_width - Framdh. X <Halfw + Image_Half_width + Frame_width && y> Halfw - Image_Half_width - Frame_width && y <Half + Image_Half_width + Frame_Width) || Image_Half_width - Frame_width && y <Halfh - Image_Half_width + Frame_width) || Image_Half_width + Frame_width && y> half + image_half_width - Frame_width && y <half + image_half_width + Frame_width)) {pixels [y * width + x] = 0xffffffff; // einen Rand um das Bild bilden} else {// QR -Code -Farbe int num1 = (int) (50 - (50.0 - 13.0) / matrix.getheight () * (y + 1)); int num2 = (int) (165 - (165,0 - 72,0) / matrix.getheight () * (y + 1)); int num3 = (int) (162 - (162.0 - 107.0) / matrix.getheight () * (y + 1)); Farbe Farbe = neue Farbe (Num1, Num2, Num3); int colorint = color.getRGB (); // Hier können Sie die QR -Codefarbe ändern und den QR -Code bzw. den Hintergrundfarben formulieren. pixel [y * width + x] = matrix.get (x, y)? Colorint: 16777215; // 0x00000: 0xffffff}}} bufferedImage Image = new bufferedImage (width, Höhe, bufferedimage.type_int_rgb); Image.getRaster (). setDataElements (0, 0, Breite, Höhe, Pixel); Return Image; } private statische BufferedImage -Skala (String srcimageFile, inthohe, int width, boolean hasfiller) löscht IOException {Double -Verhältnis = 0,0; // 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 bufferedimage image = new bufferedImage (width, Höhe, bufferedimage.type_int_rgb); Graphics2d Graphic = Image.CreateGraphics (); Graphic.setColor (color.white); grafisch.FillRect (0, 0, Breite, Höhe); if (width == destimage.getwidth (null)) grafik sonst graphic.drawimage (destimage, (width - destimage.getwidth (null)) / 2, 0, destimage.getwidth (null), destimage.getheight (null), color.white, null); grafisch.dispose (); Destimage = Bild; } return (bufferedImage) destimage; } public static void main (String [] args) löst nicht unterstütztenCodingException aus. "D: //2013-01.jpg"); }}Sie können sich auf das Thema beziehen: Java QR -Code zum Lernen
Im obigen Bereich dreht sich alles um diesen Artikel und hilft Ihnen dabei, Ihren eigenen QR -Code zu entwerfen.