使用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 


完整库文件下载地址

穿越吧


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值