Zxing掃描

在我們是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. }  

當然...取圖改的這麼大   會多佔一點內存....相應的掃描的時候快得多


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章