パッケージcom.bus.wx.action.code。
輸入java.awt.BasicStroke;
インポートをjava.awt.Color;
輸入java.awt.Graphics2D;
輸入java.awt.RenderingHintsの。
輸入java.awt.geom.RoundRectangle2D;
輸入java.awt.image.BufferedImage;
輸入java.io.ByteArrayInputStream;
輸入java.io.ByteArrayOutputStreamを;
インポートのjava.io.File;
インポートにjava.io.IOException;
輸入java.io.InputStreamを。
輸入java.util.HashMapを;
インポートするjava.util.Hashtable;
輸入java.util.Map;
輸入javax.imageio.ImageIO;
インポートのjavax.servlet.ServletOutputStream。
輸入org.springframework.context.annotation.Scope;
輸入org.springframework.stereotype.Controller;
輸入org.springframework.web.bind.annotation.PathVariable;
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.bind.annotation.RequestMethod;
輸入sun.misc.BASE64Decoder;
輸入sun.misc.BASE64Encoder;
輸入com.bus.plugin.wx.action.WxAction。
輸入com.bus.wx.util.Limits。
輸入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.QRCodeWriter;
輸入com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
輸入com.lys.sys.log.Log;
/ **
*バーコード、二次元コード
* @Authorパワー
*
* /
@Scope(値= "プロトタイプ")
@Controller( "Bar_Qr_Code_Action")
@RequestMapping(値= "プラグ/ WX / WWZ / {bcflag} / barqrcode")
パブリッククラスBar_Qr_Code_ActionはWxActionを{延び
プライベート静的最終int型LogoPart = 4;
パターンの色を設定//;プライベート静的最終int型BLACK = 0xFF000000
プライベート静的最終int型WHITE = 0xFFFFFFFFに; //背景色について
文字列フォーマット=「PNG」。
パブリック静的たBufferedImage toBufferedImage(BitMatrix行列){
INT幅= matrix.getWidth()。
INT高さ= matrix.getHeight()。
バッファ画像イメージ=新しいBufferedImageの(幅、高さ、BufferedImage.TYPE_INT_RGB)。
{(x ++; X <幅整数X = 0)のための
用(int型、Y = 0; Y <高さ; Y ++){
image.setRGB(X、Y、(matrix.get(x、y)は黒:白)?)。
// image.setRGB(X、Y、(matrix.get(x、y)はColor.YELLOW.getRGB():Color.CYAN.getRGB()))。
}
}
画像を返します。
}
公共のBufferedImage LogoMatrix(BufferedImageのmatrixImage)はIOExceptionが{スロー
/ **
*ビルドに2次元コード画像や描画オブジェクトを読みます
* /
Graphics2D G2 = matrixImage.createGraphics()。
INT matrixWidth = matrixImage.getWidth()。
INT matrixHeigh = matrixImage.getHeight()。
/ **
*読むのロゴ画像
* /
文字列パス=でrequest.getSession()のgetServletContext()getRealPath( "/画像/ロゴ/ dzjkklog.png")。
BufferedImageのロゴ= ImageIO.read(新しいファイル(パス));
//絵を描き始めます
g2.drawImage(* 2ロゴ、matrixWidth / 5、matrixHeigh / 5 * 2、matrixWidth / 5、matrixHeigh / 5、NULL); //绘制
BasicStrokeのストローク=新しいのBasicStroke(5、BasicStroke.CAP_ROUND、BasicStroke.JOIN_ROUND)。
g2.setStroke(脳卒中); //設定されたオブジェクトのストローク
//角の丸い四角形ラジアン指定鄭州婦人科病院をhttp://fk.zyfuke.com/
RoundRectangle2D.Floatラウンド=新しいRoundRectangle2D.Float(matrixWidth / * 2 5、matrixHeigh / * 2 5、matrixWidth / 5、matrixHeigh / 5,20,20)。
g2.setColor(Color.white)。
g2.draw(ラウンド); //四角形の弧を描きます
//セットロゴはグレーのボーダーを持っています
BasicStrokeのstroke2 =新規のBasicStroke(1、BasicStroke.CAP_ROUND、BasicStroke.JOIN_ROUND)。
g2.setStroke(stroke2); //設定されたオブジェクトのストローク
RoundRectangle2D.Float round2 =新しいRoundRectangle2D.Float(matrixWidth / 5 * 2 + 2、matrixHeigh / 5 * 2 + 2、matrixWidth / 5-4 / 5-4,20,20 matrixHeigh)。
g2.setColor(新色(128,128,128));
g2.draw(round2); //四角形の弧を描きます
g2.dispose();
matrixImage.flush();
matrixImageを返します。
}
/ **
*生成二次元コード:生成された2次元コードを直接フロントページに送信
* @Param bcflag
* /
(値= "createQrCodes" を、方法= RequestMethod.GET)@RequestMapping
公共ボイドcreateQrCodes(@PathVariableストリングbcflag、文字列dastid){
文字列のURL =「コンテンツスキャン二次元コードが表示されています」。
もし(URL!= nullの&&は! ""。等しい(URL)){
ServletOutputStreamをストリーム= NULL;
{試します
int型の幅= 430;
int型高さ= 430;
ストリーム= response.getOutputStream()。
QRCodeWriterライター=新しいQRCodeWriter();
Hashtableのヒント=新しいHashtableの();
hints.put(EncodeHintType.ERROR_CORRECTION、ErrorCorrectionLevel.H)。
hints.put(EncodeHintType.CHARACTER_SET、 "UTF-8");
hints.put(EncodeHintType.MARGIN、1); //二次元コードのエッジの周期を設定し、非負
符号化するBitMatrix bitMatrix =新しいMultiFormatWriter()。エンコード(URL、//コンテンツ
BarcodeFormat.QR_CODE、
幅幅、//バーコード
バーコードの高さ、//高さ
ヒント);
// BitMatrix M = writer.encode(URL、BarcodeFormat.QR_CODE、高さ、幅)。
バッファ画像イメージはtoBufferedImage(bitMatrix)を=。
画像= LogoMatrix(画像)。
IF(!ImageIO.write(画像、フォーマット、ストリーム)){
新しいIOExceptionが(+フォーマット「形式の画像を作成することができませんでした」)投げます。
}
}キャッチ(例外e){
Log.in.info(e.getMessage())。
}最後に{
もし(ストリーム!= NULL){
{試します
stream.flush();
stream.close();
}キャッチ(IOExceptionを電子){
Log.in.info(e.getMessage())。
}
}
}