Android生成二维码

来源:互联网 发布:高铭的书是编的 知乎 编辑:程序博客网 时间:2024/05/21 15:39

说到二维码大家都不陌生,大街小巷都可以看到带有二维码的广告,我们只要通过手机扫描一下就可以读取到二维码包含的信息,非常便捷。

在Android开发中我们也经常会使用到二维码,今天主要讲一下Android怎么生成二维码?

在讲怎么生成之前需要提到一个jar包,core.jar,生成二维码基本上就是靠这个jar包。


一、生成普通二维码

什么是普通的二维码?有时候我们看到的二维码会包含一个logo图案,看到这你应该知道没有包含logo的就是所谓的普通二维码。

下面看下生成普通二维码的代码:

private Bitmap createQRImage(String string) {    Bitmap bitmap = null;    int QR_WIDTH = 400, QR_HEIGHT = 400;    try {        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");        // 图像数据转换,使用了矩阵转换        BitMatrix bitMatrix = new QRCodeWriter().encode(string, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);        int[] pixels = new int[QR_WIDTH * QR_HEIGHT];        // 下面这里按照二维码的算法,逐个生成二维码的图片,        // 两个for循环是图片横列扫描的结果        for (int y = 0; y < QR_HEIGHT; y++) {            for (int x = 0; x < QR_WIDTH; x++) {                if (bitMatrix.get(x, y)) {                    pixels[y * QR_WIDTH + x] = 0xff000000;                } else {                    pixels[y * QR_WIDTH + x] = 0xffffffff;                }            }        }        // 生成二维码图片的格式,使用ARGB_8888        bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);        bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);        return bitmap;    } catch (WriterException e) {        e.printStackTrace();    }    return null;}

根据代码我们可以知道,生成二维码只需要传入一个字符串,正常结果会返回一个Bitmap对象。


二、生成带图片logo二维码

public Bitmap createQRImage(String string, Bitmap mBitmap, BarcodeFormat format)        throws WriterException {    Matrix m = new Matrix();    float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth();    float sy = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getHeight();    //设置缩放信息    m.setScale(sx, sy);    //logo图片按martix设置的信息缩放    mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), m, false);    MultiFormatWriter writer = new MultiFormatWriter();    Hashtable<EncodeHintType, String> hst = new Hashtable<EncodeHintType, String>();    //设置字符编码    hst.put(EncodeHintType.CHARACTER_SET, "UTF-8");    //生成二维码矩阵信息    BitMatrix matrix = writer.encode(string, format, 400, 400, hst);    int width = matrix.getWidth();//矩阵高度    int height = matrix.getHeight();//矩阵宽度    int halfW = width / 2;    int halfH = height / 2;    //定义数组长度为矩阵高度*矩阵宽度,用于记录矩阵中像素信息    int[] pixels = new int[width * height];    //从行开始迭代矩阵    for (int y = 0; y < height; y++) {        //迭代列        for (int x = 0; x < width; x++) {            if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH                    && y > halfH - IMAGE_HALFWIDTH                    && y < halfH + IMAGE_HALFWIDTH) {                //该位置用于存放图片信息记录图片每个像素信息                pixels[y * width + x] = mBitmap.getPixel(x - halfW                        + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);            } else {                //如果有黑块点,记录信息                if (matrix.get(x, y)) {                    //记录黑块信息                    pixels[y * width + x] = 0xff000000;                }            }        }    }    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);    // 通过像素数组生成bitmap    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);    return bitmap;}


以上调用方式如下:

//生成二维码try {    Bitmap bitmap = createQRImage(contents, mLogoBitmap, BarcodeFormat.QR_CODE);    mQRImgIv.setImageBitmap(bitmap);} catch (WriterException e) {    e.printStackTrace();}

源码下载地址

0 0
原创粉丝点击