einführen
Das zxing -Paket von GOOLE wird hauptsächlich verwendet. Das Folgende ist ein Beispielcode, der für alle sehr bequem zu verstehen und zu lernen ist. Der Code gehört zum vorläufigen Framework und hat Funktionen und muss gemäß den tatsächlichen Verwendung verbessert und optimiert werden.
Schritt 1: Maven importieren Zxing
<Depopenty> <gruppe> com.google.zxing </Groupid> <artifactId> core </artifactId> <version> 3.2.1 </Version> </abhängig>
Schritt 2: Generieren Sie den QR -Code:
private statische endgültige int schwarz = 0xff000000; 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;}/** * Generate QR code and write to file* @param content Scan the content of the QR code* @param format Image format jpg * @param file file* @throws Exception */public static void writeToFile(String content, String format, File file) throws Exception { MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); @SuppressWarnings ("rawtypes") map tidts = new HashMap (); // UTF-8 festlegen, um chinesische verstümmelte Hinweise zu verhindern (EncodeHintType.character_set, "UTF-8"); // Setzen Sie die Größe des weißen Bereichs um den QR -Code Hinweise (EncodeHintType.Margin, 1); // Setzen Sie die Fehlertoleranz von QR -Code Hinweiss.put (EncodehintType.error_correction, Fehlerkorrektur -Level.h); // Zeichnen Sie den QR -Code bitmatrix bitmatrix = multiformatwriter.encode (Inhalt, BarcodeFormat.qr_code, Breite, Höhe, Hinweise); BufferedImage image = tobufferedimage (bitmatrix); if (! imageio.write (Bild, Format, Datei)) {Neue IOException ("konnte ein Bild des Formats nicht schreiben" + Format + "to" + Datei); }}Schritt 3: Schreiben Sie den Text in das QR -Codebild:
/** * Add text to the QR code image* @param pressText text* @param qrFile QR code file* @param fontStyle * @param color * @param fontSize */public static void pressText(String pressText, File qrFile, int fontStyle, Color color, int fontSize) throws Exception { pressText = new String(pressText.getBytes(), "UTF-8"); Bild src = imageio.read (qrFile); int imagew = src.getwidth (null); int imageh = src.getheight (null); Bufferedimage image = new bufferedImage (ImageW, ImageH, bufferedimage.type_int_rgb); Grafik g = Bild.CreateGraphics (); G.Drawimage (SRC, 0, 0, ImageW, ImageH, NULL); // die Farbe des Pinsels G.SetColor (Farbe) einstellen; // Setzen Sie die Schriftart font = new Font ("安安", fontStyle, fontsize); FontMetrics metrics = g.getFontMetrics (Schriftart); // Die Koordinaten des Textes im Bild sind hier in der Mitte in startx = (width - metrics.stringwidth (pressText)) /2; int starty = Höhe/2; G.SetFont (Schriftart); g.drawstring (pressText, startx, starty); g.disponse (); FileOutputStream out = new FileOutputStream (qrFile); Imageio.Write (Bild, "JPEG", out); out.close (); System.out.println ("Image Press Success");}Schritt 4: Test in der Hauptmethode wird ein QR -Code mit Text in der Mitte generiert
public static void main (String [] args) löst eine Ausnahme aus {Datei qrcFile = new Datei ("/user/deWeixu/", "Google.jpg"); WriteToFile ("www.google.com.hk", "jpg", qrcFile); PressText ("Google", QrcFile, 5, color.red, 32);};Zusammenfassen
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, der Inhalt dieses Artikels wird Ihnen bei Ihrem Studium oder Ihrer Arbeit helfen. Wenn Sie Fragen haben, können Sie eine Nachricht zur Kommunikation überlassen.