使用ZXing扫描条形码和二维码

主要是介绍使用ZXing库进行条形码和二维码的扫描

本帖里面使用的库是别人精简以后的

在代码注释里面作者 - Ryan.Tang

这里主要是介绍一下关于扫描界面

下面是关于属性的介绍

ScanView核心源码解析

ScanView 自定义属性

<declare-styleable name="ScanView">
        <!--指定扫描区域的背景图片-->
        <attr name="scanBackground" format="reference" />
        <!--指定扫描区域的尺寸-->
        <attr name="scanWidth" format="dimension" />
        <attr name="scanHeight" format="dimension" />
        <!--指定扫描区域周围的颜色-->
        <attr name="aroundColor" format="color" />
        <!--扫描区域到顶部的距离-->
        <attr name="scanTopDistance" format="dimension" />
        <!--扫描的模式-->
        <attr name="scanMode" format="enum">
            <enum name="none" value="0" /><!--什么都没有-->
            <enum name="line" value="1" /><!--有扫描线-->
            <enum name="point" value="2" /><!--有闪烁点-->
            <enum name="both" value="3" /><!--都有-->
        </attr>
        <!--扫描线的高度-->
        <attr name="lineHeight" format="dimension" />
        <!--扫描线的颜色-->
        <attr name="lineColor" format="color" />
        <!--边框宽度-->
        <attr name="frameBorder" format="dimension" />
        <!--边框颜色-->
        <attr name="frameColor" format="color" />
        <!--指定扫描线的图片-->
        <attr name="lineSrc" format="reference" />
        <!--指定闪烁点的颜色-->
        <attr name="pointColor" format="color" />
        <!--扫描模式状态-->
        <attr name="scanStatus" format="enum">
            <enum name="slow" value="1" /><!--慢-->
            <enum name="normal" value="0" /><!--正常-->
            <enum name="fast" value="2" /><!--快-->
        </attr>
        <!--扫描线的滚动模式-->
        <attr name="lineScrollMode" format="enum">
            <enum name="repeat" value="0" /><!--重复-->
            <enum name="cycle" value="1" /><!--循环-->
        </attr>
        <!--扫描超时时间-->
        <attr name="scanTimeValue" format="integer" />
        <!--扫描时间单位 支持 秒 分钟-->
        <attr name="scanTimeUnit" format="enum">
            <enum name="second" value="0" />
            <enum name="minute" value="1" />
        </attr>
    </declare-styleable>

绘制 扫描区域 周围

分为 是否设置背景图片俩种情况

private void drawAround(Canvas canvas) {

        if (mScanBg == null) {
            canvas.drawRect(0, 0, screenWidth, scanTopDistance - frameBorder, mPaintShape);
            canvas.drawRect(0, scanTopDistance - frameBorder, scanHorizontalDsitance - frameBorder,
                    scanTopDistance + scanHeight + frameBorder, mPaintShape);
            canvas.drawRect(scanHorizontalDsitance + scanWidth + frameBorder, scanTopDistance - frameBorder, screenWidth,
                    scanTopDistance + scanHeight + frameBorder, mPaintShape);
            canvas.drawRect(0, scanTopDistance + scanHeight + frameBorder, screenWidth, screenHeight, mPaintShape);
        } else {
            // 绘制周边
            canvas.drawRect(0, 0, screenWidth, scanTopDistance, mPaintShape);
            canvas.drawRect(0, scanTopDistance, scanHorizontalDsitance,
                    scanTopDistance + scanHeight, mPaintShape);
            canvas.drawRect(scanHorizontalDsitance + scanWidth, scanTopDistance, screenWidth,
                    scanTopDistance + scanHeight, mPaintShape);
            canvas.drawRect(0, scanTopDistance + scanHeight, screenWidth, screenHeight, mPaintShape);
        }
    }

绘制 扫描区域

分为 是否设置背景图片 如果没有设置背景图片 则根据指定大小或者是默认大小绘制一个方框

private void drawScanArea(Canvas canvas) {
        // 绘制扫描区域
        if (mScanBg == null) { // 没有设置背景图片使用默认的方框
            mPaintShape.setStyle(Paint.Style.STROKE);
            mPaintShape.setStrokeWidth(frameBorder);
            mPaintShape.setColor(frameColor);
            //  画一个边框
            canvas.drawRect(scanHorizontalDsitance - frameBorder,
                    scanTopDistance - frameBorder,
                    scanHorizontalDsitance + scanWidth + frameBorder,
                    scanTopDistance + scanHeight + frameBorder, mPaintShape);
        } else { // 设置背景
            canvas.drawBitmap(mScanBg, scanHorizontalDsitance, scanTopDistance, mPaintBitmap);
        }
    }

绘制扫描线

    private void drawLine(Canvas canvas) {
        if (mScanLine != null) {
            canvas.drawBitmap(mScanLine,
                    scanHorizontalDsitance + (scanWidth - mScanLine.getWidth()) / 2,
                    linePositionHeight, mPaintBitmap);
        } else {
            mPaintShape.setStyle(Paint.Style.FILL);
            mPaintShape.setColor(lineColor);
            canvas.drawLine(scanHorizontalDsitance, linePositionHeight,
                    scanHorizontalDsitance + scanWidth, linePositionHeight + lineHeight, mPaintShape);
        }
        switch (lineSrollMode) {
            case LINE_SCROLL_MODE_REPEAT:
                if (linePositionHeight > scanTopDistance + scanHeight)
                    linePositionHeight = scanTopDistance;
                linePositionHeight += lineMoveSpeed;
                break;
            case LINE_SCROLL_MODE_CYCLE:
                if (!upFlag) {
                    linePositionHeight += lineMoveSpeed;
                } else {
                    linePositionHeight -= lineMoveSpeed;
                }
                if (linePositionHeight > scanTopDistance + scanHeight)
                    upFlag = true;
                if (linePositionHeight < scanTopDistance)
                    upFlag = false;
                break;
        }
    }

在 drawLine 通过控制可以矩形的高度可以实现和UC 扫一扫一样的效果

添加如下代码

mPaintShape.setColor(Color.parseColor("#4455ff55"));
        canvas.drawRect(scanHorizontalDsitance,
                scanTopDistance,
                scanHorizontalDsitance + scanWidth,
                linePositionHeight, mPaintShape);

绘制闪烁点

    private void drawPoint(Canvas canvas) {
        if (currentPoints.isEmpty()) {
            lastPoints = null;
        } else {
            // 记录当前的数据到上一次位置集合中
            lastPoints = currentPoints;
            // 设置绘制闪烁点的画笔
            mPaintShape.setAlpha(0xff);
            mPaintShape.setColor(pointColor);
            mPaintShape.setStyle(Paint.Style.FILL);
            // 循环绘制
            for (ResultPoint point : currentPoints) {
                // 绘制闪烁点
                canvas.drawCircle(scanHorizontalDsitance + point.getX(),
                        scanTopDistance + point.getY(), 6.0f, mPaintShape);
            }

        }
        if (lastPoints != null) {
            // 设置绘制闪烁点的画笔
            mPaintShape.setAlpha(0xff / 2);
            mPaintShape.setColor(pointColor);
            mPaintShape.setStyle(Paint.Style.FILL);
            // 循环绘制
            for (ResultPoint point : lastPoints) {
                // 绘制闪烁点
                canvas.drawCircle(scanHorizontalDsitance + point.getX(),
                        scanTopDistance + point.getY(), 3.0f, mPaintShape);
            }
        }

        // 清空本次数据
        currentPoints.clear();
    }

闪烁点的数据来源

这个方法内部自动调用

public void addPossibleResultPoint(ResultPoint point) {
        //  添加该点到集合中
        currentPoints.add(point);
    }

在使用的时候 跳转 CaptureActivity

完整库文件下载地址

穿越吧

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-29 09:47:58

使用ZXing扫描条形码和二维码的相关文章

利用Zxing扫描和产生二维码

这段时间我做的一个app需要用到二维码扫描,但是找遍了我们的51CTO都没有找到合适的文章,我想把我自己利用zxing做二维码扫描的方法提供出来供大家参考. 什么是Z?在Android平台做过二维码相关模块的肯定都熟知ZXing开源项目,Z是一个开源Java类库用于解析多种格式的1D/2D条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括: .NET and C#.J2ME.J2SE和Android等.其GitHub地址是:http

ZXing生成条形码、二维码、带logo二维码

采用的是开源的ZXing,Maven配置如下,jar包下载地址,自己选择版本下载,顺便推荐下Maven Repository <!-- https://mvnrepository.com/artifact/com.google.zxing/core --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version

iOS 使用ZBarSDK扫描条形码和二维码

ZBarSDK,一个比较优秀的开源项目,使用起来也很简单. ZBarSDK是一个开源的SDK,可从这里下载到源码,该SDK实现了识别和读取各种条形码,包括EAN-13/UPC-A, UPC-E, EAN-8, Code 128, Code 39, Interleaved 2 of 5 和 QR Code. 帮助文档:http://zbar.sourceforge.net/iphone/sdkdoc/index.html Step1.使用ZBarSDK 需要导入的framework 1.AVFou

条形码、二维码扫描

条形码.二维码扫描 使用了zxing扫描框架,能够扫描条形码和二维码的信息,直接拷贝就能使用,如果项目中需要调用摄像头扫描的就可以直接使用了.  下载地址:http://www.devstore.cn/code/info/826.html  运行截图:   

Android基于Google Zxing实现二维码/条形码扫描、生成二维码/条形码

 二维码/条形码生成器 二维码/条形码扫描器 一.二维码与条形码工作原理 目前的很多应用上都有扫码功能,当时微信推出二维码扫码功能时,觉得imagine,通过一张简单的图片就能扫描添加还有,还有分享名片功能(也是一张二维码图片,识别扫描). 下面小编将通过文章主要介绍QRCode方面技术. QRCode是被广泛应用的一种二维码,解码速度快.二维码相对于条形码来说,二维码的存储数据量更大,空间利用率高,有一定的容错性. 二维码原理介绍: 二维码是用某种特定的几何图形按一定的规律在平面上分布的黑

用C#实现的条形码和二维码编码解码器

转自:http://www.cnblogs.com/tuyile006/archive/2013/01/16/2863367.html 本篇介绍可以在C#中使用的1D/2D编码解码器.条形码的应用已经非常普遍,几乎所有超市里面的商品上面都印有条形码:二维码也开始应用到很多场合,如火车票有二维码识别.网易的首页有二维码图标,用户只需要用手机扫描一下就可以看到手机版网易的网址,免去了输入长串字符的麻烦. 条形码的标准: 条形码的标准有ENA条形码.UPC条形码.二五条形码.交叉二五条形码.库德巴条形

使用zxing生成和解析二维码

二维码: 是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的: 在代码编制上巧妙的利用构成计算机内部逻辑基础的0和1比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值信息,通过图像输入设备或光电扫描设备自动识读以实现信息自动处理: 二维码能够在横向和纵向两个方位同时表达信息,因此能在很小的面积内表达大量的信息: 二维码相对于条形码的优势就是省空间: zxing简介: zxing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库

[转]用C#实现的条形码和二维码编码解码器

条形码的标准: 条形码的标准有ENA条形码.UPC条形码.二五条形码.交叉二五条形码.库德巴条形码.三九条形码和128条形码等,而商品上最常使用的就是EAN商品条形码.EAN商品条形码亦称通用商品条形码,由国际物品编码协会制定,通用于世界各地,是目前国际上使用最广泛的一种商品条形码.我国目前在国内推行使用的也是这种商品条形码.EAN商品条形码分为EAN-13(标准版)和EAN-8(缩短版)两种. 二维码的编码标准: 全球现有的二维码多达200种以上,其中常见的技术标准有PDF417(美系标准),

基于opencv3.0和zbar下的条形码与二维码识别

其中对条码与二维码的识别分为以下4个步骤 1. 利用opencv和Zbar(或者Zxing)对标准的条形码图片(即没有多余背景干扰,且图片没有倾斜)进行解码,将解码信息显示出来,并与原始信息对比. 2. 利用opencv和Zbar(或者Zxing)对标准的QR二维码图片(即没有多余背景干扰,且图片没有倾斜)进行解码,将解码信息显示出来,并与原始信息对比. 3. 对非标准条形码,进行定位,然后用Zbar(或者Zxing)解码显示. 4. 对非标准的QR二维码图片,进行定位,然后用Zbar(或者Zx