生成二維碼圖片(android)

 

一、在項目app的build.gradle裏添加Zxing依賴庫;

implementation 'com.google.zxing:core:3.3.0'

 

二、編寫一個生成二維碼的工具類

public class CreatQrCodeUtil {

//生成普通二維碼bitmap,中心不帶logo圖片
public static Bitmap CreatEwmImg(String QrCodeMsg) {
    Bitmap bitmap=null;
    BitMatrix result=null;
    MultiFormatWriter multiFormatWriter=new MultiFormatWriter();
    try {
        result=multiFormatWriter.encode(QrCodeMsg,BarcodeFormat.QR_CODE,800,800);//800,800 爲設定的bitmap寬高,可根據需求更改
        int w=result.getWidth();
        int h=result.getHeight();
        int[] pixels=new int[w*h]; //像素值
        for (int y = 0; y < h; y++) {
            int offset=y*w;
            for (int x = 0; x <w ; x++) {
                pixels[offset+x]=result.get(x,y)? Color.BLACK:Color.WHITE;//根據黑白混色比列生成常見的二維碼圖片
            }
        }
        bitmap=Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);//固定色值,設置8888
        bitmap.setPixels(pixels,0,w,0,0,w,h);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

 

//生成中心帶logo圖案的二維碼圖片(bitmap)

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.restore();
    } catch (Exception e) {
        bitmap = null;
        e.getStackTrace();
    }
    return bitmap;
}


}

 

 

三、用法

用ImageView的 setImageBitmap() 方法加載工具類生成的bitmap格式圖片,eg:

//生成普通二維碼bitmap,中心不帶logo圖片
Bitmap bm = CreatQrCodeUtil.CreatEwmImg("MrHao:二維碼圖片生成");
ImageView.setImageBitmap(bm);

 

 

//生成中心帶logo圖案的二維碼圖片

Bitmap bm = CreatQrCodeUtil.CreateQrCode("MrHao:二維碼圖片生成", 600, 600, bitmap);
ImageView.setImageBitmap(bm);

 

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