利用Zxing生成二維碼

1.zxing Jar包下載地址

http://maven.outofmemory.cn/com.google.zxing/core/3.1.0/

2.

importandroid.util.Log;

importcom.google.zxing.BarcodeFormat;

importcom.google.zxing.EncodeHintType;

importcom.google.zxing.MultiFormatWriter;

importcom.google.zxing.WriterException;

importcom.google.zxing.common.BitMatrix;

importjava.util.Hashtable;

public classQR {

public staticBitmapQRAction(String url) {

String contentString ="";

Bitmap qrCodeBitmap =null;

try{

if(url !=null) {

contentString = url;

}

if(!contentString.equals("")) {

qrCodeBitmap = EncodingHandler.createQRCode(contentString,500);

}else{

Log.e(TAG,"Text can not be empty");

return null;

}

}catch(WriterException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

returnqrCodeBitmap;

}

private static classEncodingHandler {

private static final intBLACK=0xff000000;

public staticBitmapcreateQRCode(String str, intwidthAndHeight)throwsWriterException {

Hashtable hints =newHashtable();

hints.put(EncodeHintType.CHARACTER_SET,"utf-8");

BitMatrix matrix =newMultiFormatWriter().encode(str,BarcodeFormat.QR_CODE,widthAndHeight,

widthAndHeight);

intwidth = matrix.getWidth();

intheight = matrix.getHeight();

int[] pixels =new int[width * height];

for(inty =0;y < height;y++) {

for(intx =0;x < width;x++) {

if(matrix.get(x,y)) {

pixels[y * width + x] =BLACK;

}

}

}

Bitmap bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);

bitmap.setPixels(pixels,0,width,0,0,width,height);

returnbitmap;

}

}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容