1、單純的生成二維碼
/**
* @Title: createQRImage
* @param url
* 可以是鏈接地址,也可以是文字
* @param QR_WIDTH
* 二維碼寬度
* @param QR_HEIGHT
* 二維碼高度
* @return Bitmap 返回一個bitmap,可以自行保存到本地,也可以設置顯示在頁面
*/
public static Bitmap createQRImage(String url, int QR_WIDTH, int QR_HEIGHT) {
Bitmap bitmap = null;
try {
// 判斷URL合法性
if (url == null || "".equals(url) || url.length() < 1) {
return bitmap;
}
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 圖像數據轉換,使用了矩陣轉換
BitMatrix bitMatrix = new QRCodeWriter().encode(url,
BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
// 下面這裏按照二維碼的算法,逐個生成二維碼的圖片,
// 兩個for循環是圖片橫列掃描的結果
for (int y = 0; y < QR_HEIGHT; y++) {
for (int x = 0; x < QR_WIDTH; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * QR_WIDTH + x] = 0xff000000;
} else {
pixels[y * QR_WIDTH + x] = 0xffffffff;
}
}
}
// 生成二維碼圖片的格式,使用ARGB_8888
bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
} catch (WriterException e) {
e.printStackTrace();
return null;
}
return bitmap;
}
2、生成帶圖片的二維碼
/**
* @Title: createQRImage帶圖片
* @param url
* 可以是鏈接地址,也可以是文字
* @param QR_WIDTH
* 二維碼寬度
* @param QR_HEIGHT
* 二維碼高度
* @param mBitmap
* 圖片
* @return Bitmap 返回一個bitmap,可以自行保存到本地,也可以設置顯示在頁面
*/
public static Bitmap createQRImage(String url, int QR_WIDTH, int QR_HEIGHT,
Bitmap mBitmap,Context context) {
try {
Matrix m = new Matrix();
float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth();
float sy = (float) 2 * IMAGE_HALFWIDTH
/ mBitmap.getHeight();
m.setScale(sx, sy);
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0,
mBitmap.getWidth(), mBitmap.getHeight(), m, false);
String contentString = url;
mBitmap = cretaeBitmap(mBitmap,new String(contentString.getBytes(),"utf-8"),context);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mBitmap;
}
private static final int IMAGE_HALFWIDTH = 40;//二維碼的寬度
public static Bitmap cretaeBitmap(Bitmap mBitmap,String str,Context context) throws WriterException {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, Utils.dip2px(context, 300), Utils.dip2px(context, 300), hints);
int width = matrix.getWidth();
int height = matrix.getHeight();
int halfW = width / 2;
int halfH = height / 2;
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH
&& y > halfH - IMAGE_HALFWIDTH
&& y < halfH + IMAGE_HALFWIDTH) {
pixels[y * width + x] = mBitmap.getPixel(x - halfW
+ IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);
} else {
if (matrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
} else {
pixels[y * width + x] = 0xffffffff;
}
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}