title: Rotate camera preview to Portrait Android OpenCV Camera
body: <p>I am trying to use OpenCV 2.4.3.2 to create a camera app and do some opencv processing.  I would like it to be able to have multiple UI orientations, not just Landscape.</p>
  <p>The problem is that when I change the orientation to portrait, the image comes out sideways.</p>
  <p>I understand that <a href="https://stackoverflow.com/questions/12949793/rotate-videocapture-in-opencv-on-android">I could just rotate the input image</a> before doing image processing (and thus leave the orientation as landscape only), which is fine and works, but doesn't solve the problem that the rest of my UI will be in the wrong orientation.</p>
  <p>I have also tried using <a href="https://stackoverflow.com/questions/3841122/android-camera-preview-is-sideways">this code</a> to rotate the camera 90deg, but it just doesn't seem to work.</p>
  <pre><code>mCamera.setDisplayOrientation(90);
  </code></pre>
  <p>It either has no effect, or sometimes just causes the preview to be blacked out</p>
  <p>Has anyone done this successfully with OpenCV? My class extends from JavaCameraView.
  <img src="https://i.stack.imgur.com/tARs6.png" alt="portrait image with sideways preview" /></p>
  <h1>Edit</h1>
  <p>I have made an improvement, which is that I have rotated the image inside of OpenCV as it is displayed in the CameraBridgeViewBase.java class.</p>
  <p>In the deliver and draw frame method:</p>
  <pre><code>if (canvas != null) {
              canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
              //canvas.drawBitmap(mCacheBitmap, (canvas.getWidth() - mCacheBitmap.getWidth()) / 2, (canvas.getHeight() - mCacheBitmap.getHeight()) / 2, null);
              //Change to support portrait view
              Matrix matrix = new Matrix();
              matrix.preTranslate((canvas.getWidth() - mCacheBitmap.getWidth()) / 2,(canvas.getHeight() - mCacheBitmap.getHeight()) / 2);
  
              if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
                  matrix.postRotate(90f,(canvas.getWidth()) / 2,(canvas.getHeight()) / 2);
              canvas.drawBitmap(mCacheBitmap, matrix, new Paint());
  </code></pre>
  <p>...
  Basically, this just roatates the input image like so</p>
  <p><img src="https://i.stack.imgur.com/yiZll.png" alt="input image rotated 90" /></p>
  <p>This is better, but I obviously want this to be full screen.</p>