1. 程式人生 > >android 二維碼掃描框在高解析度手機屏上顯示很小的問題解決

android 二維碼掃描框在高解析度手機屏上顯示很小的問題解決

修改android zxing包中 中CameraManager.java的一個方法,將框的計算和手機的螢幕實際解析度結合,,這樣不管在什麼樣的手機上使用,框的大小都會顯示大小適中;

<span style="font-size:18px;">  public Rect getFramingRect() {
    Point screenResolution = configManager.getScreenResolution();
    if (framingRect == null) {
      if (camera == null) {
        return null;
      }
int width = screenResolution.x * 3 / 5;        //(1)取得當前螢幕寬的
3/5 // if (width < MIN_FRAME_WIDTH) { // width = MIN_FRAME_WIDTH; // } else if (width > MAX_FRAME_WIDTH) { // width = MAX_FRAME_WIDTH; // } int height = screenResolution.y * 3 / 5; //(2)取得當前螢幕高的3/5 // if (height < MIN_FRAME_HEIGHT) { // height = MIN_FRAME_HEIGHT; // } else if (height > MAX_FRAME_HEIGHT) { // height = MAX_FRAME_HEIGHT; // } /* 對比(1)(2)中的值,以其中一個小的為正方形的邊長 */ width = (width>height?height:width); height = (width<height?width:height); <span style="color:#ff6666;"> int leftOffset = (screenResolution.x - width) / 2;</span> //int topOffset = (screenResolution.y - height) / 2; int topOffset = (screenResolution.y - height) / 2 - 40; /*將掃描框在螢幕中的位置,放上一點 */ framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height); Log.d(TAG, "Calculated framing rect: " + framingRect); } return framingRect; }</span>