二維碼的開發(fā)使用大多都是使用Google提供的zxing這個(gè)類庫(kù).:引用的JAR包下載地址,
核心生成 代碼段
/**
* 創(chuàng)建二維碼
*
* @param content content
* @param widthPix widthPix
* @param heightPix heightPix
* @param logoBm logoBm
* @return 二維碼
*/
public static Bitmap createQRCode(String content, int widthPix, int heightPix, Bitmap logoBm) {
try {
if (content == null || "".equals(content)) {
return null;
}
// 配置參數(shù)
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 容錯(cuò)級(jí)別
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 圖像數(shù)據(jù)轉(zhuǎn)換,使用了矩陣轉(zhuǎn)換
BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix,
heightPix, hints);
int[] pixels = new int[widthPix * heightPix];
// 下面這里按照二維碼的算法,逐個(gè)生成二維碼的圖片,
// 兩個(gè)for循環(huán)是圖片橫列掃描的結(jié)果
for (int y = 0; y < heightPix; y++) {
for (int x = 0; x < widthPix; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * widthPix + x] = 0xff000000;
} else {
pixels[y * widthPix + x] = 0xffffffff;
}
}
}
// 生成二維碼圖片的格式,使用ARGB_8888
Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);
if (logoBm != null) {
bitmap = addLogo(bitmap, logoBm);
}
//建議使用compress方法將bitmap保存到文件中再進(jìn)行讀取。直接返回的bitmap是沒有任何壓縮的,內(nèi)存消耗巨大!
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
也可以為二維碼中間添加logo
添加 logo的 為
/**
* 在二維碼中間添加Logo圖案
*/
private static Bitmap addLogo(Bitmap src, Bitmap logo) {
if (src == null) {
return null;
}
if (logo == null) {
return src;
}
//獲取圖片的寬高
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
int logoWidth = logo.getWidth();
int logoHeight = logo.getHeight();
if (srcWidth == 0 || srcHeight == 0) {
return null;
}
if (logoWidth == 0 || logoHeight == 0) {
return src;
}
//logo大小為二維碼整體大小的1/5
float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
try {
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(src, 0, 0, null);
canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
} catch (Exception e) {
bitmap = null;
e.getStackTrace();
}
return bitmap;
}
在需要使用的地方
Bitmap qrBitmap= EncodingUtils.createQRCode(url, 400, 400, null);
imageview.setImageBitmap(qrBitmap);
其實(shí)這個(gè)根本沒有什么難度和技巧.嘗試分析bitmap作為二維碼繪制內(nèi)存占用和優(yōu)化辦法.
bitmap作為一個(gè)大胖子.分析下構(gòu)成.
拿繪制二維碼的配置來言:Bitmap.Config.ARGB_8888
官方解釋:
Each pixel is stored on 4 bytes.
Each channel (RGB and alpha for translucency) is stored with 8 bits of precision (256 possible values.)
This configuration is very flexible and offers the best quality. It should be used whenever possible
一個(gè)像素占用4個(gè)字節(jié),alpha(A)值,Red(R)值,Green(G)值,Blue(B)值各占8個(gè)bites共32bites,即4個(gè)字節(jié).
(注:ARGB是一種色彩模式,里面A代表Alpha,R表示red,G表示green,B表示blue,其實(shí)所有的可見色都是紅綠藍(lán)組成的,所以紅綠藍(lán)又稱為三原色。)
那么 就像上邊寫的 400px*400px ,那么一共消耗:
ARGB_8888 圖片長(zhǎng)度*圖片寬度*4 400*400*4=640000字節(jié)=625kb
如果更大,或者展示數(shù)量多,不可避免的需要消耗內(nèi)存更多,那么必須要考慮優(yōu)化.
壓縮(compress)
一般是一搜一堆的這樣的方法:
ByteArrayOutputStream out = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 80, out);
imageView.setImageBitmap(bm);
很容易進(jìn)入誤區(qū),這是壓縮存儲(chǔ)的大小.
Bitmap.compress方法確實(shí)可以壓縮圖片,但壓縮的是存儲(chǔ)大小
這樣子根本是不起作用的,正常的做法是壓縮寬高比(對(duì)于那些巨大的Bitmap).
當(dāng)然及時(shí)回收也是有必要的recycle()方法.
BitmapDrawable drawable = (BitmapDrawable) erweicode.getDrawable();
Bitmap bmp = drawable.getBitmap();
if (null != bmp && !bmp.isRecycled()) {
bmp.recycle();
bmp = null;
}