Android 二維碼相關(一)

夏沫琅琊發表於2024-03-08

Android 二維碼相關(一)

本篇文章主要記錄下android下使用zxing來建立二維碼.

1: 匯入依賴

api "com.google.zxing:core:3.5.1"

2: 建立二維碼

  1. 建立QRCodeWriter物件

    QRCodeWriter qrCodeWriter = new QRCodeWriter();	
    
  2. 將文字內容轉換成BitMatrix

    BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, size, size);
    
  3. 建立bitmap

    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565);
    
  4. 將BitMatrix渲染到bitmap

    for (int x = 0; x < size; x++) {
        for (int y = 0; y < size; y++) {
            //將BitMatrix渲染到bitmap
            bitmap.setPixel(x, y, encode.get(x, y) ? Color.BLACK : Color.WHITE);
        }
    }
    

完整的程式碼如下:

public class QRCodeUtils {
    private static final String TAG = "QRCodeUtils";
      /**
     * @param content 字串內容
     * @param size    點陣圖寬&高(單位:px)
     * @return
     */
    public static Bitmap createQRCodeBitmap(String content, int size) {
        if (TextUtils.isEmpty(content)) return null;
        if (size <= 0) return null;
        //建立QRCodeWriter物件
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        try {
            //使用QRCodeWriter將文字內容轉換成BitMatrix.
            BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, size, size);
            Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565);

            for (int x = 0; x < size; x++) {
                for (int y = 0; y < size; y++) {
                    //將BitMatrix渲染到bitmap
                    bitmap.setPixel(x, y, encode.get(x, y) ? Color.BLACK : Color.WHITE);
                }
            }
            return bitmap;
        } catch (Throwable e) {
            Log.e(TAG, "createQRCodeBitmap: ", e);
        }
        return null;
    }
}

給二維碼新增logo 與上篇文章新增水印類似.

都是透過canvas重新繪製,合成圖片.

具體的程式碼如下:

  /**
     * @param srcBitmap   二維碼
     * @param logoBitmap  二維碼logo
     * @param logoPercent 二維碼logo的佔比 [0,1]
     * @return
     */
    public static Bitmap addQRCodeLogo(Bitmap srcBitmap, Bitmap logoBitmap, float logoPercent) {
        //校驗引數合法
        if (srcBitmap == null) return null;
        if (logoBitmap == null) return srcBitmap;
        if (logoPercent < 0 || logoPercent > 1) logoPercent = 0.2f;

        //原圖/logo的寬高
        int srcWidth = srcBitmap.getWidth();
        int srcHeight = srcBitmap.getHeight();
        int logoHeight = logoBitmap.getHeight();
        int logoWidth = logoBitmap.getWidth();

        //縮放
        float scaleWidth = srcWidth * logoPercent / logoWidth;
        float scaleHeight = srcHeight * logoPercent / logoHeight;

        //使用Canvas繪製
        Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(srcBitmap, 0, 0, null);
        canvas.scale(scaleWidth, scaleHeight, srcWidth / 2, srcHeight / 2);
        canvas.drawBitmap(logoBitmap, srcWidth / 2-logoWidth/2, srcHeight / 2-logoHeight/2, null);
        return bitmap;
    }

新增建立二維碼方法:

/**
 * @param content 字串內容
 * @param size    點陣圖寬&高(單位:px)
 * @param logo    二維碼logo
 * @param logoPercent 二維碼logo的佔比 [0,1]
 * @return
 */
public static Bitmap createQRCodeBitmap(String content, int size, Bitmap logo, float logoPercent) {
    Bitmap qrCodeBitmap = createQRCodeBitmap(content, size);
    Bitmap bitmap = addQRCodeLogo(qrCodeBitmap, logo, logoPercent);
    return bitmap;
}

本文由部落格一文多發平臺 OpenWrite 釋出!

相關文章