上碼,如有問題或者優(yōu)化,勞請廣友下方留言
1.工具類
復制代碼
importcom.google.zxing.BarcodeFormat;
importcom.google.zxing.EncodeHintType;
importcom.google.zxing.client.j2se.MatrixToImageConfig;
importcom.google.zxing.client.j2se.MatrixToImageWriter;
importcom.google.zxing.common.BitMatrix;
importcom.google.zxing.qrcode.QRCodeWriter;
importcom.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
importcom.sun.org.apache.xml.internal.security.utils.Base64;
importorg.springframework.util.ResourceUtils;
importjavax.imageio.ImageIO;
importjavax.servlet.ServletOutputStream;
importjava.awt.*;
importjava.awt.geom.RoundRectangle2D;
importjava.awt.image.BufferedImage;
importjava.io.ByteArrayOutputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.HashMap;
publicclassQrCodeUtil{
publicStringcreateQRCode(Stringcontent,intwidth,intheight,Stringpath)throwsIOException{
StringresultImage=””;
if(!EmptyUtils.isEmpty(content)){
ServletOutputStreamstream=null;
ByteArrayOutputStreamos=newByteArrayOutputStream();
@SuppressWarnings(“rawtypes”)
HashMap<EncodeHintType,Comparable>hints=newHashMap<>();
hints.put(EncodeHintType.CHARACTER_SET,”utf-8″);//指定字符編碼為“utf-8”
hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);//指定二維碼的糾錯等級為中
hints.put(EncodeHintType.MARGIN,2);//指定二維碼邊距
try{
QRCodeWriterwriter=newQRCodeWriter();
BitMatrixbitMatrix=writer.encode(content,BarcodeFormat.QR_CODE,width,height,hints);
MatrixToImageConfigconfig=newMatrixToImageConfig(0xFF000001,0xFFFFFFFF);
BufferedImagebufferedImage=MatrixToImageWriter.toBufferedImage(bitMatrix,config);
//BufferedImagebufferedImage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//for(intx=0;x<width;x++){
//for(inty=0;y<height;y++){
//bufferedImage.setRGB(x,y,bitMatrix.get(x,y)?0xFF000000:0xFFFFFFFF);
//}
//}
Graphics2Dg2=bufferedImage.createGraphics();
g2.drawImage(ImageIO.read(ResourceUtils.getFile(path)),width/5*2,height/5*2,width/5,height/5,null);//logo.png自行設置
g2.setStroke(newBasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
g2.draw(newRoundRectangle2D.Float(width/5*2,height/5*2,width/5,height/5,20,20));
g2.dispose();
g2.setColor(Color.white);
g2.setBackground(Color.white);
bufferedImage.flush();
ImageIO.write(bufferedImage,”png”,os);
OutputStreamout=newFileOutputStream(“D:\\logo\\abb.png”);
out.write(os.toByteArray());
resultImage=newString(“data:image/png;base64,”+Base64.encode(os.toByteArray()));
returnresultImage;
}catch(Exceptione){
e.printStackTrace();
}finally{
if(stream!=null){
stream.flush();
stream.close();
}
}
}
returnnull;
}
}
廣州天河區(qū)珠江新城富力盈力大廈北塔2706
020-38013166(網(wǎng)站咨詢專線)
400-001-5281 (售后服務熱線)
深圳市坂田十二橡樹莊園F1-7棟
Site/ http://www.szciya.com
E-mail/ itciya@vip.163.com
品牌服務專線:400-001-5281
長沙市天心區(qū)芙蓉中路三段398號新時空大廈5樓
聯(lián)系電話/ (+86 0731)88282200
品牌服務專線/ 400-966-8830
旗下運營網(wǎng)站:
Copyright ? 2016 廣州思洋文化傳播有限公司,保留所有權利。 粵ICP備09033321號