How to record video in portrait mode

All your problems with Audio, Video and Images.

How to record video in portrait mode

Postby devand » Wed Dec 09, 2009 4:31 pm

Android Camera preview sample has it in landscape. The camera params could be set as portrait etc.

But while using Mediarecorder, you don't specify a camera, it takes the default camera. It captures ok, but since I have the app display in portrait mode (must be), the captured video gets a -90 rotate. I need to set the camera also in portrait mode. Please help.. here is the recorder class works fine.

public class CameraPreview extends Activity {

private Preview mPreview;
private MediaRecorder recorder = null;
private Handler mHandler = new Handler();

String filename = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Hide the window title.
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

ConfigRecorder();

mPreview = new Preview(this, recorder);

LinearLayout mLayout = new LinearLayout(this);
mLayout.setOrientation(LinearLayout.VERTICAL);

mLayout.addView(mPreview,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
20,
1.0f));

final Button mButton = new Button(this);
mButton.setText("Start Recording");
mLayout.addView(mButton,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
1.0f));
mButton.setBackgroundColor(Color.BLACK);
mButton.setTextColor(Color.WHITE);
mButton.setOnClickListener(new android.view.View.OnClickListener() {

public void onClick(android.view.View v) {
if (mButton.getText() == "Start Recording") {
if (mButton.getTag() != null && mButton.getTag() == "1") {
ConfigRecorder();
mPreview.setRecorder(recorder);
mPreview.prepareRecorder();
}
if (recorder != null) {
recorder.start();
mButton.setTag("0");
((android.os.Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(1000);
}
mButton.setText("Stop Recording");
} else {
if (recorder != null) {
recorder.stop();
mButton.setTag("1");
((android.os.Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(2000);
}
mButton.setText("Start Recording");
}
}
});
setContentView(mLayout);

}

void ConfigRecorder() {

if (recorder == null) {
recorder = new MediaRecorder();
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
filename = android.text.format.DateFormat.format("MM-dd-yyyy hh-mm-saa", new Date().getTime())+ "";
recorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + filename + ".3gp");
}

@Override
protected void onDestroy() {
if (mPreview != null) {
mPreview.destroyDrawingCache();
mPreview.mHolder = null;
mPreview = null;
}
if (recorder != null) {
recorder.release();
recorder = null;
}
System.gc();
super.onDestroy();
}
}//main class


class Preview extends SurfaceView implements SurfaceHolder.Callback {

SurfaceHolder mHolder;

MediaRecorder tempRecorder;

Preview(Context context, MediaRecorder recorder) {
super(context);
tempRecorder = recorder;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


}

public void setRecorder(MediaRecorder _tempRecorder) {
this.tempRecorder = _tempRecorder;

}

public void prepareRecorder() {


tempRecorder.setVideoFrameRate(4);
try {

tempRecorder.prepare();
} catch (IOException e) {
// Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG);
tempRecorder.release();
tempRecorder = null;
} catch (Exception e) {
tempRecorder.release();
tempRecorder = null;
}

}

public void surfaceCreated(SurfaceHolder holder) {


try {
prepareRecorder();


} catch (Exception exception) {

}

}

public Surface getSurface() {
return mHolder.getSurface();
}

public void surfaceDestroyed(SurfaceHolder holder) {

if (tempRecorder != null) {
tempRecorder.release();
tempRecorder = null;
}
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

}

@Override
protected void onDetachedFromWindow() {

super.onDetachedFromWindow();
}
}
devand
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Dec 09, 2009 4:17 pm

Top

Re: How to record video in portrait mode

Postby Bhupathi » Thu Oct 21, 2010 8:25 am

Hi devand, I'm facing the same issue, let me know if you have fixed this issue please..
Bhupathi
Freshman
Freshman
 
Posts: 6
Joined: Tue Apr 13, 2010 10:23 am

Re: How to record video in portrait mode

Postby blundell » Thu Oct 21, 2010 10:06 am

Answered on StackOverflow:
http://stackoverflow.com/questions/1607 ... a-rotation


Camera drivers do not know the orientation of the device. In 1.5 and 1.6, you need to call parameters.set("rotation", degree) to tell drivers about rotation before calling takePicture(). In 2.0, there is a new API setRotation (see below). Note that some devices rotate the entire pictures for you and some just set the orientation in EXIF header.

http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setRotation(int)

Sets the orientation of the device in degrees. For example, suppose the natural position of the device is landscape. If the user takes a picture in landscape mode in 2048x1536 resolution, the rotation should be set to 0. If the user rotates the phone 90 degrees clockwise, the rotation should be set to 90. Applications can use OrientationEventListener to set this parameter. The camera driver may set orientation in the EXIF header without rotating the picture. Or the driver may rotate the picture and the EXIF thumbnail. If the Jpeg picture is rotated, the orientation in the EXIF header will be missing or 1 (row #0 is top and column #0 is left side).
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: How to record video in portrait mode

Postby Bhupathi » Thu Oct 21, 2010 11:46 am

Hi blundell,

The fix you suggesting is to set the parameters for camera class, but i'm not using the camera class.
I'm usign the MediaRecorder class and there I set the camera as source via,
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

it always records in landscape mode, regardless of real device orientation. Please let me know if you find any fix for this.
Bhupathi
Freshman
Freshman
 
Posts: 6
Joined: Tue Apr 13, 2010 10:23 am

Re: How to record video in portrait mode

Postby siba420 » Thu Sep 12, 2013 2:43 pm

Hi devand,did u find any solution for this,please let me know ?
siba420
Freshman
Freshman
 
Posts: 2
Joined: Thu Sep 12, 2013 2:38 pm

Re: How to record video in portrait mode

Postby siba420 » Fri Sep 13, 2013 10:22 am

Just call the setContentView() when orientation change to portait mode.
siba420
Freshman
Freshman
 
Posts: 2
Joined: Thu Sep 12, 2013 2:38 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 10 guests