title: Android camera2 preview doesn't work right in landscape mode
body: <p>I just want to show a camera preview in a <code>textureView</code>.<br>
  In portrait mode it looks fine but in landscape mode it is rotated 90 degrees clockwise.<br>
  I know that something like that should save this issue:</p>
  
  <pre><code>private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
  
  static {
      ORIENTATIONS.append(Surface.ROTATION_0, 90);
      ORIENTATIONS.append(Surface.ROTATION_90, 0);
      ORIENTATIONS.append(Surface.ROTATION_180, 270);
      ORIENTATIONS.append(Surface.ROTATION_270, 180);
  }
  </code></pre>
  
  <p>I don't know where to call it. Do you have any suggestions? </p>
  
  <p>Here is my code:</p>
  
  <pre><code>private TextureView textureView;
  private static final String TAG = "AndroidCameraApi";
  private String cameraId;
  protected CameraDevice cameraDevice;
  protected CameraCaptureSession cameraCaptureSessions;
  protected CaptureRequest captureRequest;
  private static final int REQUEST_CAMERA_PERMISSION = 200;
  private Size imageDimension;
  protected CaptureRequest.Builder captureRequestBuilder;
  private Handler mBackgroundHandler;
  private HandlerThread mBackgroundThread;
  private ImageReader imageReader;
  
  private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
  
  static {
      ORIENTATIONS.append(Surface.ROTATION_0, 90);
      ORIENTATIONS.append(Surface.ROTATION_90, 0);
      ORIENTATIONS.append(Surface.ROTATION_180, 270);
      ORIENTATIONS.append(Surface.ROTATION_270, 180);
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_photo);
  
      textureView = (TextureView) findViewById(R.id.texturePhoto);
      assert textureView != null;
      textureView.setSurfaceTextureListener(textureListener);
  
  }
  
  TextureView.SurfaceTextureListener textureListener = new TextureView.SurfaceTextureListener() {
      @Override
      public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
          //open your camera here
          openCamera();
      }
      @Override
      public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
          // Transform you image captured size according to the surface width and height
      }
      @Override
      public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
          return false;
      }
      @Override
      public void onSurfaceTextureUpdated(SurfaceTexture surface) {
      }
  };
  
  private void openCamera() {
      CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
      Log.e(TAG, "is camera open");
      try {
          cameraId = manager.getCameraIdList()[0];
          CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
          StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
          assert map != null;
          imageDimension = map.getOutputSizes(SurfaceTexture.class)[0];
          // Add permission for camera and let user grant the permission
          if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
              ActivityCompat.requestPermissions(PhotoActivty.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAMERA_PERMISSION);
              return;
          }
          manager.openCamera(cameraId, stateCallback, null);
      } catch (CameraAccessException e) {
          e.printStackTrace();
      }
      Log.e(TAG, "openCamera X");
  }
  
  private final CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
      @Override
      public void onOpened(CameraDevice camera) {
          //This is called when the camera is open
          Log.e(TAG, "onOpened");
          cameraDevice = camera;
          createCameraPreview();
      }
      @Override
      public void onDisconnected(CameraDevice camera) {
          cameraDevice.close();
      }
      @Override
      public void onError(CameraDevice camera, int error) {
          cameraDevice.close();
          cameraDevice = null;
      }
  };
  
  protected void createCameraPreview() {
      try {
          SurfaceTexture texture = textureView.getSurfaceTexture();
          assert texture != null;
          texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
          Surface surface = new Surface(texture);
          captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
          captureRequestBuilder.addTarget(surface);
          cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback(){
              @Override
              public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                  //The camera is already closed
                  if (null == cameraDevice) {
                      return;
                  }
                  // When the session is ready, we start displaying the preview.
                  cameraCaptureSessions = cameraCaptureSession;
                  updatePreview();
              }
              @Override
              public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                  Toast.makeText(PhotoActivty.this, "Configuration change", Toast.LENGTH_SHORT).show();
              }
          }, null);
      } catch (CameraAccessException e) {
          e.printStackTrace();
      }
  }
  
  protected void updatePreview() {
      if(null == cameraDevice) {
          Log.e(TAG, "updatePreview error, return");
      }
      captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
      try {
          cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
      } catch (CameraAccessException e) {
          e.printStackTrace();
      }
  }
  private void closeCamera() {
      if (null != cameraDevice) {
          cameraDevice.close();
          cameraDevice = null;
      }
      if (null != imageReader) {
          imageReader.close();
          imageReader = null;
      }
  }
  </code></pre>