Pages

Thursday 14 March 2013

Android portrait mode video recording orientation proble,

In Android In portrait mode you need to add following to your camera object and recording object before calling prepare function.

check following function.
we assume you create surface object and camera and other required object


protected void startRecording() throws IOException {
  
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
   Log.i(TAG, ">=GINGERBREAD");
   openFrontCamera();
  }

  if (mCamera == null) {
   Log.i(TAG, "CAMERA NULL");
   mCamera = Camera.open();
  }
  
  int camera_oriantation = 90; // for portrait mode
  
  mCamera.setDisplayOrientation(camera_oriantation);

  Date date = new Date();
  filename = "rec" + date.toString().replace(" ", "_").replace(":", "_")
    + ".mp4";

  File dir = new File(General.MediaPath);
  if (!dir.exists()) {
   dir.mkdirs();
  }
  // create empty file it must use
  File file = new File(General.MediaPath, filename);

  mrec = new MediaRecorder();

  mCamera.lock();
  mCamera.unlock();

  // Please maintain sequence of following code.

  // If you change sequence it will not work.
  mrec.setCamera(mCamera);
  mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
  mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
  mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
  mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

  mrec.setOrientationHint(camera_oriantation);
  
  mrec.setPreviewDisplay(surfaceHolder.getSurface());
  mrec.setOutputFile(General.MediaPath + File.separator + filename);
  mrec.prepare();
  mrec.start();

 }

3 comments :