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

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


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

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


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

 下面是关于属性的介绍

ScanView核心源码解析


ScanView 自定义属性


        
        
        
        
        
        
        
        
        
        
        
            
            
            
            
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
            
            
        
        
        
            
            
        
        
        
        
        
            
            
        
    


绘制 扫描区域 周围 

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

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 


完整库文件下载地址

穿越吧


你可能感兴趣的:(android,Android扫码)