美文网首页
Zxing自定义扫描框

Zxing自定义扫描框

作者: 取了个很好听的名字 | 来源:发表于2019-06-03 14:53 被阅读0次

需求说明

如果大家看过扫一扫功能,会看到他的扫描框有四个边框和一条扫描线从上到下循环移动,现在我们就来实现这种效果。


微信扫一扫.jpg

修改过程

首先在zxing项目中找到ViewfinderView.java,在ondraw方法中的最底部添加如下代码,paint的颜色和粗细自己调节就好。

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      //画笔
      Paint pathPaint=new Paint();
      pathPaint.setStyle(Paint.Style.STROKE);
      pathPaint.setColor(getResources().getColor(R.color.FF8300));
      pathPaint.setStrokeWidth(8f);

      //左上角
      Path leftPath=new Path();
      leftPath.moveTo(frame.left+70,frame.top-10);
      leftPath.lineTo(frame.left-10,frame.top-10);
      leftPath.lineTo(frame.left-10,frame.top+70);
      canvas.drawPath(leftPath,pathPaint);
      //右上角
      Path rightPath=new Path();
      rightPath.moveTo(frame.right-70,frame.top-10);
      rightPath.lineTo(frame.right+10,frame.top-10);
      rightPath.lineTo(frame.right+10,frame.top+70);
      canvas.drawPath(rightPath,pathPaint);
      //左下角
      Path leftBottomPath=new Path();
      leftBottomPath.moveTo(frame.left+70,frame.bottom+10);
      leftBottomPath.lineTo(frame.left-10,frame.bottom+10);
      leftBottomPath.lineTo(frame.left-10,frame.bottom-70);
      canvas.drawPath(leftBottomPath,pathPaint);
      //右下角
      Path rightBottomPath=new Path();
      rightBottomPath.moveTo(frame.right+10,frame.bottom-70);
      rightBottomPath.lineTo(frame.right+10,frame.bottom+10);
      rightBottomPath.lineTo(frame.right-70,frame.bottom+10);
      canvas.drawPath(rightBottomPath,pathPaint);
    }

调整过后的界面如图


添加四个角后.jpg

下面就再来添加扫描先吧,首先去掉原先默认的扫描线,将else中的截屏部分删除掉。这就是默认的扫描线


删除扫描线.png

在添加如下的代码:

   //自定义激光线
      if (scanLineTop == 0) {
        scanLineTop = frame.top;
      }

      if (scanLineTop >= frame.bottom - 30) {
        scanLineTop = frame.top;
      } else {
        scanLineTop += SCAN_VELOCITY;// SCAN_VELOCITY可以在属性中设置,默认为5
      }
      Rect scanRect = new Rect(frame.left, scanLineTop, frame.right, scanLineTop + 30);
      Bitmap scanLine= BitmapFactory.decodeResource(getResources(),R.drawable.scanline);
      canvas.drawBitmap(scanLine, null, scanRect, paint);

其中 SCAN_VELOCITY是每次移动的位移量初始值为5 scanLineTop 是现在扫描线所在的位置,初始值为0。
修改后的效果如图所示:


自定义扫描线.jpg

但是还有一点瑕疵,就是zxing这个项目的默认扫描线有一些橘红色麻点,在使用自定义扫描线的时候最好将这些麻点去除掉。

 resultPointColor = resources.getColor(R.color.possible_result_points);

改为

resultPointColor = resources.getColor(R.color.transparent);

好了自定义扫描框就完成了。

相关文章

网友评论

      本文标题:Zxing自定义扫描框

      本文链接:https://www.haomeiwen.com/subject/duscxctx.html