Zxing仿微信二维码扫描

来源:互联网 发布:淘宝直通车题目答案 编辑:程序博客网 时间:2024/05/02 05:00

1.Zxing的介绍

    百忙之中抽出时间,我觉得还是得把zxing二维码扫描总结一下。Zxing是Google推出来专门为二维码这块的开发很流行的一个框架,,它可以实现使用手机内置摄像头完成二维码的扫描和解码。Zxing的项目地址:https://github.com/zxing/zxing

2.二维码的生成

1.生成二维码工具类:
/** * 生成二维码工具类 * @param content * @param width * @param height * @return */private Bitmap generateBitmap(String content,int width, int height) {    QRCodeWriter qrCodeWriter = new QRCodeWriter();    Map<EncodeHintType, String> hints = new HashMap<>();    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");    try {        BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);        int[] pixels = new int[width * height];        for (int i = 0; i < height; i++) {            for (int j = 0; j < width; j++) {                if (encode.get(j, i)) {                    pixels[i * width + j] = 0x00000000;                } else {                    pixels[i * width + j] = 0xffffffff;                }            }        }        return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);    } catch (WriterException e) {        e.printStackTrace();    }    return null;}
2.添加二维码中间标志的工具类:
/** * 添加二维码中心logo工具类 * @param qrBitmap * @param logoBitmap * @return */private Bitmap addLogo(Bitmap qrBitmap, Bitmap logoBitmap) {    int qrBitmapWidth = qrBitmap.getWidth();    int qrBitmapHeight = qrBitmap.getHeight();    int logoBitmapWidth = logoBitmap.getWidth();    int logoBitmapHeight = logoBitmap.getHeight();    Bitmap blankBitmap = Bitmap.createBitmap(qrBitmapWidth, qrBitmapHeight, Bitmap.Config.ARGB_8888);    Canvas canvas = new Canvas(blankBitmap);    canvas.drawBitmap(qrBitmap, 0, 0, null);    canvas.save(Canvas.ALL_SAVE_FLAG);    float scaleSize = 1.0f;    while ((logoBitmapWidth / scaleSize) > (qrBitmapWidth / 5) || (logoBitmapHeight / scaleSize) > (qrBitmapHeight / 5)) {        scaleSize *= 2;    }    float sx = 1.0f / scaleSize;    canvas.scale(sx, sx, qrBitmapWidth / 2, qrBitmapHeight / 2);    canvas.drawBitmap(logoBitmap, (qrBitmapWidth - logoBitmapWidth) / 2, (qrBitmapHeight - logoBitmapHeight) / 2, null);    canvas.restore();    return blankBitmap;}
ok,两个方法就搞定了二维码的生成:


3.二维码的识别

识别的话就稍微难点,首先把zxing开源项目的识别代码集成到我们的模块中,具体步骤:
1.从github中把Zxing 的开源项目下载下来
2.新建一个新工程,然后在主界面自定义一个跳转Button,用来实现扫描的跳转
3.在刚才新建的项目中创建一个module库。

选择第二项,新建一个zxing依赖库
4.将zxing开源项目里的android,android-core里面的文件复制到依赖库的相对应的文件夹

其中android中要复制的文件:

android-core中要复制的文件

全部复制到依赖库中后:

5.然后将Zxing的jar包复制到依赖库中的libs中,下载地址:http://download.csdn.net/detail/zhangxing52077/9657832
6.将工程与依赖库的最小适配版本统一,然后将工程build.gradle的依赖包全都删除,直接依赖Zxing库:
①工程中:

②依赖库中:

7.再把依赖库中manifest清单文件中application的icon属性移除,然后编译一把
8.这时还是会报错,最后你把依赖库的代码中switch 全部替换成 if.....else if....else的形式,这时就集成好了。
然后呢,效果出来了,你却能发现扫码是横屏的,shit,这也是够了,哈哈,不过别着急,慢慢来,接下来就把横屏转为我们习惯的竖屏:
1.在DecodeHandler.java中,修改decode方法

  PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);之前添加

    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++)
            rotatedData[x * height + height - y - 1] = data[x + y * width];
    }
    int tmp = width; // Here we are swapping, that's the difference to #11
    width = height;
    height = tmp;
    
2.在CameraManager.java中,注释代码:
            // rect.left = rect.left * cameraResolution.x / screenResolution.x;
            // rect.right = rect.right * cameraResolution.x / screenResolution.x;
            // rect.top = rect.top * cameraResolution.y / screenResolution.y;
            // rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
修改为
            rect.left = rect.left * cameraResolution.y / screenResolution.x;
            rect.right = rect.right * cameraResolution.y / screenResolution.x;
            rect.top = rect.top * cameraResolution.x / screenResolution.y;
            rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;

3.在CameraConfigurationManager.java中,在setDesiredCameraParameters方法中添加一句
  camera.setDisplayOrientation(90);

4.在AndroidManifest.xml中,把Activity的属性android:screenOrientation="landscape"
改为
  android:screenOrientation="portrait"

老天垂怜呀!!!屏幕终于竖屏了,可是扫描框不好看,接着苦逼的美化把委屈这里我就不多说,真的蛮累,美化链接地址:http://www.360doc.com/content/14/0904/11/16021371_406951502.shtml,按部就班就ok了。好了,今天就到这里了,我是张星,欢迎关注,后期有什么问题可以私信我!项目源码地址:http://download.csdn.net/detail/zhangxing52077/9657826



2 0
原创粉丝点击