memperkenalkan
Paket zxing Goole terutama digunakan. Berikut ini adalah kode sampel, yang sangat nyaman bagi semua orang untuk dipahami dan dipelajari. Kode ini termasuk dalam kerangka awal dan memiliki fungsi, dan perlu ditingkatkan dan dioptimalkan sesuai dengan penggunaan aktual.
Langkah 1: Maven Impor Zxing
<dependency> <GroupId> com.google.zxing </groupId> <ArTifactId> core </stifactid> <version> 3.2.1 </version> </dependency>
Langkah 2: Mulai menghasilkan kode QR:
private static final int black = 0xff000000; private static final int white = 0xffffffff;/*** Simpan kode QR yang dihasilkan ke dalam gambar* @param matrix qr Code kelas di bawah paket zxing* @return*/public static BufferedImage tobufferedImage (bitmatrix matrix) {intelth = matrier static. int height = matrix.getHeight (); BufferedImage Image = BufferedImage baru (lebar, tinggi, bufferedImage.type_int_rgb); untuk (int x = 0; x <lebar; x ++) {untuk (int y = 0; y <tinggi; y ++) {image.setrgb (x, y, matrix.get (x, y)? Hitam: putih); } } 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") peta petunjuk = hashmap baru (); // atur UTF-8 untuk mencegah petunjuk charple charbleds.put (encodehinttype.character_set, "UTF-8"); // Atur ukuran area putih di sekitar kode QR Hints.put (encodehinttype.margin, 1); // Atur toleransi kesalahan kode QR Hints.put (encodehinttype.error_correction, errorCorrectionLevel.h); // Gambar kode QR bitmatrix bitmatrix = multiformatwriter.encode (konten, barcodeformat.qr_code, lebar, tinggi, petunjuk); BufferedImage Image = TobufferedImage (Bitmatrix); if (! imageo.write (gambar, format, file)) {lempar ioException baru ("tidak dapat menulis gambar format" + format + "ke" + file); }}Langkah 3: Tulis teks ke dalam gambar kode QR:
/*** Tambahkan teks ke gambar kode qr* @param pressText Text* @param qrfile file kode qr* @param fontstyle* @param warna* @param fontSize*/public static void pressText (string pressText, file qrfile, int fontStyle, warna public, int fontext) presstext (file qrfile, fontStyle, color, int fontext) presstext {fontstyle {new fontStyle, introws {public presstext (Int fontext) "UTF-8"); Image src = ImageIo.read (qrfile); int imagew = src.getWidth (null); int imageH = src.getHeight (null); BufferedImage Image = BufferedImage baru (Imagew, ImageH, BufferedImage.type_int_rgb); Grafik g = image.creategraphics (); G.DrawImage (Src, 0, 0, Imagew, ImageH, Null); // atur warna kuas g.setColor (warna); // Atur font font = font baru ("安安", fontstyle, fontSize); Metrik fontmetrik = g.getFontMetrics (font); // Koordinat teks dalam gambar diatur di tengah di sini int startx = (lebar - metrics.stringwidth (pressText)) /2; int starty = tinggi/2; g.setfont (font); g.drawstring (pressText, startx, starty); g.dispose (); FileOutputStream out = FileOutputStream baru (qrfile); Imageio.write (gambar, "jpeg", out); out.close (); System.out.println ("Image Press Success");}Langkah 4: Tes dalam metode utama, kode QR dengan teks di tengah akan dihasilkan
public static void main (string [] args) melempar pengecualian {file qrcfile = file baru ("/user/deweixu/", "google.jpg"); writeToFile ("www.google.com.hk", "jpg", qrcfile); pressText ("Google", qrcfile, 5, color.red, 32);}Meringkaskan
Di atas adalah seluruh konten artikel ini. Saya berharap konten artikel ini akan membantu studi atau pekerjaan Anda. Jika Anda memiliki pertanyaan, Anda dapat meninggalkan pesan untuk berkomunikasi.