2017-08-04 81 views
1

我在camera2 API中預覽的方向有問題。即使設備處於肖像模式,我所獲得的預覽仍然處於橫向模式。更準確地說,我得到的傳感器旋轉角度是90°,因此傳感器數據也被映射到90°。觀察結果在仿真器(Nexus 5)上。相機預覽方向在風景中,但設備在potrait

private void startPreview() { 
    SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture(); 
    surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); 
    Surface previewSurface = new Surface(surfaceTexture); 

    try { 
     mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); 
     mCaptureRequestBuilder.addTarget(previewSurface); 

     mCameraDevice.createCaptureSession(Arrays.asList(previewSurface), 
       new CameraCaptureSession.StateCallback() { 
      @Override 
      public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) { 
       try { 
        cameraCaptureSession.setRepeatingRequest(mCaptureRequestBuilder.build(), 
          null, mBackgroundHanlder); 
       } catch (CameraAccessException e) { 
        e.printStackTrace(); 
       } 
      } 

      @Override 
      public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) { 

      } 
     }, null); 
    } catch (CameraAccessException e) { 
     e.printStackTrace(); 
    } 

} 

private void setupCamera(int width, int height) { 
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); 
    try { 
     for(String cameraId: cameraManager.getCameraIdList()) { 
      CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId); 
      if (cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) 
        == CameraCharacteristics.LENS_FACING_FRONT) { 
       continue; 
      } 
      StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); 

      int deviceOrientation = getWindowManager().getDefaultDisplay().getRotation(); 
      mTotalRotation = sensorToDeviceRotation(cameraCharacteristics, deviceOrientation); 
      boolean swapRotation = mTotalRotation == 90 || mTotalRotation == 270; 
      int rotatedWidth = width; 
      int rotatedHeight = height; 

      if (swapRotation) { 
       rotatedHeight = width; 
       rotatedWidth = height; 
      } 
      mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedWidth, rotatedHeight); 
      mVideoSize = chooseOptimalSize(map.getOutputSizes(MediaRecorder.class), rotatedWidth, rotatedHeight); 

      mcameraId = cameraId; 
      return; 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 


private static int sensorToDeviceRotation(CameraCharacteristics cameraCharacteristics, int deviceOrientation) { 
    int sensorOrientation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); 
    deviceOrientation = ORIENTATIONS.get(deviceOrientation); 
    return (sensorOrientation+deviceOrientation+360)%360; 
} 

回答

0

它看起來並不像實際上將旋轉變換應用到輸出幀。只是交換寬度/高度不會做任何事情,因爲一般情況下相機只支持風景分辨率。

您需要使用TextureView的getTransform和setTransform函數來添加必要的旋轉。

具體做法是:

  1. 讀取當前與getTransform
  2. 乘上必要的旋轉變換變換你變換計算基於您的UI方向
  3. 設置爲TextureView用的setTransform
  4. 變換

Camera2Basic示例應用程序的代碼可能對此有所幫助,但我不確定它是否設置爲縱向:https://github.com/googlesamples/android-Camera2Basic/blob/master/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java