QRコードはどこにでもあり、スキャンすることができれば贈り物が得られます。 QRコードは現在とても人気があります。 QRコードがどのように生成されるかについて誰もがそれほど明確ではないと思います。これで、編集者は、この記事を共有してQRコード生成方法を学習するのを支援する方法をお客様と共有します。
実際、この関数は、主にGoggleが公開したJARを使用して使用されます。
1。QRコードの生成
zxing-core.jarパッケージをClassPathに追加します。
QRコードの生成には、MatrixToImageWriterクラスの助けが必要です。このクラスはGoogleによって提供されます。クラスをソースコードにコピーできます。ここでは、このクラスのソースコードを貼り付け、直接使用できます。
QRコードを直接生成できるコード
public void test1()throws exception {string content = "www.baidu.com";文字列パス= "d://"; multiformatwriter multiformatwriter = new multiformatwriter(); // zxingはGoogleが提供するバーコードです。 Map Hints = new Hashmap(); hints.put(encodehinttype.character_set、 "utf-8"); BitMatrix BitMatrix = multiformatwriter.encode(content、barcodeformat.qr_code、400、400、hints); //これは写真ファイルのサイズ= newファイル(path、 "my.jpg"); matrixtoimagewriter.writetofile(bitmatrix、 "jpg"、file1); System.out.println( "実行完了"); }このコードをコピーした後、MatrixToImageWriterによって報告されたエラーがあることがわかったので、探す必要がありますが、ここにコードを投稿して直接使用できます。
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); }}}これで、Dディスクのルートディレクトリをダウンロードして、生成されたQRコードを表示できます
QRコード分析
Generationと同様に、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)を返します。 }}QRコードを解析するコード
MultiformatReader FormatReader = new MultiformatReader();文字列filepath = "画像のパス"; 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()); tch(例外e){e.printstacktrace();このようにして、コンソール上のQRコードのコンテンツを見ることができます。
上記は、Zxing-CoreによるQRコードを生成し、それらを分析する方法に関する関連する知識です。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!