Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

仿微信效果:Android 生成二維碼與條形碼的實現

編輯:關於 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 的縮放、拼接與圓角裁剪,就能做出各種個性化的二維碼。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved