QRコードは、黒と白の平面の幾何学的図を使用して、対応するコーディングアルゴリズムを介してテキスト、写真、URL、その他の情報を記録するバーコード画像です。次の図
QRコードの機能:
1。高密度コーディング、大きな情報容量
最大1850の大文字、2710の数字または1108バイト、または500を超える漢字に対応できます。これは、通常のバーコード情報よりも約数十倍高いです。
2。広いエンコーディング範囲
このバーコードは、写真、サウンド、テキスト、署名、指紋などのデジタル化された情報をエンコードし、バーコードで表現できます。複数の言語でテキストを表すことができます。そして、それらは画像データを表すことができます。
3。強い断層のトレランスとエラー補正機能
これにより、2次元バーコードを穿孔、汚染などにより適切に損傷することができ、損傷領域が50%に達すると情報を回復できます。
4.高解読の信頼性
これは、100万当たりの2つのパートの通常のバーコードデコードのエラー率よりもはるかに低く、エラー率は100万分の1パート以下です。
5。暗号化測定を導入できます
優れた機密性と争い防止。
6.低コスト、簡単に作成し、耐久性があります
上記の特性のため、QRコードはますます人気が高まり、より広く使用されています(詳細については、Baidu Encyclopediaを参照してください。紹介はこの記事の焦点ではありません)。したがって、QRコードの開発方法を習得することは、非常に優れた知識保護区です。したがって、この記事では、QRコードの生成と分析方法について説明します。
1。ジャワ
必要なJARパッケージ:qrcode.jar
http://sourceforge.jp/projects/qrcode/
TwodimensionCodeクラス:QRコード操作コアクラス
パッケージqrcode; import java.awt.color; import java.awt.graphics2d; import java.awt.image.bufferedimage; Import java.io.io.ioexception; Import java.io.inputstream; Import java.io.Outputstream; Import Javax.imageioio; jp.sourceforge.qrcode.qrcodededecoder; Import jp.sourceforge.qrcode.exception.decodingfailedexception; Import com.swetake.util.qrcode; public class twodimensioncode { / *** generate qr code(qrcode)写真encoderqrcode(string content、string imgpath){this.encoderqrcode(content、imgpath、 "png"、7); } / *** qrコード(qrcode)画像を生成* @paramコンテンツストアコンテンツ* @param出力出力ストリーム* / public void encoderqrcode(string content、outputstream output){this.encoderqrcode(content、output、 "png"、7); } / *** qrコード(qrcode)picture* @paramコンテンツストアコンテンツ* @param imgpath picture path* @param imgtype picture type* / public void encoderqrcode(string content、string imgpath、string imgtype){this.encoderqrcode(content、imgtype、7); } / *** qrコード(qrcode)picture* @paramコンテンツストアコンテンツ* @param出力出力ストリーム* @param imgtype画像タイプ* / public void encoderqrcode(string content、outputstream output、string imgtype){this.encoderqrcode(content、output、imgtype、7); } / *** qrコード(qrcode)画像を生成* @paramコンテンツストアコンテンツ* @param imgpath picture path* @param imgtype image Type* @param size qr code size* / public void encoderqrcode(文字列Imgpath、文字列imgtype、intサイズ) サイズ);ファイルimgfile = new file(imgpath); // QRコードqrcode imageio.write(bufimg、imgtype、imgfile)を生成します。 } catch(Exception e){e.printstacktrace(); }} / *** qrコード(qrcode)画像を生成* @paramコンテンツストアコンテンツ* @param出力出力ストリーム* @param imgtype画像タイプ* @param size qr code size* / public void encoderqrcode(string content、outputstream output、string imgtype、content {try {bufferedimage bufimmon imgtype、size); // QRコードqrcode imageio.write(bufimg、imgtype、output)を生成します。 } catch(Exception e){e.printstacktrace(); }} / *** qrコード(qrcode)画像を生成するパブリックメソッド* @paramコンテンツストアコンテンツ* @param imgtype画像タイプ* @param size qr code size* @return* / private bufferedimage qrcodecommon(string content、string imgtype、int size){bufferedimage bufimg = null; try {qrcode qrcodehandler = new qrcode(); // QRコードエラー率を設定します。これは、L(7%)、M(15%)、Q(25%)、およびH(30%)として選択できます。エラー率が高いほど、保存できる情報は少なくなりますが、QRコードClarity QRCodeHandler.SetQRCodeErrorCorrect( 'M')の要件が小さくなります。 qrcodehandler.setqrcodeencodemode( 'b'); // 1〜40の値範囲でQRコードサイズを設定します。値が大きいほど、サイズが大きくなるほど、qrcodehandler.setqrcodeversion(size)を保存できる情報が大きくなります。 //コンテンツのバイト配列を取得し、エンコード形式のバイト[] contentBytes = content.getBytes( "utf-8"); //画像サイズint imgsize = 67 + 12 *(size -1); bufimg = new bufferedimage(imgsize、imgsize、bufferedimage.type_int_rgb); graphics2d gs = bufimg.creategraphics(); //背景色gsetbackground(color.white)を設定します。 gs.ClearRect(0、0、imgsize、imgsize); //イメージカラーを設定>ブラックgs.setcolor(color.black); //オフセットを設定すると、設定ではなく、解析エラーを引き起こす可能性があります。 // output content> qrコードif(contentbytes.length> 0 && contentbytes.length <800){boolean [] [] codeout = qrcodehandler.calqrcode(contentbytes); for(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 {throw new Exception( "qrcode content bytes length =" + contentbytes.length + "not in [0、800]。"); } gs.dispose(); bufimg.flush(); } catch(Exception e){e.printstacktrace(); } bufimgを返します。 } / ** * qrコード(qrcode)を分析します * @param imgpathイメージパス * @return * / public string decoderqrcode(string imgpath){// qrcode qr code image image file file file file file file = newファイル(imgpath); bufferedimage bufimg = null;文字列コンテンツ= null; try {bufimg = imageio.read(imageFile); QRCODEDECODER DECODER = new qrcodedeCoder(); content = new String(decoder.decode(new TwodimensionCodeImage(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(); } return content; } / ** * qrコード(qrcode) * @param入力ストリーム * @return * / public string decoderqrcode(inputstream input){bufferedimage bufimg = null;文字列コンテンツ= null; try {bufimg = imageio.read(input); QRCODEDECODER DECODER = new qrcodedeCoder(); content = new String(decoder.decode(new TwodimensionCodeImage(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(); } return content; } public static void main(string [] args){string imgpath = "g:/tddownload/michael_qrcode.png";文字列encoderContent = "こんにちは、小さい、qrcodeへようこそ!" + "/nmyblog [http://sjsky.iteye.com]" + "/nemail [[email protected]]"; TwodimensionCode Handler = new TwodimensionCode(); Handler.EncoderQRCode(encoderContent、imgpath、 "png"); // try {// outputStream output = new fileoutputStream(imgpath); // handler.encoderqrcode(content、output); //} catch(例外E){// System.out.println( "======================= string decodercontent = handler.decoderqrcode(imgpath); System.out.println( "解析結果は次のとおりです。"); System.out.println(decodercontent); System.out.println( "======================== !!!"); }}TwodimensionCodeImageクラス:QRコード画像オブジェクト
パッケージqrcode; import java.awt.image.bufferedimage; Import jp.sourceforge.qrcode.data.qrcodeimage;パブリッククラスTwodimensionCodeImageはqrcodeimage {bufferedimage bufimg; public TwodimensionCodeImage(bufferedimage bufimg){this.bufimg = bufimg; } @Override public int getheight(){return bufimg.getheigh(); } @Override public int getPixel(int x、int y){return bufimg.getrgb(x、y); } @Override public int getWidth(){return bufimg.getWidth(); }}上記は、Java QRコードの生成と分析に関する情報の編集です。後で追加し続けます。このウェブサイトへのご支援ありがとうございます!