1. 程式人生 > >zxing掃描後出現 FAILED BINDER TRANSACTION

zxing掃描後出現 FAILED BINDER TRANSACTION

今天在進行 zxing 二維碼掃描時,出現  FAILED BINDER TRANSACTION,心裡那叫一個不爽。

細看了下程式碼,因為之前寫的掃描的程式碼,掃描框有些小,於是修改了zxing包下 camera 包下的 CameraManager 類的 getFramingRect 方法,原 getFramingRect  中是

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

 public Rect getFramingRect() {
    Point screenResolution = configManager.getScreenResolution();
    if (framingRect == null) {
      if (camera == null) {
        return null;
      }
      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;
  }

這樣的話,掃描是可以的,也能返回結果,但是由於預覽框太小,於是 改 getFramingRect() 方法如下:
    public Rect getFramingRect() {
        Point screenResolution = configManager.getScreenResolution();
        if (framingRect == null) {
            if (mCamera == null) {
                return null;
            }
            int width = screenResolution.x * 3 / 4;
            // int width = screenResolution.x * 4 / 5;
            // if (width < MIN_FRAME_WIDTH) {
            // width = MIN_FRAME_WIDTH;
            // } else if (width > MAX_FRAME_WIDTH) {
            // width = MAX_FRAME_WIDTH;
            // }
            int height = screenResolution.y * 44 / 99;
            // 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 * 24 / 102;
            framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
            Log.d(TAG, "Calculated framing rect: " + framingRect);
        }
        return framingRect;
    }

結果看 
	public void handleDecode(Result result, Bitmap barcode) {
		inactivityTimer.onActivity();
		playBeepSoundAndVibrate();
		// 在介面可見時閃光燈可以繼續開啟
		// isOpen = false;
		// contralLight();
		String resultString = result.getText();
		
		if (resultString.equals("")) {
			Toast.makeText(ScanActivity.this, "無效二維碼", Toast.LENGTH_SHORT).show();
		} else {
			Intent resultIntent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("result", resultString);
//			bundle.putParcelable("bitmap", barcode);
			resultIntent.putExtras(bundle);
			this.setResult(RESULT_OK, resultIntent);
		}
		AppManager.getInstance().closeActivity(ScanActivity.this);
}

方法中返回的結果 : resultString 的值是有的,但程式卡在這個介面不能動,log裡面顯示:FAILED BINDER TRANSACTION,於是上網各種查,終於找到原因:

Android中使用Intent進行傳遞資料時出現:E/JavaBinder﹕ !!! FAILED BINDER TRANSACTION !!!錯誤資訊,例如傳遞一個大的Bitmap物件時。

這種情況是因為系統對Intent傳遞資料有大小限制。

原來是我將預覽框變大後,掃描獲得的 bitmap 也就大了,在 intent' 傳輸的時候,受到大小的限制,於是出現 FAILED BINDER TRANSACTION,由於我呼叫掃描的時候不需要

bitmap,所以我在傳輸的時候把  bundle.putParcelable("bitmap", barcode);註釋掉了,大家如果需要傳輸 bitmap的話,記得在放入 intent之前,對 bitmap的大小做一下處理