設置google zxing 生成的二維碼邊框大小


 Map<EncodeHintType, Object> his = new HashMap<EncodeHintType, Object>();
his.put(EncodeHintType.MARGIN, 1);
--Map his = new HashMap();



public static void zxingCodeCreate(String text, int width, int height, String outPutPath, String imageType) {
Map<EncodeHintType, Object> his = new HashMap<EncodeHintType, Object>();
his.put(EncodeHintType.MARGIN, 1);


//設置編碼字符集
his.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
//1、生成二維碼
BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, his);

//2、獲取二維碼寬高
int codeWidth = encode.getWidth();
int codeHeight = encode.getHeight();

//3、將二維碼放入緩沖流
BufferedImage image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < codeWidth; i++) {
for (int j = 0; j < codeHeight; j++) {
//4、循環將二維碼內容定入圖片
image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
}
}
File outPutImage = new File(outPutPath);
//如果圖片不存在創建圖片
if (!outPutImage.exists())
outPutImage.createNewFile();
//5、將二維碼寫入圖片
ImageIO.write(image, imageType, outPutImage);
} catch (WriterException e) {
e.printStackTrace();
System.out.println("二維碼生成失敗");
} catch (IOException e) {
e.printStackTrace();
System.out.println("生成二維碼圖片失敗");
}
}

----------上面方法不行,用下面的






1、基本方法

//二維碼容錯率,分四個等級:H、L 、M、 Q

        ErrorCorrectionLevel level = ErrorCorrectionLevel.H;
  
        String qrName = "test.png"; //生成二維碼圖片名稱
        String targetPath = ServletActionContext.getServletContext().getRealPath("/");  //不解釋
        File target = new File(targetPath, qrName);
        if(!target.exists()){
            target.mkdirs();
        }

//生成二維碼中的設置
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //編碼
        hints.put(EncodeHintType.ERROR_CORRECTION, level); //容錯率
        hints.put(EncodeHintType.MARGIN, 0);  //二維碼邊框寬度,這里文檔說設置0-4,但是設置后沒有效果,不知原因,

        String content = “二維碼內容”;

         int size = 200;  //二維碼圖片大小

        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size,hints); //生成bitMatrix

        int margin = 5;  //自定義白邊邊框寬度

        bitMatrix = updateBit(bitMatrix, margin);  //生成新的bitMatrix

        //因為二維碼生成時,白邊無法控制,去掉原有的白邊,再添加自定義白邊后,二維碼大小與size大小就存在差異了,為了讓新

生成的二維碼大小還是size大小,根據size重新生成圖片

        BufferedImage bi =  MatrixToImageWriter.toBufferedImage(bitMatrix);
        bi = zoomInImage(bi,size,size);//根據size放大、縮小生成的二維碼
        ImageIO.write(bi, "png", target); //生成二維碼圖片

這樣生成的二維碼在圖片屬性上跟我們設置的圖片大小size是一致的。

唯一不明白的就是zxing庫中生成二維碼是設置白邊邊框不起作用,如果起作用,就不用這么麻煩了。

 

 2、調用的方法

因為二維碼邊框設置那里不起作用,不管設置多少,都會生成白邊,所以根據網上的例子進行修改,自定義控制白邊寬度,

該方法生成自定義白邊框后的bitMatrix;

 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;
    }

 

  /**
     * 圖片放大縮小
     */
    public static BufferedImage  zoomInImage(BufferedImage  originalImage, int width, int height){
        BufferedImage newImage = new BufferedImage(width,height,originalImage.getType());
        Graphics g = newImage.getGraphics();
        g.drawImage(originalImage, 0,0,width,height,null);
        g.dispose();
        return newImage;

    }


免責聲明!

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



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