Ada banyak paket toples pengembangan untuk generasi kode QR Java. Misalnya, zxing dikembangkan oleh Google. Di sini saya menggunakan paket pengembangan Zxing untuk mengimplementasikannya. Dalam banyak proyek, kita perlu secara dinamis menghasilkan kode QR untuk memberikannya kepada pengguna, sehingga lebih banyak orang dapat mengalami aplikasi mereka dengan baik melalui kode QR.
Kode ini diposting di bawah ini, dan telah diuji dan Anda dapat menyalin kode secara langsung untuk digunakan:
Akhirnya mencapai hasil:
Java menghasilkan kode QR
Kodenya adalah sebagai berikut:
impor java.awt.color; impor java.awt.graphics2d; impor java.awt.image; impor java.awt.geom.affinetransform; impor java.awt.image.affinetransformop; impor java.awt.image.bufferedimage; impor java.ioo -compleo; impor Javaon. java.io.unsupportedencodingException; impor java.util.hashmap; impor java.util.map; import javax.imageio.imageio; Impor com.google.zxing.barcodeformat; import com.google.zxing.encodehinttype; import com.google.zxing.multiformatwriter; import com.google.zxing.writerexception; impor com.google.zxing.common.bitmatrix; com.google.zxing.qrcode.decoder.errorcorrectionlevel; matrixtoimagewriter kelas publik {private static final int image_width = 100; private static final int image_height = 100; private static int int image_half_width = image_width / 2; private static final int frame_width = 2; private static multiformatwriter mutiwriter = multiformatwriter baru (); public static void encode (konten string, lebar int, tinggi int, string srcimagePath, string destImagePath) {coba {imageo.write (genbarcode (konten, lebar, tinggi, srcimagePath), "jpg", file baru (destimagePath)); } catch (ioException e) {e.printstacktrace (); } catch (writeRexception e) {e.printstacktrace (); }} private static BufferedImage genbarcode (konten string, lebar int, tinggi int, string srcimagePath) melempar writerexception, ioException {bufferedimage scaleImage = skala (srcimagePath, image_width, image_height, true); int [] [] srcpixels = int baru [gambar_width] [gambar_height]; untuk (int i = 0; i <scaleImage.getWidth (); i ++) {for (int j = 0; j <scaleImage.getHeight (); j ++) {srcpixels [i] [j] = scaleImage.getrgb (i, j); }} Peta <encodehintType, objek> hint = hashmap baru <encodehintType, objek> (); hint.put (encodehinttype.character_set, "UTF-8"); hint.put (encodehinttype.error_correction, errorCorrectionLevel.h); // menghasilkan kode qr bitmatrix matrix = mutiwriter.encode (konten, barcodeformat.qr_code, lebar, tinggi, petunjuk); // Konversi matriks dua dimensi menjadi array piksel satu dimensi int halfw = matrix.getWidth () / 2; int setengah = matrix.getHeight () / 2; int [] piksel = int baru [lebar * tinggi]; untuk (int y = 0; y <matrix.getHeight (); y ++) {for (int x = 0; x <matrix.getWidth (); x ++) {// warna di sudut kiri atas, sesuaikan rentang warna dan warna sesuai dengan kebutuhan Anda jika (x> 0 && x <170 && y> 0 & & y <170 int colorint = color.getrgb (); piksel [y * lebar + x] = matrix.get (x, y)? Colorint: 16777215; } // Baca gambar lain jika (x> setengah - image_half_width && x <setengah + gambar_half_width && y> setengah - gambar_half_width && y <setengah + gambar_half_width) {piksel [y * width + x] = srcpixels [x -taruh + setengah + hidth h [y * width + x] = srcpixels [x -setengah + hidhhyhH + HIXELS [y * width + x] = srcpixels [x -setengah + haxels [y * width + x] = srcpixels [x -setengah + holfsels [y * hidth + x] = srcpixels [x -setengah + haxels [y * width + x] = srcpixels [x -setengah + HIXELS [y * width + x] = srcpixels [x -setengah + h [x] Image_half_width]; } else if ((x> setengah - gambar_half_width - frame_width && x <setengah - gambar_half_width + frame_width && y> setengah - gambar_half_width - frame_width && y <setengah + gambar_half_width + frame_width) || Image_half_width + frame_width && y> setengah - gambar_half_width - frame_width && y <setengah + gambar_half_width + frame_width) || Frame_width && y <setengah - image_half_width + frame_width) || (x> setengah - gambar_half_width - frame_width && y <setengah - gambar_half_width -frame_width) || > setengah + gambar_half_width - frame_width && y <setengah + gambar_half_width + frame_width)) {piksel [y * lebar + x] = 0xffffffff; // Bentuk perbatasan di sekitar gambar} else {// qr code warna 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)); Warna warna = warna baru (num1, num2, num3); int colorint = color.getrgb (); // Di sini Anda dapat memodifikasi warna kode QR, dan Anda dapat merumuskan kode QR dan warna latar belakang masing -masing; piksel [y * lebar + x] = matrix.get (x, y)? Colorint: 16777215; // 0x00000: 0xFFFFFF}}} BufferedImage Image = BufferedImage baru (lebar, tinggi, bufferedImage.type_int_rgb); image.getRaster (). setDataelement (0, 0, lebar, tinggi, piksel); gambar kembali; } Skala private static BufferedImage (String srcimageFile, int tinggi, lebar int, boolean hasfiller) melempar ioException {rasio ganda = 0,0; // file skala file = file baru (srcimageFile); BufferedImage SRCIMAGE = ImageIO.READ (file); DestImage gambar = srcimage.getScaledInstance (lebar, tinggi, bufferedImage.scale_smooth); // Calculate the scale if ((srcImage.getHeight() > height) || (srcImage.getWidth() > width)) { if (srcImage.getHeight() > srcImage.getWidth()) { ratio = (new Integer(height)).doubleValue() / srcImage.getHeight(); } else {rasio = (integer baru (lebar)). doublevalue () / srcimage.getWidth (); } Affinetransformop op = affinetransformop baru (affinetransform.getscaleInstance (rasio, rasio), null); destImage = op.filter (srcimage, null); } if (hasFiller) {// Mengisi BufferedImage Image = BufferedImage baru (lebar, tinggi, bufferedImage.type_int_rgb); Graphics2d Graphic = Image.Creategraphics (); graphic.setColor (color.white); graphic.fillrect (0, 0, lebar, tinggi); if (width == destImage.getWidth (null)) grafis.drawimage (destImage, 0, (height - destImage.getheight (null)) / 2, destImage.getWidth (null), destimage.getheight (null), color.white, null); lain graphic.drawimage (destimage, (width - destImage.getWidth (null)) / 2, 0, destImage.getWidth (null), destImage.getheight (null), color.white, null); graphic.dispose (); destImage = gambar; } return (bufferedImage) desttimage; } public static void main (string [] args) melempar UnsportedencodingException {// pada gilirannya, konten (tidak didukung dalam bahasa Cina), lebar, panjang, jalur ikon tengah, jalur penyimpanan matrixtoimagewriter.encode ("http://www.baidu.com/", 512, 512, "22, "http://www.baidu.com/", 512, 512, 512, "2, "512," 512, "512," 512, " "D: //2013-01.jpg"); }}Anda dapat merujuk pada topik: Java QR Code untuk Belajar
Di atas adalah semua tentang artikel ini, membantu Anda merancang kode QR Anda sendiri.