2次元コードは、現在、日常の開発では、どこでも見ることができたとき、それは多くの場合、2次元コードを生成伴うだろう、活動やプロモーションのいくつかの特に開発が特長
2次元コードも必携の特徴点になります。この記事では、あなたの参考のために、Googleにzxingことにより、ロゴパケットで二次元コードを生成するプロセスを説明します。
図1に示すように、二次元コード生成
1.1 Zxing-core.jarパッケージには、クラスパスに追加しました。GitHubのを介して利用可能なhttps://github.com/zxing
1.2二次元コード生成手段は、Googleによって提供されるMatrixToImageWriterクラスを、必要、およびクラスは、ソースコード、Iペーストクラスは、直接使用することができ、ソースコードにコピーすることができます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
パッケージ cn.guoqick.zxing;
輸入 com.google.zxing.BarcodeFormat。 輸入 com.google.zxing.EncodeHintType。 輸入 com.google.zxing.MultiFormatWriter; 輸入 com.google.zxing.client.j2se.MatrixToImageWriter; 輸入 com.google.zxing.common.BitMatrix; 輸入 com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
インポートのjava.io.File; 輸入 java.nio.file.Path; 輸入 java.util.HashMapを;
パブリック クラス { 公共静的ボイドメイン(文字列[]引数) { int型幅= 300。int型高さ= 300 ; 文字列フォーマット= 「PNG」。 文字列の内容= 「www.guoqick.cn」。 HashMapのヒント= 新しい HashMapの(); hints.put(EncodeHintType.CHARACTER_SET、"UTF-8")。 hints.put(EncodeHintType.ERROR_CORRECTION、ErrorCorrectionLevel.M)。 hints.put(EncodeHintType.MARGIN、2)。
トライ { BitMatrix bitMatrix = 新しい MultiFormatWriter()エンコード(コンテンツ、BarcodeFormat.QR_CODE、幅、高さ、ヒント)。 パスファイル= 新しいファイル("E:/code/img.png").toPath(); MatrixToImageWriter.writeToPath(bitMatrix、フォーマット、ファイル)。 } キャッチ(例外e){ e.printStackTrace(); }
} }
|
図2に示すように、二次元コードの解析
2.1 Zxing-core.jarパッケージには、クラスパスに追加しました。
2.2および生成された、我々は、Googleはまた、同じクラスを提供し、ヘルパークラス(BufferedImageLuminanceSource)を必要とする、私はソースコードを掲載同じクラスでは、あなたは見つけることは、トラブルを解消、直接コピーを使用することができます
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61である 62は 63である 64 65 66 67 68 69 70 71であり、 72 73である 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91である 92 93 94 95 96 大列 zxing二次元コード生成によって 97 98 99
|
cn.guoqick.zxingパッケージ;
輸入java.awt.Graphics2D; 輸入java.awt.geom.AffineTransformの。 輸入java.awt.image.BufferedImage;
輸入com.google.zxing.LuminanceSource。
パブリッククラスBufferedImageLuminanceSourceはLuminanceSourceを{延び
民間最終BufferedImageのイメージ。 民間最終int型は左。 民間最終int型のトップ。 公共BufferedImageLuminanceSource(バッファ画像イメージ){ この(イメージ、0、0、image.getWidth()、image.getHeight())。 } 公共BufferedImageLuminanceSource(バッファ画像イメージ、intは、INTトップ、INT幅、INT高左){ スーパー(幅、高さ)。 INT sourceWidth = image.getWidth()。 INT sourceHeight = image.getHeight()。 (左+幅> sourceWidth ||トップ+高さ> sourceHeightが){場合 新しいIllegalArgumentExceptionをスローし(「クロップ矩形は、画像データに収まりません。」); } {(; Y <トップ+高Y ++のint Y =最上位)のために
用(INT X =左; X <左+幅; X ++){ IF((image.getRGB(X、Y)&0xFF000000)== 0){ image.setRGB(X、Y、0xFFFFFFFFの); // =白 } } } this.image =新しいBufferedImageの(sourceWidth、sourceHeight、BufferedImage.TYPE_BYTE_GRAY)。 this.image.getGraphics()のdrawImage(イメージ、0、0、NULL)。 this.left =左; this.top =トップ。 } @Override 公共バイト[]のgetRow(INT yを、バイト[]行){ IF(Y <0 || Y> =のgetHeight()){ 新をスロー( "要求された行が画像の外にある" + Y)。 } INT幅=のgetWidth()。
IF(行== NULL || row.length <幅){ 列=新しいバイト[幅]。 } image.getRaster()getDataElements(左、トップ+ yを、幅、1行)。 行を返します。 } @Override 公共バイト[] getMatrix(){ int型幅=のgetWidth()。 INT高さ=のgetHeight(); int型の面積=幅*高さ。 バイト[]マトリックス=新しいバイト[領域]。 image.getRaster()getDataElements(左、上、幅、高さ、マトリックス)。 行列を返します。 } @Override isCropSupportedブールパブリック(){ trueを返します。 } @Override
公共LuminanceSource作物(intは、INTトップ、INT幅、INT高さを残した){ 新しいBufferedImageLuminanceSource返す(画像を、this.left +は、this.top +上、幅、高さを残しました)。 } @Override パブリックブールisRotateSupported(){ trueを返します。 } @Override 公共LuminanceSource rotateCounterClockwise(){ int型sourceWidth = image.getWidth()。 INT sourceHeight = image.getHeight()。 AffineTransform変換=、新しいAffineTransform(0.0、-1.0、1.0、0.0、0.0、sourceWidth)。 BufferedImageのrotatedImage =新しいBufferedImageの(sourceHeight、sourceWidth、BufferedImage.TYPE_BYTE_GRAY)。 Graphics2D G = rotatedImage.createGraphics()。
g.drawImage(画像、変換、NULL); )(g.dispose。 INT幅=のgetWidth()。 新しいBufferedImageLuminanceSource(rotatedImage、トップ、sourceWidth - (左+幅)、のgetHeight()、幅)を返します。 }
}
|
2.3ライト・コード、その実装分析、二次元コード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
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(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); Result result = formatReader.decode(binaryBitmap, hints);
System.out.println( "結果=" + result.toString())。 System.out.println( "のresultFormat =" + result.getBarcodeFormat())。 System.out.println( "resultText =" + result.getText())。
}キャッチ( 例外e) { e.printStackTrace(); }
|
この時点で、全体の二次元コード生成および解決プロセスは賞賛の少しのように、上です。。。。