Zxing扫描

来源:互联网 发布:king新域名 编辑:程序博客网 时间:2024/05/18 05:32

在我们是Zxing框架进行二维码扫描的时候,会发现,现在手机随着分辨率的增加,那个扫描框会越来越小解决办法:

1:找到CameraManager最上面四行参数就是设置宽高的,

 private static final int MIN_FRAME_WIDTH = (int)  DensityUtils.dp2px(MyApplication.getInstance(),180);  private static final int MIN_FRAME_HEIGHT = (int)  DensityUtils.dp2px(MyApplication.getInstance(),180);  private static final int MAX_FRAME_WIDTH = (int)  DensityUtils.dp2px(MyApplication.getInstance(),240);  private static final int MAX_FRAME_HEIGHT = (int)  DensityUtils.dp2px(MyApplication.getInstance(),240);


第二个问题:每次扫描后图片都会压缩下

解决方法:在Zxing包下的camera包下找到CameraConfigurationManager.java类,修改:

搜索initFromCameraParameters 这个方法,在该方法下找到  Log.d(TAG, "Screen resolution: " + 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;          }  

然后下面有一行这样的代码:

[java] view plaincopy技术分享技术分享
  1. cameraResolution = getCameraResolution(parameters, screenResolution);  

中的screenResolution改为  screenResolutionForCamera


第三个问题:调整扫描区域 优化取图速度


Zxing 是google提供的二维码扫描工程

Demo本身默认的扫图区域最大只有 360*480    需要拉开很远的距离才能将整个二维码扫描到

因此需要我们自己调整取图大小

 

在CameraManager.java这个类中进行调整

默认的大小是 以下这4个参数 

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //  private static final int MIN_FRAME_WIDTH = 240;  
  2. //  private static final int MIN_FRAME_HEIGHT = 240;  
  3. //  private static final int MAX_FRAME_WIDTH = 480;  
  4. //  private static final int MAX_FRAME_HEIGHT = 360;  


参数实际在 getFramingRect() 方法中起作用

以下是原本Demo中提供的

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.   * Calculates the framing rect which the UI should draw to show the user where to place the 
  3.   * barcode. This target helps with alignment as well as forces the user to hold the device 
  4.   * far enough away to ensure the image will be in focus. 
  5.   * 
  6.   * @return The rectangle to draw on screen in window coordinates. 
  7.   */  
  8.  public Rect getFramingRect() {  
  9.    Point screenResolution = configManager.getScreenResolution();  
  10.    if (framingRect == null) {  
  11.      if (camera == null) {  
  12.        return null;  
  13.      }  
  14.        
  15.      //原生  
  16.      int width = screenResolution.x * 3 / 4;  
  17.      if (width < MIN_FRAME_WIDTH) {  
  18.        width = MIN_FRAME_WIDTH;  
  19.      } else if (width > MAX_FRAME_WIDTH) {  
  20.        width = MAX_FRAME_WIDTH;  
  21.      }  
  22.      int height = screenResolution.y * 3 / 4;  
  23.      if (height < MIN_FRAME_HEIGHT) {  
  24.        height = MIN_FRAME_HEIGHT;  
  25.      } else if (height > MAX_FRAME_HEIGHT) {  
  26.        height = MAX_FRAME_HEIGHT;  
  27.      }  
  28.      int leftOffset = (screenResolution.x - width) / 2;  
  29.      int topOffset = (screenResolution.y - height) / 2;  
  30.      framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);  
  31.      Log.d(TAG, "Calculated framing rect: " + framingRect);  
  32.    }  
  33.    return framingRect;  
  34.  }  

我将代码改成了

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public Rect getFramingRect() {  
  2.   Point screenResolution = configManager.getScreenResolution();  
  3.   if (framingRect == null) {  
  4.     if (camera == null) {  
  5.       return null;  
  6.     }  
  7.       
  8.   //修改之后    
  9.    int width = screenResolution.x * 3 / 4;  
  10.     
  11.   int leftOffset = (screenResolution.x - width) / 2;  
  12.   int topOffset = (screenResolution.y - width ) / 3;  
  13.   framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + width );  
  14.       
  15.       
  16.     Log.d(TAG, "Calculated framing rect: " + framingRect);  
  17.   }  
  18.   return framingRect;  
  19. }  

当然...取图改的这么大   会多占一点内存....相应的扫描的时候快得多


0 0
原创粉丝点击