1. Mendukung QRCode dan Zxing QR Code Generation and Analysis;
2. QRCode menghasilkan kode QR dan mendukung penambahan gambar, sebagai berikut:
paket umum; Impor java.awt.color; impor java.awt.graphics2d; impor java.awt.image; gambar java.awt.image.bufferedimage; impor java.io.file; impor java.io.ioexception; impor java.util.hashmap; impor javaage. impor jp.sourceForge.qrcode.qrcodedecoder; impor jp.sourceForge.qrcode.exception.decodingfailedException; Impor com.google.zxing.barcodeformat; import com.google.zxing.binarizer; import com.google.zxing.binarybitmap; import com.google.zxing.encodehinttype; import com.google.zxing.luminancesource; impor com.google.google.zxing; luminancesource; impor com.google.google.zxingat; com.google.zxing.multiformatwriter; import com.google.zxing.notfoundException; import com.google.zxing.writerexception; impor com.google.zxing.common.bitmatrix; impor com.google.zxing.common.hybridbrid; /** * QR Code Generation Tool Class * @author cloud * @data 2016-12-15 * qrcode */kelas publik qrcodeutil {// kode qr warna private static final int black = 0xff000000; // kode qr warna private static final int white = 0xffffffff; /** * <span style = "font-size: 18px; font-weight: blod;"> metode zxing menghasilkan kode qr </span> * @param text <a href = "javascript: void ();" rel = "eksternal nofollow"> konten kode qr </a>* @param width qr width kode* @param tinggi qr tinggi kode* @param outputpath Qr code generation dan save path* @param imagetype qr code format*/ string public void zxingCodeCreate (string text, int lebar, tinggi, output his = public void zxingCodeCeate (string text, int widtth, int height, string outpute outpute = string outpute, string outpute, string outpate oViPATH (string outpath outpath, string outpath, string, hashmap baru <encodehinttype, string> (); // Atur karakter yang dikodekan set his.put (encodehinttype.character_set, "UTF-8"); coba {// 1. Hasilkan kode QR bitmatrix encode = multiformatwriter baru (). Encode (Teks, Barcodeformat.qr_code, lebar, tinggi, HIS); // 2. Dapatkan lebar kode QR dan tinggi int codewidth = encode.getWidth (); int codeHeight = encode.getHeight (); // 3. Masukkan kode QR ke dalam buffered stream bufferedImage Image = BufferedImage baru (codewidth, codeHeight, bufferedImage.type_int_rgb); untuk (int i = 0; i <codewidth; i ++) {for (int j = 0; j <codeHeight; j ++) {// 4. Loop untuk memperbaiki konten kode QR ke dalam gambar gambar.setrgb (i, j, encode.get (i, j)? Hitam: putih); }} File outputImage = file baru (outputPath); // Jika gambar tidak ada, buat gambar if (! OutputImage.exist ()) outputimage.createNewFile (); // 5. Tulis kode QR ke gambar gambaro.write (gambar, imagetype, outputImage); } catch (writeRexception e) {e.printstacktrace (); System.out.println ("QR Code Gagal Gagal"); } catch (ioException e) {e.printstacktrace (); System.out.println ("Ubah gambar kode QR gagal"); } } /** * <span style="font-size:18px;font-weight:blod;">QR code analysis</span> * @param analyzePath QR code path* @return * @throws IOException */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static Object zxingCodeAnalyze(String analyzePath) throws Pengecualian {multiformatreader formatReader = multiformatreader baru (); Hasil Objek = NULL; coba {file file = file baru (analisisPath); if (! file.exists ()) {return "Kode QR tidak ada"; } BufferedImage Image = ImageIo.read (file); Sumber Luminancesource = Luminancesourceutil baru (gambar); Binarizer binarizer = hybridbinarizer baru (sumber); BinaryBitMap BinaryBitMap = BinaryBitMap baru (binarizer); Petunjuk peta = hashmap baru (); hints.put (encodehinttype.character_set, "UTF-8"); hasil = formatReader.decode (BinaryBitmap, petunjuk); } catch (notfoundException e) {e.printstacktrace (); } hasil pengembalian; } /** * <span style="font-size:18px;font-weight:blod;">QRCode generates QR code</span> * @param content QR code content* @param imgPath QR code generation path* @param version QR code version* @param isFlag Whether to generate a logo image as NULL does not generate*/ public static void QRCodeCreate(String content, String IMGPath, Versi int, String Logopath) {coba {qrcode qrcodeHandler = qrcode baru (); // Atur tingkat penguraian kesalahan kode QR, opsional L (7%) M (15%) Q (25%) H (30%). Semakin tinggi laju penguraian kesalahan, semakin sedikit informasi yang dapat disimpan, tetapi semakin kecil persyaratan untuk kejelasan kode QR qrcodeHandler.setqrcodeErrorCorrect ('m'); // N mewakili angka, A mewakili karakter AZ, dan B mewakili karakter lain qrcodeHandler.setqrcodeEncodemode ('b'); // Versi 1 adalah matriks 21*21, dan untuk setiap 1 peningkatan versi, kedua sisi kode QR meningkat sebesar 4; Jadi versi 7 adalah matriks dengan 45*45; Versi tertinggi adalah 40, yang merupakan matriks dengan 177*177 qrcodeHandler.setqrcodeversion (versi); // Hitung ukuran sesuai dengan versi int imgsize = 67 + 12 * (versi - 1); byte [] contentBytes = content.getbytes ("gb2312"); BufferedImage bufimg = BufferedImage baru (imgsize, imgsize, bufferedImage.type_int_rgb); Grafik2d gs = bufimg.creategraphics (); gs.setBackground (color.hite); gs.clearrect (0, 0, imgsize, imgsize); // atur gambar gambar> gs.setcolor hitam (color.black); // Setel offset untuk tidak mengatur dapat menyebabkan kesalahan parsing int pixoff = 2; // Konten Output> Kode QR if (contentBytes.length> 0 && contentBytes.length <130) {boolean [] [] codeout = qrcodeHandler.calqrcode (contentBytes); untuk (int i = 0; i <codeout.length; i ++) {for (int j = 0; j <codeout.length; j ++) {if (codeout [j] [i]) {gs.fillrect (j * 3+pixoff, i * 3+pixoff, 3, 3); }}}} else {System.err.println ("Qrcode Content Bytes Length =" + ContentBytes.Length + "Tidak di [0,130]."); } /* Tentukan apakah Anda perlu menambahkan gambar logo* / if (logopath! = Null) {file icon = file baru (logopath); if (icon.exists ()) {int width_4 = imgsize / 4; int width_8 = width_4 / 2; int height_4 = imgsize / 4; int height_8 = height_4 / 2; Image img = ImageIo.read (ikon); gs.drawimage (img, width_4 + width_8, height_4 + height_8, width_4, height_4, null); gs.dispose (); bufimg.flush (); } else {System.out.println ("Kesalahan: Gambar login tidak ada!"); }} gs.dispose (); bufimg.flush (); // Buat file kode QR file imgfile = file baru (imgpath); if (! imgfile.exists ()) imgfile.createNewFile (); // Dapatkan gambar dari string gambar yang dihasilkan imgType = imgpath.substring (imgpath.LastIndexof (".") + 1, imgpath.length ()); // menghasilkan kode qr qrcode gambar gambaro.write (bufimg, imgtype, imgfile); } catch (Exception e) {E.PrintStackTrace (); } } /** * <span style="font-size:18px;font-weight:blod;">QRCode QR code analysis</span> * @param codePath QR code path* @return parsing result*/ public static String QRCodeAnalyze(String codePath) { File imageFile = new File(codePath); BuferedImage bufimg = null; String decodeddata = null; Coba {if (! ImageFile.exists ()) kembalikan "Kode QR tidak ada"; bufimg = ImageIo.read (ImageFile); Qrcodedecoder decoder = qrcodedecoder baru (); decodeddata = string baru (decoder.decode (gambar baru (bufimg)), "gb2312"); } catch (ioException e) {System.out.println ("error:" + e.getMessage ()); e.printstacktrace (); } catch (decodingfailedException dfe) {system.out.println ("error:" + dfe.getMessage ()); dfe.printstacktrace (); } return decodeddata; }} 3. Kode tes posting terakhir: tes paket; impor java.awt.image.bufferedimage; impor java.io.inputstream; import java.net.url; impor javax.imageio.imageio; impor umum.imageutil; impor Common.qrcodeutil; impor jp.sourceForge.qrcode.qrcodedecoder; /*** QR Code Generation Test Class* @Author cloud* @data 2016-11-21* qrcodetest* /kelas publik qrcodetest {public static void main (string [] args) melempar pengecualian { /*** qrcode qr code Generation Test* Qrcodeutil.qrcodecreate ("http://blog.csdn.net/u014266877", "e: //qrcode.jpg", 15, "e: //icon.png"); *//*** Tes Analisis Kode QRCode QR* String qrcodeAnalyze = qrcodeutil.qrcodeAnalyze ("e: //qrcode.jpg"); *//*** ZXINGCODE QR Code Generation Test* qrcodeutil.zxingCodeCreate ("http://blog.csdn.net/u014266877", 300, 300, "e: //zxingcode.jpg", "jpg"); *//*** analisis kode qr zxingcode* string zxinganyze = qrcodeutil.zxingCodeAnalyze ("e: //zxingcode.jpg") .toString (); */ System.out.println ("Success"); }}