2017-04-10 107 views
1

我正在用Camera2 API構建自定義相機。 到目前爲止,相機的工作情況很好,除了有時會失真的預覽。假設我連續打開相機7次。所有的嘗試都是成功的,並且第8次相機預覽失真。它看起來像使用寬度作爲高度,反之亦然。Android Camera2 API預覽有時會失真

我已經將我的代碼基於camera2的Google樣例實現,可以找到它here。 有趣的是,有時甚至連Google樣本實現都有這個扭曲的預覽。我試圖修改AutoFitTextureView,但沒有成功。我目前正在使用Google提供的AutoFitTextureView.java。

與此相似的帖子can be found here。 但是,建議的修復程序並未解決問題。

我可以通過改變setUpCameraOutputs方法如下重現該問題:

mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth()); 

到:

mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight()); 

另一個奇怪的是,每當發生扭曲預覽,您只需按home按鈕,所以應用程序進入onPause()並再次打開應用程序,所以onResume()被調用,預覽每次都是完美的。

有沒有人遇到過這個問題,並找到了解決辦法?

在此先感謝

+1

你有沒有找到任何解決辦法?我也面臨這個問題。 –

+0

我也面臨同樣的問題 – FaisalAhmed

回答

0

的谷歌Camera2Basic樣品was finally fixed。原始代碼有一個微小的< >錯誤。 2年來一直是錯誤的。

0

我在Sony Xperia Z3 Tablet Compact上面臨同樣的問題。

亞歷克斯指出的拉請求似乎不適用於我。它導致相機預覽大於視圖區域(預覽被裁剪)。

雖然我無法專門跟蹤此問題,但我找到了解決方法。似乎在打開相機的過程中,mTextureView的尺寸發生變化時會發生變形。延遲照相機開啓程序可以解決問題。

修改openCamera方法:

/** 
* Opens the camera specified by {@link StepFragmentCamera#mCameraId}. 
*/ 
private void openCamera(int width, int height) { 
    startBackgroundThread(); 

    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) 
      != PackageManager.PERMISSION_GRANTED) { 
     requestCameraPermission(); 
     return; 
    } 
    setUpCameraOutputs(width, height); 
    configureTransform(width, height); 

    /* 
    * Delay the opening of the camera until (hopefully) layout has been fully settled. 
    * Otherwise there is a chance the preview will be distorted (tested on Sony Xperia Z3 Tablet Compact). 
    */ 
    mTextureView.post(new Runnable() { 
     @Override 
     public void run() { 
      /* 
      * Carry out the camera opening in a background thread so it does not cause lag 
      * to any potential running animation. 
      */ 
      mBackgroundHandler.post(new Runnable() { 
       @Override 
       public void run() { 
        Activity activity = getActivity(); 
        CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); 
        try { 
         if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) { 
          throw new RuntimeException("Time out waiting to lock camera opening."); 
         } 
         manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler); 
        } catch (CameraAccessException e) { 
         e.printStackTrace(); 
        } catch (InterruptedException e) { 
         throw new RuntimeException("Interrupted while trying to lock camera opening.", e); 
        } 
       } 
      }); 
     } 
    }); 
} 
相關問題