title: how do i set android camera view port always landscape?
body: <p>I have to solve a problem with android hardware camera. I want my device always record landscape video even when its orientation is vertical. I'd drawn some pics to support my issue:</p>
  
  <p><strong>This is what I get:</strong> <a href="http://joxi.ru/9IHbU_3JTJDrRyVH-XA" rel="nofollow">http://joxi.ru/9IHbU_3JTJDrRyVH-XA</a></p>
  
  <p><strong>This is what I want:</strong> <a href="http://joxi.ru/IYLbU4wyTJC1LhQcd4Q" rel="nofollow">http://joxi.ru/IYLbU4wyTJC1LhQcd4Q</a></p>
  
  <p>I can't say is it even possible (I think it's not, because of hardware matrix can't rotate). But my client thinks it is. Please, judge us. </p>
  
  <p>Update: there's my code.</p>
  
  <pre><code>//..import...
  public class MainActivity extends Activity {
  
  SurfaceView surfaceView;
  Camera camera;
  MediaRecorder mediaRecorder;
  //...public and private variables...
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  //......
      SurfaceHolder holder = surfaceView.getHolder();
      holder.addCallback(new SurfaceHolder.Callback() {
      @Override
      public void surfaceCreated(SurfaceHolder holder) {
              try {
                   camera.setPreviewDisplay(holder);
                   setCameraDisplayOrientation(CAM_ID);
  
                   camera.startPreview();
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
  
           @Override
           public void surfaceChanged(SurfaceHolder holder, int format,
                   int width, int height) {
           }
  
          @Override
           public void surfaceDestroyed(SurfaceHolder holder) {
           }
      });
  }
  
  @Override
  protected void onResume() {
      super.onResume();
      camera = Camera.open(this.CAM_ID);
   }
  
  @Override
  protected void onPause() {
      super.onPause();
      releaseMediaRecorder();
      if (camera != null)
          camera.release();
      camera = null;
  }
  
  public void setListeners() {/*....*/}
  public void onClickStartRecord(View view) {
      if (prepareVideoRecorder()) {
          mediaRecorder.start();
      } else releaseMediaRecorder();
  }
  
  public void onClickStopRecord(View view) {
      if (mediaRecorder != null) {
          mediaRecorder.stop();
          releaseMediaRecorder();
      }
  }
  
  private boolean prepareVideoRecorder() {
  
      camera.unlock();
  
      mediaRecorder = new MediaRecorder();
  
      mediaRecorder.setCamera(camera);
      mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
      mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
  
      CamcorderProfile profile = CamcorderProfile.get(this.CAM_ID, CamcorderProfile.QUALITY_LOW);
      mediaRecorder.setProfile(profile);
      if(this.orientation == 270 || this.orientation == 90 )
          mediaRecorder.setOrientationHint(90);
  
      mediaRecorder.setOutputFile(videoFile.getAbsolutePath());
      mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
  
      try {
          mediaRecorder.prepare();
      } catch (Exception e) {
          e.printStackTrace();
          releaseMediaRecorder();
          return false;
      }
      return true;
  }
  
  private void releaseMediaRecorder() {
      if (mediaRecorder != null) {
          mediaRecorder.reset();
          mediaRecorder.release();
          mediaRecorder = null;
          camera.lock();
      }
  }
  
  private void setCameraDisplayOrientation(int cameraId) {
      // определяем насколько повернут экран от нормального положения
      int rotation = getWindowManager().getDefaultDisplay().getRotation();
      int degrees = 0;
      switch (rotation) {
      case Surface.ROTATION_0:
          degrees = 0;
          break;
      case Surface.ROTATION_90:
          degrees = 90;
          break;
      case Surface.ROTATION_180:
          degrees = 180;
          break;
      case Surface.ROTATION_270:
          degrees = 270;
          break;
      }
  
      int result = 0;
  
      CameraInfo info = new CameraInfo();
      Camera.getCameraInfo(cameraId, info);
  
      if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
          result = ((360 - degrees) + info.orientation);
      } else
      if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
          result = ((360 - degrees) - info.orientation);
          result += 360;
      }
      result = result % 360;
      camera.setDisplayOrientation(result);
  
      this.orientation = result;
  }
  
  }
  </code></pre>