Java Designを使用してQRコードを生成するために段階的に教えてください
モノのインターネットの時代では、QRコードは非常に重要なことです。さて、あなたが何であろうと、QRコードロゴを取得する必要があります。時代遅れになることを恐れて、QRコードを使用してそれを識別することはできません。たぶん、いつか出生証明書または世帯登録帳がQRコードで識別されるでしょう。今日、私は気まぐれで、他の人が彼らのブログにQRコードを追加したことを見ました。また、私もテストをしたかったのです。
主に2つのポイントを達成するために使用されます。
1.任意のテキストでQRコードを生成します。
2. QRコードの中央に画像を追加します。
1。準備作業。
QR QRコード3.0コアパッケージとJPG画像を準備します。
QR QRコードパッケージをダウンロードします。
まず、Zxing.jarパッケージをダウンロードする必要があります。ここで3.0のコアパッケージを使用しています
アドレスをダウンロード:Githubに移動されました:https://github.com/zxing/zxing/wiki/getting-developing、
もちろん、MavenリポジトリのJARパッケージをダウンロードすることもできます:http://central.maven.org/maven2/com/google/zxing/core/
2。プログラミング
1. Eclipseを開始し、新しいJavaプロジェクトを作成し、プロジェクト名を見つけます(この例では、qrcodesoftに名前を付けます)。 [次へ]をクリックします:
2。zxing.jarパッケージをインポートします。ここでは、3.0のコアパッケージを使用しています。「外部jar(x)...」をクリックします。
3。つまり、2つの新しいクラスを作成します。
bufferedimageluminanceource.java
qrcodeutil.java
重要なコードは、BufferedimageluminanceSource.javaおよびqrcodeutil.javaです。テストした主な方法はqrcodeutil.javaにあります。
bufferedimageluminanceSource.javaプログラムコード:
パッケージcom.yihaomen.barcode; import java.awt.graphics2d; import java.awt.geom.affinetransform; Import java.awt.image.bufferedimage; import com.zxing.luminanceource; public class bufferedimageluminage intext extends lignanceource Top; public BufferedimageluminanceSource(bufferedimage Image){this(image、0、0、image.getWidth()、image.getheight());} public bufferedimageluminanceSource(bufferedimage image、bufferedimage image、int left、int top、int width、) image.getheight(); if(left + width> sourcewidth || height> height> sourceheight){throw new IllegalargumentException( "Crop Rectangleは画像データに適合しません。");} 0xff000000)== 0){image.setrgb(x、y、0xffffff); // = white}}} thimage = new bufferedimage(sourcewidth、sourceheight、bufferedimage.type_byte_gray); this.image.getGraphics()。ドローイメージ(画像、0、0、null); y> = getheigh()){新しいIllegalargumentException( "要求の行は画像の外側にあります:" + y);} int width = getWidth(); if(row = null || low.length <width){row = new byte [width];} image.getraster() byte [] getMatrix(){int width = getWidth(); int height = getheight(); int area = width * height; byte = new byte [] matrix = new.getRaster()。 crop(int left、int top、int width、int height){新しいbufferedimageluminanceSource(画像、左 +左、this.top + top、width、height);} public boolean isrotatesupported(){return true;} public luminanceSource rotateccountclockwise(){int source = sures = getwidth(); image.getheight(); affinetransform transform = new affintransform(0.0、-1.0、1.0、0.0、sourceWidth); bufferedimage rotedimage = new bufferedimage(sourceheight、sourcewidth、bufferedimage.type_byte_gray) null); g.dispose(); int width = getWidth(); return new BufferedimageluminanceSource(RotatedImage、Top、SourceWidth-(左 +幅)、Getheight()、width);}}}qrcodeutil.javaプログラムコード:
パッケージcom.yihaomen.barcode; import java.awt.basicstroke; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; import java.awt.shape; import java.awt.geom.roundrectangrectangrectangrection2d; import java.aw.aw.awt.image java.io.file; Import java.outputstream; Import java.util.hashtable; Import java.util.random; import javax.imageio.google.zxing.barcodeformat; import com.google.zxing.binarybitmap; import com.google.xxdeded.xchedのインポートcom.google.zxing.encodehinttype; Import com.google.zxing.multifformatreader; import com.google.zxing.multifialtatwriter; Import com.google.zxing.result; import com.google.zxing.common.bitmatrix; com.google.zxing.qrcode.decoder.errorcorrectionlevel;/*** qr code tool class**/public class qrcodeutil {private static final string charset = "utf-8"; private static final string format_name = "jpg"; // qr code size size size private static int qrcode_size = 300; 60; errorCorrectionLevel.h); hints.put(encodehinttype.character_set、charset); hints.put(encodehinttype.margin、1); bitmatrix bitmatrix = new multiformatwriter()。 bitmatrix.getWidth(); int height = bitmatrix.getheight(); bufferedimage image = new bufferedimage(width、height、bufferedimage.type_int_rgb); for(int x = 0; x <width; x ++){for(int y = 0; y <height; y ++){image.setrgb(x、y、bitmatrix.get(x、y)?0xffffffff);}} if(imgpath == null || ""。 needcompress); return image;}/** * logo * * @param source * qr code picture * @param imgpath * logo pictureアドレス * @param needcompress * @throws例外 */プライベート静的insertimage(bufferedimage source、string imgpath、boolean needcompress) {system.err.println( ""+imgpath+"このファイルは存在しません!"); return;} image src = imageio.read(new file(imgpath)); int width = src.getwidth(null); int height = src.getheight(null); if(needcompress(edcompress> fid(fid) (高さ){高さ){高さ=高さ;}画像= src.getScaledInstance(幅、高さ、画像、Scale_smooth); bufferedimageタグ= new bufferedimage(width、height、bufferedimage.type_int_int_rgb);グラフィックg = tag.getgraphics(null); //縮小グラフを描画したG.dispose(); src = image;} // logographics2dグラフ= source.creategraphics(); int x =(qrcode_size -width)/ 2; int y =(qrcode_size -height)/ 2; 2; graph.drawimage(src、y、y、width、height、nullect(null); y、width、width、6、6); graph.setstroke(new basicstroke(3f)); graph.draw(shape); graph.dispose();}/** * @param content * content * @param imgpath * logo * @param destortory redidursississ * @param redispussissodussussussispussuss * content * @param imgpath * @param static void encode(string content、string imgpath、string destpath、boolean needcompress)スロー{bufferedimage image = qrcodeutil.createimage(content、imgpath、needcompress); mkdirs(destpath); string file = new Random()。 file(destpath+"/"+file);}/***フォルダーが存在しない場合、mkdirはmkdirとは異なるマルチレイヤーディレクトリを自動的に作成します。 (親ディレクトリが存在しない場合、例外がスローされます) * @author lanyuan * email:[email protected] * @date 2013-12-11 10:16:36 am * @param destpath storeディレクトリ */パブリック静的ボイドmkdirs(string destpath) //フォルダーが存在しない場合、MKDIRSはMKDIRとは異なるマルチレイヤーディレクトリを自動的に作成します。 (MKDIRは、親ディレクトリが存在しない場合に例外をスローします)if(!file.exists()&&!file.isdirectory()){file.mkdirs();}}/** * denerate qrコード(埋め込みロゴ)コンテンツ、文字列imgpath、string destpath)スロー例外{qrcodeutil.encode(content、imgpath、destpath、false);}/** * @param content * content * @param destpath * @param needcompress * @param needcompress * @go @thrups content、string ecode(string ecode(string) {qrcodeutil.encode(content、null、destpath、needcompress);}/*** @param content* content* @param destpath* @throws exception*/public static void encode(string content、string destpath)throws Exception(content、content、content、destpath、qr empr、empad、conter、conteロゴ) * @paramコンテンツ *コンテンツ * @param imgpath *ロゴアドレス * @param出力 *出力ストリーム * @param needcompress * @param needcompress * @throws例外 */public static void encode(string content、string imgpath、outputstream output、boolean needcompress) needCompress);ImageIO.write(image, FORMAT_NAME, output);}/** * Generate QR code* * @param content * Content* @param output * Output stream* @throws Exception */public static void encode(String content, OutputStream output)throws Exception {QRCodeUtil.encode(content, null, output, false);}/** * Analyze QR code* * @param file * qr code image * @return * @throws例外 */public static string decode(file file)shrows {bufferedimage image; image = imageio.read(file); if(image == null){return null;} bufferedimageluminancessource source = new bufferedimageluminanceource(emage bitmap = new bitmap bitmap = new bitmap = new bitmap = new bitmap = HybridBinarizer(source)); result result; hashtable <decodehinttype、object> hints = new hashtable <decodehinttype、object>(); hints.put(decodehinttype.character_set、charset); result = new multiformatreader(); resultStr;}/** * qr code * * @param path * qr code image dresdoug * @return * @throws例外 */public static string decode(string path)throws exception {return qrcodeutil.decode(new file(path));} public static void main(String [] args)throws {String text = String = String 「http://www.yhaomen.com "; qrcodeutil.encode(text、" c:/me.jpg "、" c:/barcode "、true);}}}ロゴなしでQRコードを生成します
プログラムコードは次のとおりです。
public static void main(string [] args)スロー例外{string text = "http://www.dans88.com.cn"; qrcodeutil.encode(text、 ""、 "d:/myworkdoc"、true);}このテスト方法を実行すると、生成されたQRコードにはロゴがありません。スタイルは次のとおりです。
興味がある場合は、電話でスキャンできます
ロゴでQRコードを生成します
ロゴ独自のアバターまたは好きな画像を使用して、次のコードを使用できます。プログラムコードは次のとおりです。
public static void main(string [] args)スロー例外{string text = "http://www.dans88.com.cn"; qrcodeutil.encode(text、 "d:/myworkdoc/my180.jpg"、 "d:/myworkdoc"、true); }唯一の違いは、ロゴのアドレスが以前に指定されており、Cディスクの画像ファイルがここで使用されていることです。
携帯電話でスキャンすると、表示したいテキストが表示されます。クリックして独自のWebサイトを入力します。
上記は、Javaデザインを使用してQRコードを生成することです。興味のある友達はそれを参照できます。このサイトへのご支援ありがとうございます!