Zxing 近距离扫描二维码

使用zxing扫描二维码,需要远距离扫描才能识别,用户体验不好,在网上搜了一下,发现一个解决办法,最起码现在是可以近距离扫描了。
1.修改扫描框大小,使用各种分辨率
CameraManager.java类中修改

  public Rect getFramingRect() {
   /* Point screenResolution = configManager.getScreenResolution();
    if (framingRect == null) {
      if (camera == null) {
        return null;
      }
      int width = screenResolution.x * 3 / 4;
      if (width < MIN_FRAME_WIDTH) {
        width = MIN_FRAME_WIDTH;
      } else if (width > MAX_FRAME_WIDTH) {
        width = MAX_FRAME_WIDTH;
      }
      int height = screenResolution.y * 3 / 4;
      if (height < MIN_FRAME_HEIGHT) {
        height = MIN_FRAME_HEIGHT;
      } else if (height > MAX_FRAME_HEIGHT) {
        height = MAX_FRAME_HEIGHT;
      }
      int leftOffset = (screenResolution.x - width) / 2;
      int topOffset = (screenResolution.y - height) / 2;
      framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
      Log.d(TAG, "Calculated framing rect: " + framingRect);
    }*/
    if (framingRect == null) {
      if (camera == null) {
        return null;
      }
      Point screenResolution = configManager.getScreenResolution();
      if (screenResolution == null) {
        // Called early, before init even finished
        return null;
      }

      // int width = findDesiredDimensionInRange(screenResolution.x,
      // MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
      // int height = findDesiredDimensionInRange(screenResolution.y,
      // MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);

            /* 扫描框修改 */
      DisplayMetrics metrics = context.getResources().getDisplayMetrics();
      int width = (int) (metrics.widthPixels * 0.7);
      int height = (int) (width );

      int leftOffset = (screenResolution.x - width) / 2;
      int topOffset = (screenResolution.y - height) / 2;
      framingRect = new Rect(leftOffset, topOffset, leftOffset + width,
              topOffset + height);
      Log.d(TAG, "Calculated framing rect: " + framingRect);
    }

修改扫描距离
CameraConfigurationManager.java

void setDesiredCameraParameters(Camera camera) {
   /* Camera.Parameters parameters = camera.getParameters();
    Log.d(TAG, "Setting preview size: " + cameraResolution);
    parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
    setFlash(parameters);
    setZoom(parameters);
    //setSharpness(parameters);
    //modify here
    camera.setDisplayOrientation(90);
    camera.setParameters(parameters);*/

    Camera.Parameters parameters = camera.getParameters();
    List<Camera.Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
    int position =0;
    if(supportedPreviewSizes.size()>2){
      position=supportedPreviewSizes.size()/2+1;//supportedPreviewSizes.get();
    }else {
      position=supportedPreviewSizes.size()/2;
    }

    int width = supportedPreviewSizes.get(position).width;
    int height = supportedPreviewSizes.get(position).height;
    Log.d(TAG, "Setting preview size: " + cameraResolution);
    camera.setDisplayOrientation(90);
    cameraResolution.x=width;
    cameraResolution.y=height;
    parameters.setPreviewSize(width,height);
    setFlash(parameters);
    setZoom(parameters);
    //setSharpness(parameters);
    camera.setParameters(parameters);
  }
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值