【Android-Zxing框架】二維碼掃描框區域大小與不同手機分辨率適配問題


Zxing框架進行二維碼掃描時候會發現,隨着分辨率的增加,掃描框會越來越小,SurfaceView掃描窗口就看不見了,我們可以自己定義掃描窗口的大小,以及適配屏幕問題。

 

 Zxing包中有個類CameraManager,它是來設置掃描框的大小

 掃描框框初始化數值

  private static  int MIN_FRAME_WIDTH = 240;
  private static  int MIN_FRAME_HEIGHT = 240;
  private static  int MAX_FRAME_WIDTH = 480;
  private static  int MAX_FRAME_HEIGHT = 360;

 

此類里面有個getFramingRect方法用來設置掃描的框的大小,如果要修改掃描框的大小可以在這個方法里修改

  public Rect getFramingRect() {
    Point screenResolution = configManager.getScreenResolution();
    if (framingRect == null) {
      if (camera == null) {
        return null;
      }

      MIN_FRAME_WIDTH = Dp2Px(context,180);
      MIN_FRAME_HEIGHT =Dp2Px(context,180);
      MAX_FRAME_WIDTH = Dp2Px(context,280);
      MAX_FRAME_HEIGHT =Dp2Px(context,240);

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); } return framingRect; }

 

要讓掃描框適應不同的分辨率,我們需要根據分辨率將掃描框的初始值轉化就好。

      MIN_FRAME_WIDTH = Dp2Px(context,180);
      MIN_FRAME_HEIGHT =Dp2Px(context,180) ;
      MAX_FRAME_WIDTH = Dp2Px(context,280);
      MAX_FRAME_HEIGHT =Dp2Px(context,240) ;


dp轉為px的方法為

  public static  int Dp2Px( Context context,float dp) { 
        final float scale = context.getResources().getDisplayMetrics().density; 
        return (int) (dp * scale + 0.5f); 
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM