我是靠谱客的博主 愉快保温杯,最近开发中收集的这篇文章主要介绍ZXing扫描的优化,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

斑马线扫描优化 

  1.  将AndroidManifest中的屏幕对齐方式改为android:screenOrientation =“portrait”;
  2. CameraConfigurationManager类的setDesiredCameraParameters()方法中在camera.setParameters(参数)之前加入设置相机后摄像头的方向为90       theCamera.setDisplayOrientation(90);
  3. CameraManager 类的 getFramingRectInPreview() 方法中原来的方法注销更替为新代码,这样写的目的其实就是将相机旋转90之后,获取宽高也需要变更。
    /*rect.left = rect.left * cameraResolution.x / screenResolution.x;
     rect.right = rect.right * cameraResolution.x / screenResolution.x;
     rect.top = rect.top * cameraResolution.y / screenResolution.y;
     rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;*/
    rect.left = rect.left * cameraResolution.y / screenResolution.x;
    rect.right = rect.right * cameraResolution.y / screenResolution.x;
    rect.top = rect.top * cameraResolution.x / screenResolution.y;
    rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;
  4. DecodeHandler 类的 解码 方法中在activity.getCameraManager()。buildLuminanceSource()之前添加以下代
     
        
    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++)
        rotatedData[x * height + height - y - 1] = data[x + y * width];
    
    }
    int tmp = width;
    width = height;
    height = tmp;
    data = rotatedData;
  5. 解决竖屏拉伸问题
    CameraConfigurationManager类的initFromCameraParameters()方法中:
    在Log.i(TAG,“屏幕分辨率”+ screenResolution);之后添加以下代码:
     
        
    Point screenResolutionForCamera = new Point();
    screenResolutionForCamera.x = screenResolution.x;
    screenResolutionForCamera.y = screenResolution.y;
    if (screenResolution.x < screenResolution.y) {
      screenResolutionForCamera.x = screenResolution.y;
      screenResolutionForCamera.y = screenResolution.x;
    }
    同时修改下一句为cameraResolution = findBestPreviewSizeValue(parameters,screenResolutionForCamera);
  6. 框扫描位置大小:修改 此时的扫描框的竖直拉伸的矩形,很难看,我们可以将其修改为正方形或扁平型的。
    CameraManager 类的getFramingRect()方法中替换以下代码:
     
       
    /* 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.625);
     int height = (int) (width * 0.9);
    此处我们根据屏幕分辨率来定扫描框大小更灵活一点,同时将偏移量topOffset修改为 int topOffset = (screenResolution.height) / 3;
  7. 框扫描四个角状语从句:扫描线条修改
    。示例代码中的线条的英文居中且不动的,我们可以将其修改为上下移动的扫描线,可以御姐改变线条的样式
    在自定义扫描布局ViewfinderView类中的的onDraw()中方法绘制四个角,关键代码如下:
    //左上角
     canvas.drawRect(frame.left,frame.top,frame.left+ CORNER_WIDTH,frame.top+ CORNER_HEIGHT,paint);
     canvas.drawRect(frame.left,frame.top,frame.left + CORNER_HEIGHT,frame.top + CORNER_WIDTH,paint);
    //左下角
     canvas.drawRect(frame.left,frame.bottom - CORNER_HEIGHT,frame.left + CORNER_WIDTH,frame.bottom,paint);
     canvas.drawRect(frame.left,frame.bottom - CORNER_WIDTH,frame.left + CORNER_HEIGHT,frame.bottom,paint);
    //右上角
     canvas.drawRect(frame.right- CORNER_WIDTH,frame.top,frame.right,frame.top+ CORNER_HEIGHT,paint);
     canvas.drawRect(frame.right - CORNER_HEIGHT,frame.bottom,frame.right,frame.top + CORNER_WIDTH,paint);
    //右下角
     canvas.drawRect(frame.right - CORNER_HEIGHT,frame.bottom - CORNER_WIDTH,frame.right,frame.bottom,paint);
     canvas.drawRect(frame.right - CORNER_WIDTH,frame.bottom - CORNER_HEIGHT,frame.right,frame.bottom,paint);
    

最后

以上就是愉快保温杯为你收集整理的ZXing扫描的优化的全部内容,希望文章能够帮你解决ZXing扫描的优化所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部