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

Zxing框架進行二維碼掃描時候會發現,隨着分辨率的增長,掃描框會愈來愈小,SurfaceView掃描窗口就看不見了,咱們能夠本身定義掃描窗口的大小,以及適配屏幕問題。框架

 

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

 掃描框框初始化數值code

  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方法用來設置掃描的框的大小,若是要修改掃描框的大小能夠在這個方法裏修改blog

  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; }

 

要讓掃描框適應不一樣的分辨率,咱們須要根據分辨率將掃描框的初始值轉化就好。get

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


dp轉爲px的方法爲io

  public static  int Dp2Px( Context context,float dp) { 
        final float scale = context.getResources().getDisplayMetrics().density; 
        return (int) (dp * scale + 0.5f); 
    }
相關文章
相關標籤/搜索