生活随笔
收集整理的這篇文章主要介紹了
27.用zxing生成二维码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先在idea里面導入jar包
鏈接:https://pan.baidu.com/s/1UdorNg8o0A4FsElghqkJyw
提取碼:c4bt
復制這段內容后打開百度網盤手機App,操作更方便哦
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
/***使用zxing生成二維碼*/
public class CreateQRCode {public static void main(String[] arge) throws WriterException, IOException {int width = 300;//定義圖片寬度int height = 300;//定義圖片高度String format ="png";//定義圖片格式String content ="https://blog.csdn.net/weixin_43392489";//定義二維碼內容//定義二維碼參數HashMap hints = new HashMap();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//設置編碼//設置容錯等級,等級越高,容量越小hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);hints.put(EncodeHintType.MARGIN,2);//設置邊距//生成矩陣BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);//設置路徑Path file = new File("D:/Users/ma.png").toPath();MatrixToImageWriter.writeToPath(bitMatrix, format, file);//輸出圖像}
}
復制上面代碼
運行即可生成
?
最后在d盤user下可以找到
總結
以上是生活随笔為你收集整理的27.用zxing生成二维码的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。