Android Zxing調整掃描區域 優化取圖速度(調節掃描框大小)

Zxing 是google提供的二維碼掃描工程

Demo本身默認的掃圖區域最大隻有 360*480    需要拉開很遠的距離才能將整個二維碼掃描到

因此需要我們自己調整取圖大小

 

在CameraManager.java這個類中進行調整

默認的大小是 以下這4個參數 

[html] view plaincopy在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 plaincopy在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 plaincopy在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 * 7 / 10;  
  10.   int height = screenResolution.y * 7 / 10;  
  11.     
  12.   int leftOffset = (screenResolution.x - width) / 2;  
  13.   int topOffset = (screenResolution.y - height) / 3;  
  14.   framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);  
  15.       
  16.       
  17.     Log.d(TAG, "Calculated framing rect: " + framingRect);  
  18.   }  
  19.   return framingRect;  
  20. }  

寬高 佔據了屏幕的   7/10 
當然...取圖改的這麼大   會多佔一點內存....相應的掃描的時候快得多

以上是實際讀取圖片的大小

實際的界面美化 在ViewfinderView 這個類當中進行繪製

不足之處請在下方留言  謝謝

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