java 通過Qrcode生成二維碼添加圖片logo和文字描述


/**
 *  二維碼創建
 * @author yhzm
 *
 */
public class printServiceImpl extends BaseService {
     public void barCodeGenera() {
             init(false);
             //先創建一個二維碼
             String text = strRequiredParam("barcode","二維碼信息");
             String desc = strRequiredParam("desc","文字內容");//二維碼下面的文字描述 
             String logoPath = "d:\\aa.png";//二維碼的logo地址
            int logoWidth = 40; //logo的寬
            int logoHeight = 40;  //logo的高
            try{
                 Qrcode qrcode = new Qrcode();
                 qrcode.setQrcodeErrorCorrect('M');//設置糾錯等級(分為:L、M、H三個等級)
                 qrcode.setQrcodeEncodeMode('B');//N代表數字、A代表a-Z、B代表其他字符
                 qrcode.setQrcodeVersion(7);//設置版本
                 
                 int width = 67+12*(7-1);//設置二維碼的寬  二維碼像素的大小和版本號有關  但是版本號越大   二維碼也越是復雜  這個需要注意
                 int height = 67+12*(7-1);//設置二維碼的高
                 //將內容變為特定UTF-8格式編碼的字節碼
                 byte [] qrData = text.getBytes("UTF-8");
                 
                 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
                 //創造畫筆
                 Graphics2D gs = bufferedImage.createGraphics();
                 gs.setBackground(Color.WHITE);//設置背景
                 gs.setColor(Color.BLACK);//設置畫筆顏色
                 gs.clearRect(0, 0, width, height);//清除畫板內容
                 //設置偏移量
                 int pixoff = 2;
                 boolean [][] d = qrcode.calQrcode(qrData);
                 for(int y=0;y<d.length;y++) {
                     for(int x=0;x<d.length;x++) {
                         if(d[x][y]) {
                             gs.fillRect(x*3+pixoff, y*3+pixoff, 3, 3);
                         }
                     }
                 }
                 gs.dispose();
                BufferedImage bm = bufferedImage;//二維碼  
                File logoFile = new File(logoPath); //logo圖片   
                BufferedImage logoImg = ImageIO.read(logoFile);  
                /* float ratio = 0.5;   //倒圓角
                 if(ratio>0){  
                     logoWidth = logoWidth>width*ratio ? (int)(width*ratio) : logoWidth;  
                     logoHeight = logoHeight>height*ratio ? (int)(height*ratio) : logoHeight;  
                 }  */
                int x = (width-logoWidth)/2;  
                int y = (height-logoHeight)/2;  
                Graphics g = bm.getGraphics();  
                g.drawImage(logoImg, x, y, logoWidth, logoHeight, null);  
                int whiteWidth = 0;  //白邊
                Font font = new Font("黑體", Font.BOLD, 12);  
                int fontHeight = g.getFontMetrics(font).getHeight();//得到字體的高度  
                //計算需要多少行  
                int lineNum = 1;  
                int currentLineLen = 0;  
                for(int i=0;i<desc.length();i++){  
                    char c = desc.charAt(i);  
                    int charWidth = g.getFontMetrics(font).charWidth(c); 
                    //循環文字得到文字區域的行數
                    if(currentLineLen+charWidth>width){  
                        lineNum++;  
                        currentLineLen = 0;  
                        continue;  
                    }  
                    currentLineLen += charWidth;  
                }  
                int totalFontHeight = fontHeight*lineNum; //得到文字區域的高度 
                int wordTopMargin = 4;  
                BufferedImage bm1 = new BufferedImage(width, height+totalFontHeight+wordTopMargin-whiteWidth, BufferedImage.TYPE_INT_RGB); //創建將文字高度計算到其中的圖片 
                Graphics g1 = bm1.getGraphics();   
                g1.setColor(Color.WHITE);  
                g1.fillRect(0, height, width, totalFontHeight+wordTopMargin-whiteWidth); //將文字部分的背景填充成白色 
                g1.setColor(Color.black);  
                g1.setFont(font);  
                g1.drawImage(bm, 0, 0, null); //將創建好的二維碼從起點(0,0)開始畫在圖中 
                int currentLineIndex = 0;  
                //判斷是否只有一行,只有一行就居中顯示
                currentLineLen = lineNum-1==currentLineIndex?(width-g.getFontMetrics(font).stringWidth(desc))/2:0;
                int baseLo = g1.getFontMetrics().getAscent();  
                for(int i=0;i<desc.length();i++){  
                    String c = desc.substring(i, i+1);  
                    int charWidth = g.getFontMetrics(font).stringWidth(c); 
                    //判斷是否需要換行
                    if(currentLineLen+charWidth>width){  
                        currentLineIndex++;  
                        //判斷是否是最后一行  最后一行居中顯示
                        currentLineLen = lineNum-1==currentLineIndex?(width-g.getFontMetrics(font).stringWidth(desc.substring(i)))/2:0;
                        g1.drawString(c, currentLineLen + whiteWidth, height+baseLo+fontHeight*(currentLineIndex)+wordTopMargin);//將單個文字畫到對應位置 
                        currentLineLen = charWidth;  
                        continue;  
                    }  
                    g1.drawString(c, currentLineLen+whiteWidth, height+baseLo+fontHeight*(currentLineIndex) + wordTopMargin);  
                    currentLineLen += charWidth;  
                } 
                g1.dispose();  
                bm = bm1;
             response.setContentType("image/jpeg");
             //好了 ,現在通過IO流來傳送數據
            ImageIO.write(bm , "JPEG", response.getOutputStream());
        }catch(Throwable e){  
            e.printStackTrace();  
        }  
    }
}

這個可以用來生成二維碼,並且攜帶log和文字。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM