編輯:關於 Android 程式設計
二維碼與條形碼已經深入到我們生活的方方面面,無論是支付、加好友還是商品溯源,都能看到它們的身影。本文將借助 Google 的 ZXing 庫,實現生成條形碼、生成二維碼,並進一步做出仿微信那種帶中心圖標的二維碼效果。
關鍵是調用 MultiFormatWriter 把字符串編碼成 BitMatrix,再逐像素還原成 Bitmap。注意編碼時就指定好尺寸,避免生成後再縮放導致模糊識別失敗:
public Bitmap Create2DCode(String str) throws WriterException {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
hints.put(EncodeHintType.CHARACTER_SET, "GBK");
BitMatrix matrix = new MultiFormatWriter().encode(
str, BarcodeFormat.QR_CODE, 500, 500, hints);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixels[y * width + x] = matrix.get(x, y) ? 0xff000000 : 0xffffffff;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
條形碼的生成思路類似,用 MultiFormatWriter 指定 CODE_128 格式即可,並可選地在下方拼接一段顯示內容的文字:
public Bitmap creatBarcode(Context context, String contents,
int desiredWidth, int desiredHeight, boolean displayCode) {
Bitmap resultBitmap = null;
if (displayCode) {
Bitmap barcodeBitmap = encodeAsBitmap(contents, barcodeFormat, desiredWidth, desiredHeight);
Bitmap codeBitmap = creatCodeBitmap(contents, desiredWidth + 2 * marginW, desiredHeight, context);
resultBitmap = mixtureBitmap(barcodeBitmap, codeBitmap, new PointF(0, desiredHeight));
} else {
resultBitmap = encodeAsBitmap(contents, barcodeFormat, desiredWidth, desiredHeight);
}
return resultBitmap;
}
仿微信效果的核心,是把縮放後的 Logo 疊加到二維碼的正中央。createBitmap 方法負責把水印畫到源圖中央:
private Bitmap createBitmap(Bitmap src, Bitmap watermark) {
if (src == null) return null;
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas cv = new Canvas(newb);
cv.drawBitmap(src, 0, 0, null);
cv.drawBitmap(watermark, w / 2 - ww / 2, h / 2 - wh / 2, null); // 居中
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
return newb;
}
為了美觀,可以再為中心圖標加上圓角與描邊。zoomBitmapBorder 負責縮放並墊一個底色邊框,getRoundedCornerBitmap 則通過 PorterDuff 模式裁出圓角:
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 12;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
基礎版二維碼:

加上圓角描邊後的仿微信效果:

整體而言,二維碼/條形碼的底層能力都來自 ZXing 這類成熟庫,我們需要做的更多是在圖像合成與美化上下功夫。掌握了 Bitmap 的縮放、拼接與圓角裁剪,就能做出各種個性化的二維碼。
自定義 SwipeLayout:一步步實現列表項側滑菜單
先看效果。 圖比較多,這裡只放一張。想看 ListView、GridView 上的表現和完整源碼的,可以直接去 GitHub 翻。 廢話不多說,先交代一下來歷。前幾天
Activity、Window 與 View:Android 界面繪製全鏈路拆解
本文想回答一個問題:Activity 到底是怎麼透過 Window 與 View 把自己畫到螢幕上的。 先看 Lifecycle 這裡只聚焦兩個最常混淆的場景——按 H
Android 常用控件全覽:Activity 類列表控件與適配器體系
列表與適配器是 Android UI 開發的地基。本文把 Activity 層面幾個常用列表類控件、以及適配器的類型劃分與工作流程做系統整理,適合當作速查手冊。 一、Ac
免權限實現檔案存取:Android 快取與應用目錄全攻略
在 Android 中,Context 提供了四個便捷的目錄方法,讓你能免開權限就完成檔案存取: getCacheDir、getFilesDir、getExternalC