java帶logo的二維碼
發布時間: 2020-12-03 22:38:34
A. QRCode(java)生成帶logo的二維碼,能否在中間空出固定大小的位置給logo讓解析不出錯
二維碼中間部分是校驗碼,在那裡加上一個圖標是沒有影響的。
這就是為什麼很多二維碼有圖標而不會出錯的原因。
B. java怎麼生成帶logo二維碼
1、下載生成二維碼所需要的jar包qrcode.jar;
2、直接上生成二維碼的java代碼
//需要導入的包
importjava.awt.Color;
importjava.awt.Graphics2D;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjavax.imageio.ImageIO;
importcom.swetake.util.Qrcode;
/**
*生成二維碼(QRCode)圖片
*@paramcontent二維碼圖片的內容
*@paramimgPath生成二維碼圖片完整的路徑
*@paramccbpath二維碼圖片中間的logo路徑
*/
publicstaticintcreateQRCode(Stringcontent,StringimgPath,StringccbPath){
try{
QrcodeqrcodeHandler=newQrcode();
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
qrcodeHandler.setQrcodeVersion(7);
//System.out.println(content);
byte[]contentBytes=content.getBytes("gb2312");
//構造一個BufferedImage對象設置寬、高
BufferedImagebufImg=newBufferedImage(140,140,BufferedImage.TYPE_INT_RGB);
Graphics2Dgs=bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0,0,140,140);
//設定圖像顏色>BLACK
gs.setColor(Color.BLACK);
//設置偏移量不設置可能導致解析出錯
intpixoff=2;
//輸出內容>二維碼
if(contentBytes.length>0&&contentBytes.length<120){
boolean[][]codeOut=qrcodeHandler.calQrcode(contentBytes);
for(inti=0;i<codeOut.length;i++){
for(intj=0;j<codeOut.length;j++){
if(codeOut[j][i]){
gs.fillRect(j*3+pixoff,i*3+pixoff,3,3);
}
}
}
}else{
System.err.println("QRCodecontentbyteslength="
+contentBytes.length+"notin[0,120].");
return-1;
}
Imageimg=ImageIO.read(newFile(ccbPath));//實例化一個Image對象。
gs.drawImage(img,55,55,30,30,null);
gs.dispose();
bufImg.flush();
//生成二維碼QRCode圖片
FileimgFile=newFile(imgPath);
ImageIO.write(bufImg,"png",imgFile);
}catch(Exceptione){
e.printStackTrace();
return-100;
}
return0;
}
來自網友孤獨青鳥的博客
C. java生成中間帶logo圖片二維碼,logo圖片模糊。請問各位大俠,有啥解決辦法
圖片太模糊的話應該是失真了,引用圖片的時候設置下img標簽的height和width屬性與圖片的尺寸一致應該就行了,你可以試試看。
熱點內容