Android 集成二维码,含logo二维码

方法二:

添加zxing依赖:

compile 'cn.yipianfengye.android:zxing-library:2.2'
Bitmap mBitmap = CodeUtils.createImage("sssss77777", 400, 400, null);//无logo
mBitmap = CodeUtils.createImage("ssss6666", 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));//logo
imageView.setImageBitmap(mBitmap);

方法一:

zxing.jar下载地址:https://pan.baidu.com/s/1_XLBnj7daO6bzP-tlRwKzg

下载下来复制到libs文件夹下,并Add As Library

<ImageView
    android:id="@+id/iv_qrcode"
    android:scaleType="fitXY"
    android:layout_width="150dp"
    android:layout_height="150dp"/>
        ImageView ivQrCode = (ImageView)findViewById(R.id.iv_qrcode);

        Bitmap bitmap = strTo2DCode("aabbbb", 800, 800);
        ivQrCode.setImageBitmap(addLogo(bitmap,
                BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)));//有logo

//        ivQrCode.setImageBitmap(bitmap);//无logo

1、生成二维码方法:

/**
 * 字符串生成二维码
 *
 * @param strEncode 要生成二维码的字符串
 * @param width     要生成图片的宽度
 * @param height    要生成图片的高度
 * @return
 */
public static Bitmap strTo2DCode(String strEncode, int width, int height) {
    Bitmap bitmap = null;
    try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(new String(strEncode.getBytes("UTF-8"), "iso-8859-1"), BarcodeFormat.QR_CODE, width, height);
        // 二维矩阵转为一维像素数组,也就是一直横着排了
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * width + x] = 0xff000000;
                } else {
                    pixels[y * width + x] = 0x00000000;
                }

            }
        }
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        // 通过像素数组生成bitmap,具体参考api
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return bitmap;
}

2、向二维码中心添加logo方法:

/**
 * 在二维码中间添加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;
}

猜你喜欢

转载自blog.csdn.net/meixi_android/article/details/82697654
今日推荐