Zxingは、Googleが提供するバーコード(One-Rコード、QRコード)に関する解析ツールです。 QRコードを生成および分析する方法を提供します。次に、Zxingを使用してQRコードを生成および分析するためにJavaの使用を簡単に紹介します。
1。QRコードの生成
1.1 Zxing-Core.jarパッケージをClassPathに追加します。
1.2 QRコードの生成には、MatrixToImageWriterクラスの助けが必要です。このクラスはGoogleによって提供されます。クラスをソースコードにコピーできます。ここでは、このクラスのソースコードを貼り付け、直接使用できます。
com.google.zxing.common.bitmatrixをインポートします。 javax.imageio.imageioをインポートします。 java.io.fileをインポートします。 java.io.outputStreamをインポートします。 java.io.ioexceptionをインポートします。 java.awt.image.bufferedimageをインポートします。パブリックファイナルクラスmatrixtoimagewriter {private static final int black = 0xff000000;プライベート静的final int white = 0xffffffff; private matrixtoimagewriter(){} public static bufferedimage tobufferedimage(bitmatrix matrix){int width = matrix.getWidth(); int height = matrix.getheight(); bufferedimage image = new BufferedImage(幅、高さ、bufferedimage.type_int_rgb); for(int x = 0; x <width; x ++){for(int y = 0; y <height; y ++){image.setrgb(x、y、matrix.get(x、y)?black:white); }} return image; } public static void writetofile(bitmatrix matrix、string format、file file)throws ioexception {bufferedimage image = tobufferedimage(matrix); if(!imageio.write(image、format、file)){throw new ioexception( "format" + format + "to" + file); }} public static void writetostream(bitmatrix matrix、string format、outputstream stream)をスローIoexception {bufferedimage image = tobufferedimage(matrix); if(!imageio.write(image、format、stream)){throw new ioException( "フォーマットの画像を書くことができませんでした" + format); }}}1.3 QRコードを生成する実装コードを書き込みます
try {string content = "120605181003; http://www.cnblogs.com/jtmjx"; string path = "c:/users/administrator/desktop/testimage"; multiformatwriter multiformatwriter = new multiformatwriter(); Map Hints = new Hashmap(); hints.put(encodehinttype.character_set、 "utf-8"); BitMatrix BitMatrix = multiformatwriter.encode(content、barcodeformat.qr_code、400、400、hints); file file1 = new file(path、 "nambeginner.jpg"); matrixtoimagewriter.writetofile(bitmatrix、 "jpg"、file1); } catch(Exception e){e.printstacktrace(); }実行後、QRコード画像を生成できます。とても簡単ではありませんか?次に、QRコードを解析する方法を見てみましょう
2。QRコードの分析
2.1 Zxing-Core.jarパッケージをClassPathに追加します。
2.2世代と同様に、Googleが提供する補助クラス(BufferedimageluminanceSource)が必要です。ここでは、このクラスのソースコードを貼り付けて、直接コピーして使用して、検索のトラブルを節約できます。
BufferedimageluminasanceSourceインポートcom.google.zxing.luminasanceource; java.awt.graphics2dをインポートします。 java.awt.geom.affinetransformをインポートします。 java.awt.image.bufferedimageをインポートします。パブリックファイナルクラスbufferedimageluminanceSourceは、luminanceSourceを拡張します{プライベートファイナルバッファリダイムイメージ。プライベートファイナルINT左;プライベートファイナルイントトップ。 public bufferedimageluminanceSource(bufferedimage image){this(image、0、0、image.getWidth()、image.getheigh()); } public bufferedimageluminanceSource(bufferedimage Image、int left、int top、int width、int height){super(width、height); int sourcewidth = image.getWidth(); int sourceheight = image.getheigh(); if(left + width> sourcewidth || top + height> sourceheight){新しいIllegalargumentException( "Crop Rectangleは画像データに適合しません。"); } for(int y = top; y <top+height; y ++){for(int x = left; x <left+width; x ++){if((image.getrgb(x、y)&0xff000000)== 0){image.setrgb(x、y、0xfffffff); // = white}}} this.image = new BufferedImage(sourceWidth、sourceHeight、bufferedimage.type_byte_gray); this.image.getGraphics()。drawimage(image、0、0、null); this.left = left; this.top = top; } @Override public byte [] getRow(int y、byte [] row){if(y <0 || y> = getheight()){throw new IllegalargumentException( "要求の行は画像の外側にあります:" + y); } int width = getWidth(); if(row == null || row.length <width){row = new byte [width]; } image.getRaster()。getDataelements(左、上 + y、幅、1、行); return行; } @Override public byte [] getMatrix(){int width = getWidth(); int height = getheight(); intエリア=幅 *高さ; byte [] matrix = new byte [area]; image.getRaster()。getDataelements(左、上、幅、高さ、マトリックス);戻るマトリックス; } @Override public boolean iscropsupported(){return true; } @Override public luminanceSource Crop(int left、int top、int width、int height){return new bufferedimageluminanceSource(image、this.this.left + left、this.top + top、width、height); } @Override public boolean isrotatesupported(){return true; } @Override public luminanceSource rotatecounterclockwise(){int sourcewidth = image.getWidth(); int sourceheight = image.getheigh(); affintransform transform = new affinetransform(0.0、-1.0、1.0、0.0、0.0、SourceWidth); bufferedimage rotedimage = new bufferedimage(sourceheight、sourcewidth、bufferedimage.type_byte_gray); graphics2d g = rotedimage.creategraphics(); G.drawimage(画像、変換、null); g.dispose(); int width = getWidth();新しいbufferedimageluminanceSource(rotedimage、top、sourcewidth-(左 +幅)、getheight()、width)を返します。 }}2.3 QRコードを解析する実装コードを書き込みます
try {multiformatreader formatreader = new MultiformatReader(); string filepath = "c:/users/administrator/desktop/testimage/test.jpg"; file file = new file(filepath); bufferedimage image = imageio.read(file);; luminanceSource source = new BufferedimageluminanceSource(画像);バイナライザーバイナライザー=新しいハイブリッドビナライザー(ソース); binarybitmap binarybitmap = new binarybitmap(binarizer); Map Hints = new Hashmap(); hints.put(encodehinttype.character_set、 "utf-8");結果result = formatreader.decode(binarybitmap、hints); System.out.println( "result ="+ result.toString()); system.out.println( "resultFormat ="+ result.getBarCodeFormat()); System.out.println( "resterText ="+ result.getText()); } catch(Exception e){e.printstacktrace(); }実行後、コンソールがQRコードのコンテンツを印刷することがわかります。
これまでのところ、ZxingはZxingを使用してQRコードを生成および解析し、デモンストレーションが完了し、主に自分のためにメモを作成し、同時に困っている人に便利になりました。 hehe!
Zxingを使用してQRコードを生成するJavaの上記の簡単な例は、私があなたと共有するすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。