Android中生成帶logo的二維碼

轉載請標明出處:http://blog.csdn.net/donkor_/article/details/79799366

▲ 前言

目前在我國內,炒的最火熱的就是二維碼與O2O(Online To Offline)模式的結合,即利用二維碼的讀取將線上的用戶引流給線下的商家。簡單、便捷的小圖標,給多少大街小巷的攤販,各種華麗shopping商城帶來方便。再加上現有軟件大部分支持掃描二維碼,可以看出二維碼帶給軟件推廣和運營減輕不少成本。

▲ 使用二維碼的優勢

  • 高密度編碼,信息容量大。

  • 編碼範圍廣。

  • 容錯能力強,具有糾錯功能。

  • 譯碼可靠性高。

  • 可引入加密措施。

  • 成本低,易製作,持久耐用。

▲ Android中使用

首先需要下載zxing的jar包

Github地址 : https://github.com/zxing/zxing

百度網盤下載鏈接 : https://pan.baidu.com/s/1ztPlnH2s-DDxbgoRv5mnSQ 密碼 : u9d1

複製zxing.jar包到libs目錄下,這時候就可以愉快的敲代碼啦~

二維碼生成工具類

public class EncodingUtils {

    /**
     * 創建二維碼
     *
     * @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;
            }
            // 配置參數
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            // 容錯級別
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            // 圖像數據轉換,使用了矩陣轉換
            BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix,
                    heightPix, hints);
            int[] pixels = new int[widthPix * heightPix];
            // 下面這裏按照二維碼的算法,逐個生成二維碼的圖片,
            // 兩個for循環是圖片橫列掃描的結果
            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保存到文件中再進行讀取。直接返回的bitmap是沒有任何壓縮的,內存消耗巨大!
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 在二維碼中間添加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;
    }
}

Activity中使用

//第一個參數爲二維碼鏈接
//第二、三個參數爲二維碼寬高
//第四個參數爲二維碼中心logo
EncodingUtils.createQRCode("https://blog.csdn.net/donkor_", 500, 500,
                BitmapFactory.decodeResource(getResources(), R.mipmap.qr_logo));

查看效果圖


About me
Email :[email protected]
Android開發交流QQ羣 : 537891203
Android開發交流羣

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章