1。QRCODEおよびZXING QRコードの生成と分析をサポートします。
2。QRCODEはQRコードを生成し、次のように写真の追加をサポートします。
パッケージ共通。 java.awt.color; Import java.awt.graphics2d; import java.awt.image; image java.awt.image.bufferedimage; import java.io.ioexception; Import java.util.hashmap; Import java.util.util.util.util.util.util.util.util.util.util.magmap; jp.sourceforge.qrcode.qrcodedeCoder; import jp.sourceforge.qrcode.exception.decodingfailedexception; com.google.zxing.barcodeformat;インポートcom.google.zxing.binarizer; import com.google.zxing.binarybitmap; import com.google.zxing.encodehinttype; Import com.google.zxing.luminancesource; import com.google.zxxing.multiortideder; com.google.zxing.multiformatwriter; import com.google.zxing.notfoundexception; import com.google.zxing.writerexception; import com.google.zxing.common.common.bitmatrix; Import com.google.zxing.common.hybridbinarizer; /** * QRコード生成ツールクラス * @Author Cloud * @Data 2016-12-15 * QRCode */public class qrcodeutil {// qrコードカラープライベート静的final int black = 0xff000000; // QRコードカラープライベート静的ファイナルint white = 0xffffffff; /** * <span style = "font-size:18px; font-weight:blod;"> zxingメソッド生成qr code </span> * @param text <a href = "javascript:void();" rel = "external nofollow"> qrコードコンテンツ</a>* @param width qr code width* @param height qr code height* @param outputpath qrコード生成とパスの保存his = new hashmap <encodehinttype、string>(); //エンコードされた文字を設定します。HIS.put(encodehinttype.character_set、 "utf-8"); {// 1を試してください。 qr code bitmatrix encode = new MultiformatWriter()。encode(text、barcodeformat.qr_code、width、height、his)を生成します。 // 2。 QRコード幅と高さを取得しますint codewidth = encode.getWidth(); int codeheight = encode.getheigh(); // 3。 QRコードをバッファーストリームBufferEdImage Image = new BufferEdimage(codewidth、codeheight、bufferedimage.type_int_rgb)に入れます。 for(int i = 0; i <codewidth; i ++){for(int j = 0; j <codeheight; j ++){// 4。 QRコードコンテンツを画像に固定するためのループimage.setrgb(i、j、encode.get(i、j)?black:white); }} file outputimage = new file(outputPath); //画像が存在しない場合、if(!outputimage.exists())outputimage.createNewfile(); // 5。 QRコードをImageIo.Write(Image、ImageType、outputImage)に書き込みます。 } catch(writerexception e){e.printstacktrace(); System.out.println( "QRコード生成が失敗しました"); } catch(ioexception e){e.printstacktrace(); system.out.println( "qrコードイメージの変更に失敗しました"); }}/** * <Span style = "font-size:18px; font-weight:blod;"> qrコード分析</span> * @param Analyzepath qr code path * @throws ioexception */@suppresswarnings({"rawtypes"、 "Unched"例外{multiformatreader formatreader = new MultiformatReader();オブジェクト結果= null; try {file file = new file(analyzepath); if(!file.exists()){return "qrコードは存在しません"; } bufferedimage image = imageio.read(file); luminanceSource source = new luminanceSourceutil(画像);バイナライザーバイナライザー=新しいハイブリッドビナライザー(ソース); binarybitmap binarybitmap = new binarybitmap(binarizer); Map Hints = new Hashmap(); hints.put(encodehinttype.character_set、 "utf-8"); result = formatreader.decode(binarybitmap、hints); } catch(notFoundException e){e.printstacktrace(); } return result; }/*** <span style = "font-size:18px; font-weight:blod;"> qrcode生成qr code </span>* @param content qr code content* @param imgpath qr codeジェネレーションパス* @paramバージョンqr codeバージョン* @param isflag imgpath、intバージョン、string logopath){try {qrcode qrcodehandler = new qrcode(); // QRコードエラーデコードレート、オプションのL(7%)M(15%)Q(25%)H(30%)を設定します。エラーセコードレートが高いほど、保存できる情報は少なくなりますが、QRコードQRCODEHANDLER.SETQRCODEERRORCORRECT( 'M')の明確性の要件が小さくなります。 // nは数を表し、aは文字azを表し、bは他の文字qrcodehandler.setqrcodeencodemode( 'b')を表します。 //バージョン1は21*21マトリックスであり、バージョンの1つの増加ごとに、QRコードの2つの側面が4増加します。したがって、バージョン7は45*45のマトリックスです。最高バージョンは40です。これは177*177 QRCODEHANDLER.SETQRCODEVERSION(バージョン)を持つマトリックスです。 //バージョンに従ってサイズを計算しますint imgsize = 67 + 12 *(バージョン-1); byte [] contentBytes = content.getBytes( "gb2312"); bufferedimage bufimg = new bufferedimage(imgsize、imgsize、bufferedimage.type_int_rgb); graphics2d gs = bufimg.creategraphics(); Gs.setbackground(color.white); gs.ClearRect(0、0、imgsize、imgsize); //イメージカラーを設定>ブラックgs.setcolor(color.black); //設定しないオフセットを設定することは、解析エラーを引き起こす可能性がありますint pixoff = 2; // output content> qr code if(contentbytes.length> 0 && contentbytes.length <130){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 {system.err.println( "qrcode content bytes lengtes =" + contentbytes.length + "not in [0,130]。"); } /*ロゴイメージを追加する必要があるかどうかを判断します* / if(logopath!= null){file icon = new file(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;画像img = imageio.read(icon); gs.drawimage(img、width_4 + width_8、height_4 + height_8、width_4、height_4、null); gs.dispose(); bufimg.flush(); } else {system.out.println( "エラー:ログインイメージは存在しません!"); }} gs.dispose(); bufimg.flush(); // QRコードファイルを作成しますimgfile = new File(imgpath); if(!imgfile.exists())imgfile.createNewfile(); //生成された画像文字列から画像を取得するimgtype = imgpath.substring(imgpath.lastindexof( "。") + 1、imgpath.length()); // QRコードqrcode imageio.write(bufimg、imgtype、imgfile)を生成します。 } catch(Exception e){e.printstacktrace(); }}/*** <span style = "font-size:18px; font-weight:blod;"> qrcode qrコード分析</span>* @param codepath qr code path* @return parsing result*/public string string qrcodeanalyze(string codepath){file yagefilis(codepath); bufferedimage bufimg = null;文字列decodedData = null; try {if(!imagefile.exists())return "qrコードは存在しません"; bufimg = imageio.read(imagefile); QRCODEDECODER DECODER = new qrcodedeCoder(); decodedData = new String(decoder.decode(new ImageUtil(bufimg))、 "gb2312"); } catch(ioexception e){system.out.println( "error:" + e.getmessage()); e.printstacktrace(); } catch(decodingfailedexception dfe){system.out.println( "error:" + dfe.getmessage()); dfe.printstacktrace(); } decodedDataを返します。 }} 3。最後の投稿テストコード:パッケージテスト。 java.awt.image.bufferedimage; import java.io.inputStream; Import java.net.url; javax.imageio.imageioをインポートします。 common.imageutilをインポートします。 common.qrcodutilをインポートします。 jp.sourceforge.qrcode.qrcodedeCoderをインポートします。 /*** QRコード生成テストクラス* @Authorクラウド* @Data 2016-11-21* QRCODETEST* /public class qrcodetest {public static void main(string [] args)throws exception { /*** qrcode qrコード生成テスト* qrcodeutil.qrcodecreate( "http://blog.csdn.net/u014266877"、 "e://qrcode.jpg"、15、 "e://icon.png"); *//*** QRCODE QRコード分析テスト*文字列QRCODEANALYZE = QRCODEUTIL.QRCODEANALYZE( "e://qrcode.jpg"); *//*** ZxingCode QRコード生成テスト* QRCODEUTIL.ZXINGCODECREATE( "http://blog.csdn.net/u014266877"、300、300、 "e://zxingcode.jpg"、 "jpg"); *//*** ZxingCode QRコード分析* String ZXINGANALYZE = QRCODEUTIL.ZXINGCODEANALYZE( "e://zxingcode.jpg").toString(); */ system.out.println( "success"); }}