Kode QR adalah gambar barcode yang menggunakan angka geometris bidang hitam dan putih untuk merekam teks, gambar, URL, dan informasi lainnya melalui algoritma pengkodean yang sesuai. Angka berikut
Fitur Kode QR:
1. Pengkodean dengan kepadatan tinggi, kapasitas informasi yang besar
Ini dapat menampung hingga 1850 huruf kapital atau 2710 angka atau 1108 byte, atau lebih dari 500 karakter Cina, yaitu sekitar lusinan kali lebih tinggi dari informasi barcode biasa.
2. Rentang penyandian yang luas
Barcode ini dapat mengkodekan informasi digital seperti gambar, suara, teks, tanda tangan, sidik jari, dll., Dan mengekspresikannya dalam barcode; Mereka dapat mewakili teks dalam berbagai bahasa; dan mereka dapat mewakili data gambar.
3. Fungsi toleransi kesalahan dan kesalahan kesalahan yang kuat
Hal ini memungkinkan barcode dua dimensi rusak dengan baik karena perforasi, kekotoran, dll., Dan informasi masih dapat dipulihkan jika area kerusakan mencapai 50%.
4. Keandalan Dekoding Tinggi
Jauh lebih rendah dari tingkat kesalahan decoding kode batang biasa dari dua pihak per juta, dan tingkat kesalahan tidak lebih dari satu pihak per juta.
5. Langkah -langkah enkripsi dapat diperkenalkan
Kerahasiaan yang baik dan anti-pembantu.
6. Biaya rendah, mudah dibuat, tahan lama
Karena karakteristik di atas, kode QR menjadi lebih dan lebih populer dan lebih banyak digunakan (lihat Baidu Encyclopedia untuk detailnya, pengantar bukanlah fokus artikel ini). Oleh karena itu, menguasai cara mengembangkan kode QR adalah cadangan pengetahuan yang sangat baik. Oleh karena itu, artikel ini akan menjelaskan kepada Anda cara menghasilkan dan menganalisis kode QR.
1. Java
Paket Jar yang Diperlukan: Qrcode.jar
http://sourceForge.jp/projects/qrcode/
Kelas TwoDimensionCode: Kode Operasi Kode QR Kelas Inti
Paket qrcode; impor java.awt.color; impor java.awt.graphics2d; import java.awt.image.bufferedImage; import java.io.file; impor java.io.ioexception; import java.inputStream; impor java.ooutputSputSception; jp.sourceforge.qrcode.QRCodeDecoder;import jp.sourceforge.qrcode.exception.DecodingFailedException;import com.swetake.util.Qrcode;public class TwoDimensionCode { /** * Generate QR code (QRCode) picture* @param content Store content* @param imgPath Picture path*/ public void encoderqrcode (konten string, string imgpath) {this.encoderqrcode (konten, imgpath, "png", 7); } / *** Hasilkan kode qr (qrcode) gambar* @param konten konten konten* @param output output stream* / public void encoderqrcode (konten string, outputStream output) {this.encoderqrcode (konten, output, "png", 7); } /** * Generate QR code (QRCode) picture* @param content Store content* @param imgPath Picture path* @param imgType Picture type*/ public void encoderQRCode(String content, String imgPath, String imgType) { this.encoderQRCode(content, imgPath, imgType, 7); } /** * Generate QR code (QRCode) picture* @param content Store content* @param output Output stream* @param imgType Image type*/ public void encoderQRCode(String content, OutputStream output, String imgType) { this.encoderQRCode(content, output, imgType, 7); } /** * Generate QR code (QRCode) image* @param content Store content* @param imgPath Picture path* @param imgType Image type* @param size QR code size*/ public void encoderQRCode(String content, String imgPath, String imgType, int size) { try { BufferedImage bufImg = this.qRCodeCommon(content, imgType, ukuran); File imgfile = file baru (imgpath); // menghasilkan kode qr qrcode gambar gambaro.write (bufimg, imgtype, imgfile); } catch (Exception e) {E.PrintStackTrace (); }} / *** Hasilkan kode qr (qrcode) gambar* @param konten konten konten* @param output output stream* @param imgtype tipe* @param ukuran qr ukuran* / public void encoderqrcode (konten string, outputStream, string imgType, int ukuran) {try {content {outputSstream, string imgType, int ukuran) {try {content {outputSstream, string IMGType, int ukuran) {try {content {outputSstream, string IMGTYPE, int size) {try {content {BUFFEDEMAGEMAGE = outputStream, string IMGTYPE, int size) {try {content {BUFFEDEMADEMAGE = ukuran); // menghasilkan kode qr qrcode gambar gambaro.write (bufimg, imgtype, output); } catch (Exception e) {E.PrintStackTrace (); }} / *** Metode publik untuk menghasilkan kode qr (qrcode) gambar* @param konten penyimpanan konten* @param imgtype tipe* @param ukuran qr ukuran kode* @return* / private bufferedimage qrcodecommon (konten string, string imgtype, ukuran int) {bufferedimage bufimg = null; coba {qrcode qrcodeHandler = qrcode baru (); // Atur tingkat kesalahan kode QR, yang dapat dipilih sebagai L (7%), M (15%), Q (25%), dan H (30%). Semakin tinggi tingkat kesalahan, semakin sedikit informasi yang dapat disimpan, tetapi semakin kecil persyaratan untuk Kode QR yang jelas qrcodeHandler.setqrcodeErrorCorrect ('m'); qrcodeHandler.setqrcodeEncodemode ('b'); // Atur ukuran kode QR, dengan kisaran nilai 1-40. Semakin besar nilainya, semakin besar ukurannya, semakin besar informasi yang dapat Anda simpan qrcodeHandler.setqrcodeversion (ukuran); // Dapatkan array byte konten dan atur format pengkodean byte [] contentBytes = content.getbytes ("UTF-8"); // ukuran gambar int imgsize = 67 + 12 * (ukuran - 1); bufimg = BufferedImage baru (imgsize, imgsize, bufferedImage.type_int_rgb); Grafik2d gs = bufimg.creategraphics (); // atur latar belakang warna gs.setBackground (color.white); gs.clearrect (0, 0, imgsize, imgsize); // atur gambar gambar> gs.setcolor hitam (color.black); // Setel offset, bukan pengaturan dapat menyebabkan kesalahan parsing int pixoff = 2; // Konten Output> Kode QR if (contentBytes.length> 0 && contentBytes.length <800) {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 {lempar pengecualian baru ("Qrcode Content Bytes Length =" + ContentBytes.Length + "Tidak di [0, 800]."); } gs.dispose (); bufimg.flush (); } catch (Exception e) {E.PrintStackTrace (); } return bufimg; } / ** * Analisis kode qr (qrcode) * @param imgpath path * @return * / public string decoderqrcode (string imgpath) {// qrcode qr kode file gambar imagefile = file baru (imgpath); BuferedImage bufimg = null; Konten String = NULL; coba {bufimg = imageIo.read (imagefile); Qrcodedecoder decoder = qrcodedecoder baru (); content = new string (decoder.decode (twoDimensionCodeImage baru (bufimg)), "UTF-8"); } catch (ioException e) {System.out.println ("error:" + e.getMessage ()); e.printstacktrace (); } catch (decodingfailedException dfe) {system.out.println ("error:" + dfe.getMessage ()); dfe.printstacktrace (); } mengembalikan konten; } / ** * Parsing qr code (qrcode) * @param input stream input * @return * / public string decoderqrcode (inputStream input) {bufferedImage bufimg = null; Konten String = NULL; coba {bufimg = imageIo.read (input); Qrcodedecoder decoder = qrcodedecoder baru (); content = new string (decoder.decode (twoDimensionCodeImage baru (bufimg)), "UTF-8"); } catch (ioException e) {System.out.println ("error:" + e.getMessage ()); e.printstacktrace (); } catch (decodingfailedException dfe) {system.out.println ("error:" + dfe.getMessage ()); dfe.printstacktrace (); } mengembalikan konten; } public static void main (string [] args) {string imgpath = "g: /tddownload/michael_qrcode.png"; String EncoderContent = "Halo besar, kecil, selamat datang di QRCode!" + "/nmyblog [http://sjsky.iteye.com]" + "/nemail [[email protected]]"; TwoDimensionCode Handler = twoDimensionCode baru (); handler.encoderqrcode (encodercontent, imgpath, "png"); // coba {// outputStream output = new fileoutputStream (imgpath); // handler.encoderqrcode (konten, output); //} catch (pengecualian e) {// e.printskode (konten, output); //} catch (pengecualian e) {/ e.printStack (output); //} (Exception e) {// e.printStack (output); System.out.println ("============= Success Encoder"); String decodercontent = handler.decoderqrcode (imgpath); System.out.println ("Hasil parsing adalah sebagai berikut:"); System.out.println (DecoderContent); System.out.println ("============ Decoder Success !!!"); }}Kelas TwoDimensionCodeImage: Objek Gambar Kode QR
Paket qrcode; import java.awt.image.bufferedImage; import jp.sourceForge.qrcode.data.qrcodeimage; kelas publik TwoDimensionCodeImage mengimplementasikan qrcodeimage {buferedimage bufimg; TwoDimensionCodeImage publik (bufferedImage bufimg) {this.bufimg = bufimg; } @Override public int getHeight () {return bufimg.getHeight (); } @Override int getPixel (int x, int y) {return bufimg.getrgb (x, y); } @Override public int getWidth () {return bufimg.getWidth (); }}Di atas adalah informasi untuk generasi dan analisis kode Java QR. Kami akan terus menambahkannya nanti. Terima kasih atas dukungan Anda untuk situs web ini!