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