Zxingを使用して、QRコードジェネレーターの埋め込み画像には、次のように特定の基準値があります。
基本的なアイデアは、最初にZxingによって生成されたQRコード画像を使用し、次に画像を読み、その中にアイコンを挿入してから、画像全体を出力することです。
最近のプロジェクトでは、QRコードを生成する必要があります。いくつかの例を見つけた後、最終効果を達成しました。 QRコードは、画像形式(JPGなど)を生成したり、Webページに表示したりできます。この記事は記録されており、多くの類似点があるので、忘れたいと思います。 。 。 。
注:Zxingパッケージを必要とするツールの場合、一般的なプロセスは、埋め込み写真を読み取り、コンテンツをQRコードに変換し、写真をQRコードに埋め込み、写真を作成することです。
これが完全なコードです:
java.awt.basicstroke; Import java.awt.color; import java.awt.graphics2d; Import java.awt.image; image java.awt.shape; Import.geom.affinetransform; import java.awt.geom.geom. java.awt.image.bufferedimage; import java.io.file; Import java.io.ioexception; import java.util.hashmap; Import javax.imageio.imageio; Import com.google.zxing.barcodeformat; import com.google.zxthet. com.google.zxing.multifformatwriter; Import com.google.zxing.writerexception; Import com.google.zxing.common.bitmatrix; import com.google.zxing.qrcode.decoder.errorcorrectionlevel; public intic int int int int int final intic 0xffffffff; //一般的なprivate static final int image_width = 80; private static final int image_half_width = image_width / 2; private static final int frame_width = 2; // QRコードプライベートStatic MultiformatWriter MutiWriter = new MultiformatWriter()を書き込みます。 public static void main(string [] args){try {// bitmatrix bitmatrix = multiformatwriter.encode(content、barcodeformat.qr_code、400、400、hints); string content = "134000000"; // qrコードコンテンツバッファーコード=ジェンバルコード(コンテンツ、400、400、400、400、400、400、400、400、400、400、400、400、400、400、400、400、400、 "f://amazed.png"); if(!imageio.write(image、 "jpg"、new file( "f://2122.jpg")))))))) } /** //上記のコードをここに交換し、ストリームを使用してページに読み取りますoutputStream os = respons.getOutputStream(); if(!imageio.write(image、 "jpg"、os))){throw new ioException( "フォーマットの画像を書くことができなかった"); } **/} catch(writerexception e){e.printstacktrace();} catch(ioexception e){// todo auto-denerated catch blocke.printstacktrace();}} private bufferedimage tobufferedimage(bitmatrix matrix){int width = matrix(); matrix.getheight(); bufferedimage image = new bufferedimage(width、height、bufferedimage.type_int_rgb); for(int x = 0; x <width; x ++){for画像;} private static bufferedimage genbarcode(string content、int width、int height、string srcimagepath)writerexception、ioexception {//ソース画像bufferedimage scaleimage = scale(srcimagepath、image_width、image_height、true); int [] [] srcpixels = new int [image_width] [image_height]; for(int i = 0; i <scaleimage.getWidth(); i ++){for(int j = 0; j <sacleimage.getheight(); j ++){srcpixels [i] [j] = scaleimage.getrgb(i、j); }} map <encodehinttype、object> hint = new hashmap <encodehinttype、object>(); hint.put(encodehinttype.character_set、 "utf-8"); // hint.putをエンコードするコンテンツ(encodehinttype.error_correction、errorcorrectionlevel.h); //エラーレベルHint.put(encodehinttype.margin、1); // QRコードの外側境界の空白の領域の幅を設定します// 2Dマトリックスを1次元ピクセル配列に変換しますint halfw = matrix.getWidth() / 2; int halfh = matrix.getheigh() / 2; int [] pixels = new int [width * height]; for(int y = 0; y <matrix.getheight(); y ++){for(int x = 0; x <matrix.getWidth(); x ++){//画像を読むif(x> halfw -halfw -half_width && x <halfw+image_half_width image_half_width){pixels [y * width + x] = srcpixels [x -halfw + image_half_width] [y -halfh + image_half_width]; } //写真の周りに境界線を形成しますelse if((x> halfw -image_half_width -frame_width && x <halfw -image_half_width + frame_width && y> halfh -image_half_width -frame_width && y <half_width | width + frame_width)| width + frame_width | -frame_width && x <halfw + image_half_width + frame_width && y> halfh -image_half_width && y <halfh + image_half_width + frame_width)|| && y> halfh -image_half_width -frame_width && y <halfh -image_half_width + frame_width)|| + image_half_width + frame_width)){pixels [y * width + x] = 0xffffffff; } else {//ここでは、QRコードの色を変更でき、QRコードと背景の色をそれぞれ策定できます。ピクセル[y * width + x] = matrix.get(x、y)? 0xff000000:0xffffffff; }}} bufferedimage image = new bufferedimage(幅、height、bufferedimage.type_int_rgb); image.getRaster()。setDataelements(0、0、幅、高さ、ピクセル);画像を返します。 } /** *要件を満たすアイコンを生成するために、渡された元の画像を高さと幅でスケーリングします * * @param srcimagefile *ソースファイルアドレス * @param height *ターゲット高さ * @param width *ターゲット幅 * @param hasfiller *スケールが間違っているときに記入する必要がありますか:trueは入力します。 Falseは記入されていません。 * @Throws IOException */ private static bufferedimageスケール(String srcimagefile、int height、int width、boolean hasfiller)throws ioexception {double ratio = 0.0; // scale file = new file(srcimageFile); bufferedimage srcimage = imageio.read(file); Image Destimage = srcimage.getScaledInstance(幅、高さ、bufferedimage.scale_smooth); //スケールの計算if((srcimage.getheight()> height)||(srcimage.getWidth()> width)){if(srcimage.getheight()> srcimage.getWidth(){ratio =(height))。 } else {ratio =(new Integer(width))。doublevalue() / srcimage.getWidth(); } affinetransformop op = new affinetransformop(affinetransform.getScaleinStance(比率、比率)、null); destimage = op.filter(srcimage、null); } if(hasfiller){// bufferedimage image = new bufferedimage(width、height、bufferedimage.type_int_rgb); Graphics2d Graphic = image.creategraphics(); Graphic.setColor(color.pink); Graphic.FillRect(10、10、幅、高さ); Graphic.DrawRect(100、360、幅、高さ); if(width == destimage.getWidth(null)){graphic.drawimage(destimage、0、(height -destimage.getheight(null)) / 2、destimage.getWidth(null)、destimage.getheigh(null)、color.white、null); shape shape = new RoundRectangle2d.float(0、(height -destimage.getheight(null)) / 2、幅、幅、20、20); Graphic.setStroke(新しいBasicStroke(5F)); graphic.draw(shape); } else {graphic.drawimage(destimage、(width -destimage.getWidth(null)) / 2、0、destimage.getWidth(null)、destimage.getheight(null)、color.white、null); shape shape = new RoundRectangle2d.float((width -destimage.getWidth(null)) / 2、0、width、width、20、20); Graphic.setStroke(新しいBasicStroke(5F)); graphic.draw(shape); } graphic.dispose(); Destimage = image; } return(bufferedimage)destimage; }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。