生成二维码

    String short_url = "";
    String format = "gif";
    Hashtable hints = new Hashtable();

    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix bitMatrix;
    try {
     bitMatrix = new MultiFormatWriter().encode(short_url,
       BarcodeFormat.QR_CODE, width, height, hints);
       int margin = 20;  //自定义白边边框宽度
     bitMatrix = updateBit(bitMatrix, margin);
     MatrixToImageWriter.writeToFile(bitMatrix, format,
       outputFile);

 

 

 private BitMatrix updateBit(BitMatrix matrix, int margin){         int tempM = margin*2;        int[] rec = matrix.getEnclosingRectangle();   //获取二维码图案的属性             int resWidth = rec[2] + tempM;             int resHeight = rec[3] + tempM;             BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定义边框生成新的BitMatrix             resMatrix.clear();         for(int i= margin; i < resWidth- margin; i++){   //循环,将二维码图案绘制到新的bitMatrix中             for(int j=margin; j < resHeight-margin; j++){                 if(matrix.get(i-margin + rec[0], j-margin + rec[1])){                     resMatrix.set(i,j);                 }             }         }          return resMatrix;     }

相关文章
相关标签/搜索