I'm trying to record a video in the correct orientation in Android. I've built my Activity, it works correctly, the only thing is that the saved video after being recorded is rotated. The preview of the camera works correctly so I can't understand why. To fix the rotation I use this code (the method I've found on Google Developers):
mMediaRecorder.setOrientationHint(getCameraDisplayOrientation());
private int getCameraDisplayOrientation()
{
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(CameraInfo.CAMERA_FACING_BACK,
info);
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;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
{
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else
{ // back-facing
result = (info.orientation - degrees + 360) % 360;
}
return result;
The Activity is defined in the manifest like that:
<activity android:name="pages.CameraPage"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="landscape"
>
</activity>
The problem is that getCameraDisplayOrientation always returns 0 degrees, no matter how I orient the screen.
I call the method just before the record starts so if I would be able to get the rotation of the screen with respect to the default landscape I'd be ok.
Thanks for your help!
Finally I was able to build successfully my own camera app without any orientation problem. The key was looking at the surfaceView for the rotation. I think it might help someone to have all the code posted here, just copy it and launch the Activity!
CameraPage.java
package pages;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URI;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import model.CameraView;
import com.controller.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.Toast;
public class CameraPage extends Activity
{
private Camera mCamera;
private CameraView mPreview;
private MediaRecorder mMediaRecorder;
private FrameLayout preview;
private ImageButton buttonRecordStop;
private boolean isRecording = false;
private String fileName;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d("CameraPage-onCreate", "Called onCreate()");
setContentView(R.layout.camera_surface);
buttonRecordStop = (ImageButton) findViewById(R.id.camera_surface_buttonRecordStop);
buttonRecordStop.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_MOVE)
{
AlphaAnimation alpha = new AlphaAnimation(1.0F, 0.3F);
alpha.setDuration(100);
alpha.setFillAfter(true); // Tell it to persist after the animation ends
buttonRecordStop.startAnimation(alpha);
} else if (event.getAction() == MotionEvent.ACTION_UP
|| event.getAction() == MotionEvent.ACTION_CANCEL)
{
AlphaAnimation alpha = new AlphaAnimation(0.3F, 1.0F);
alpha.setDuration(100);
alpha.setFillAfter(true); // Tell it to persist after the animation ends
buttonRecordStop.startAnimation(alpha);
}
return false;
}
});
buttonRecordStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if (!isRecording)
{
Log.d("CameraPage-buttonRecordStop-onClick", "Start Record");
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// initialize video camera
if (prepareVideoRecorder())
{
v.setVisibility(View.GONE);
((ImageButton) v)
.setImageResource(R.drawable.iconapause);
v.setVisibility(View.VISIBLE);
isRecording = true;
// Camera is available and unlocked, MediaRecorder is prepared,
// now you can start recording
mMediaRecorder.start();
} else
{
// prepare didn't work, release the camera
releaseMediaRecorder();
// inform user
}
} else
{
Log.d("CameraPage-buttonRecordStop-onClick", "Stop Record");
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
v.setVisibility(View.GONE);
((ImageButton) v).setImageResource(R.drawable.iconarecord);
v.setVisibility(View.VISIBLE);
isRecording = false;
stopRecording();
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}
}// end onClick
});
mCamera = getCameraInstance();
preview = (FrameLayout) findViewById(R.id.camera_preview);
}// end onCreate
#Override
protected void onResume()
{
super.onResume();
Log.d("CameraPage-onResume",
"Chiamato onResume ripristino le risorse se necessario");
if (mCamera == null)
{
// Create an instance of Camera
mCamera = getCameraInstance();
}
if (mPreview == null)
{
mPreview = new CameraView(getApplicationContext(), mCamera, this);
preview.addView(mPreview);
}
}
#Override
protected void onPause()
{
super.onPause();
Log.d("CameraPage-onPause", "Vado in onPause e rilascio le risorse");
releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera(); // release the camera immediately on pause event
preview.removeView(mPreview);
mPreview = null;
}
private void stopRecording()
{
mMediaRecorder.stop();
releaseMediaRecorder();
// Restarto la preview della camera
mCamera.startPreview();
// mCamera.lock();
}
private boolean prepareVideoRecorder()
{
// mCamera = getCameraInstance();
mMediaRecorder = new MediaRecorder();
// Stop preview because I have to start the preview for the video
mCamera.stopPreview();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mMediaRecorder.setProfile(CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH));
// Step 4: Set output file
setOutputFile();
mMediaRecorder.setOutputFile(getOutputFile());
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
mMediaRecorder.setOrientationHint(CameraPage
.getCameraDisplayOrientation(this,
Camera.CameraInfo.CAMERA_FACING_BACK, mCamera));
// Step 6: Prepare configured MediaRecorder
try
{
mMediaRecorder.prepare();
} catch (IllegalStateException e)
{
Log.d("CameraPage-prepareVideoRecorder",
"IllegalStateException preparing MediaRecorder: "
+ e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e)
{
Log.d("CameraPage-prepareVideoRecorder",
"IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}
private void releaseMediaRecorder()
{
if (mMediaRecorder != null)
{
mMediaRecorder.reset(); // clear recorder configuration
mMediaRecorder.release(); // release the recorder object
mMediaRecorder = null;
mCamera.lock(); // lock camera for later use
}
}
private void releaseCamera()
{
if (mCamera != null)
{
mCamera.release(); // release the camera for other applications
mCamera = null;
}
if (mPreview != null)
mPreview.getHolder().removeCallback(mPreview);
}
/** A safe way to get an instance of the Camera object. */
private Camera getCameraInstance()
{
Camera c = null;
try
{
Log.d("CameraPage-getCameraInstance", "Prendo istanza camera");
c = Camera.open(); // attempt to get a Camera instance
} catch (Exception e)
{
// Camera is not available (in use or does not exist)
Log.d("CameraPage-getCameraInstance",
"Eccezione durante retrieve istanza camera " + e);
}
return c; // returns null if camera is unavailable
}
#SuppressLint("SimpleDateFormat")
private void setOutputFile()
{
String folderPath = Environment.getExternalStorageDirectory().getPath();
File vims = new File(folderPath + "/ViMS");
if (!vims.exists())
vims.mkdir();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd---HH-mm-ss");
Date date = new Date();
String timestamp = "/" + dateFormat.format(date) + ".mp4";
Log.d("CameraPage-getOutputFile", Environment
.getExternalStorageDirectory().getPath());
fileName = vims.getPath() + timestamp;
}
private String getOutputFile()
{
return fileName;
}
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera)
{
int result = CameraPage.getCameraDisplayOrientation(activity, cameraId,
camera);
if (android.os.Build.VERSION.SDK_INT <= 14)
{
camera.stopPreview();
camera.setDisplayOrientation(result);
camera.startPreview();
} else
{
camera.setDisplayOrientation(result);
}
}// end setCameraDisplayOrientation
public static int getCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera)
{
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.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;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
{
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else
{ // back-facing
result = (info.orientation - degrees + 360) % 360;
}
return result;
}
#Override
public void onDestroy()
{
super.onDestroy();
Log.d("CameraPage-onDestroy", "Called onDestroy");
}
}// end CameraPage
CameraView.java
package model;
import java.io.IOException;
import pages.CameraPage;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/** A basic Camera preview class */
public class CameraView extends SurfaceView implements SurfaceHolder.Callback
{
private SurfaceHolder mHolder;
private Camera mCamera;
private Activity activity;
public CameraView(Context context)
{
super(context);
Log.d("CameraView-default constructor",
"Chiamato uno dei due default constructors");
}
public CameraView(Context context, AttributeSet attrs)
{
super(context, attrs);
Log.d("CameraView-default constructor",
"Chiamato uno dei due default constructors");
}
public CameraView(Context context, Camera camera, Activity activity)
{
super(context);
Log.d("CameraView-constructor",
"Inizializzo la classe con la camera e il context");
this.activity = activity;
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder)
{
Log.d("CameraView-surfaceCreated",
"La superfice è stata creata, setto la preview della camera sulla superficie");
// The Surface has been created, now tell the camera where to draw the preview.
try
{
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e)
{
Log.d("CameraView-surfaceCreated", "Error setting camera preview: "
+ e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
Log.d("CameraView-surfaceDestroyed",
"Distrutta superficie della preview");
// Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
Log.d("CameraView-surfaceChanged",
"Gestisco i cambiamenti nella superficie che ospita la camera");
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null)
{
// preview surface does not exist
Log.d("CameraView-surfaceChanged", "Preview surface doesn't exist");
return;
}
try
{
CameraPage.setCameraDisplayOrientation(activity,
Camera.CameraInfo.CAMERA_FACING_BACK, mCamera);
} catch (Exception e)
{
Log.d("CameraView-surfaceChanged",
"Exception reorienting the camera " + e);
}
}// end surfaceChanged
}// end CameraView
camera_surface.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
<ImageButton
android:id="#+id/camera_surface_buttonRecordStop"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:adjustViewBounds="true"
android:background="#null"
android:contentDescription="#string/action"
android:scaleType="fitCenter"
android:src="#drawable/iconarecord" />
</RelativeLayout>
The Activity has been tested from Android 2.3.6 to 4.3. If you find any problem or have any suggestion please let me know!
If your issue is associated with your rotation variable, you may consider looking at this link. The main difference to pull away from that link is a WindowManager object is created and then used to get the rotation. This seemingly small difference has been the difference maker between a piece of my code working and not working.
You can simply say Camera.CameraInfo info = new Camera.CameraInfo(); and Camera.getCameraInfo(...);.
I am curious as to why you are specifying CameraInfo.CAMERA_FACING_BACK in android.hardware.Camera.getCameraInfo(CameraInfo.CAMERA_FACING_BACK, info); I feel like you could just call info.facing to get the int instead.
I hope between the link and my suggestions/inquiries that something sparks the correct answer!
Related
If I take a 10 minute video I would like that only the last 2 minutes of the video will be saved in mobile not the whole 10 minutes. How can I do this??
And how can I do that when recording starts every time when I press the button it will save 2 minute video and then start video recorder when press button it again save 2 minutes video???
package com.example.checkvidio;
import android.app.Activity;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
public class VideoWithSurfaceVw extends Activity{
//Starter Tutorial: http://sandyandroidtutorials.blogspot.co.uk/2013/05/android-video-capture-tutorial.html
private Camera myCamera;
private MyCameraSurfaceView myCameraSurfaceView;
private MediaRecorder mediaRecorder;
public static int orientation;
Button myButton;
SurfaceHolder surfaceHolder;
boolean recording;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recording = false;
setContentView(R.layout.activity_main);
//Get Camera for preview
myCamera = getCameraInstance();
//myCamera.setDisplayOrientation(90); //Doesn't error here, but doesn't affect video.
if(myCamera == null){
Toast.makeText(VideoWithSurfaceVw.this,
"Fail to get Camera",
Toast.LENGTH_LONG).show();
}
myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera,this);
FrameLayout myCameraPreview = (FrameLayout)findViewById(R.id.videoview);
myCameraPreview.addView(myCameraSurfaceView);
myButton = (Button)findViewById(R.id.mybutton);
myButton.setOnClickListener(myButtonOnClickListener);
}
Button.OnClickListener myButtonOnClickListener
= new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
if(recording){
// stop recording and release camera
mediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
//Exit after saved
//finish();
myButton.setText("REC");
recording = false;
}else{
//Release Camera before MediaRecorder start
releaseCamera();
if(!prepareMediaRecorder()){
Toast.makeText(VideoWithSurfaceVw.this,
"Fail in prepareMediaRecorder()!\n - Ended -",
Toast.LENGTH_LONG).show();
finish();
}
mediaRecorder.start();
recording = true;
myButton.setText("STOP");
}
}catch (Exception ex){
ex.printStackTrace();
}
}};
private Camera getCameraInstance(){
// TODO Auto-generated method stub
Camera c = null;
try {
c = Camera.open(0); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
private boolean prepareMediaRecorder(){
myCamera = getCameraInstance();
// set the orientation here to enable portrait recording.
setCameraDisplayOrientation(this,0,myCamera);
mediaRecorder = new MediaRecorder();
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setOutputFile("/sdcard/myvideo1.mp4");
mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(50000000); // Set max file size 50Mb
mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
mediaRecorder.setOrientationHint(VideoWithSurfaceVw.orientation);
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
#Override
protected void onPause() {
super.onPause();
releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera(); // release the camera immediately on pause event
}
private void releaseMediaRecorder(){
if (mediaRecorder != null) {
mediaRecorder.reset(); // clear recorder configuration
mediaRecorder.release(); // release the recorder object
mediaRecorder = new MediaRecorder();
myCamera.lock(); // lock camera for later use
}
}
private void releaseCamera(){
if (myCamera != null){
myCamera.release(); // release the camera for other applications
myCamera = null;
}
}
public class MyCameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private Camera mCamera;
private Activity mActivity;
public MyCameraSurfaceView(Context context, Camera camera,Activity activity) {
super(context);
mCamera = camera;
mActivity=activity;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
try {
setCameraDisplayOrientation(mActivity,0,mCamera);
previewCamera();
} catch (Exception e) {
e.printStackTrace();
}
}
public void previewCamera()
{
try
{
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
}
catch(Exception e)
{
//Log.d(APP_CLASS, "Cannot start preview", e);
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.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;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing`enter code here`
result = (info.orientation - degrees + 360) % 360;
}
VideoWithSurfaceVw.orientation=result;
camera.setDisplayOrientation(result);
You can put restriction that after 2 mins the video recording gets automatically stopped.
Or if you want to do a video operations like croping or trimming that Android is not providing any APIs for that you have to use third party library like FFMPEG.
This question is similar to posts here, here, here, here and here, but I'm stuck and have spent hours trying to figure it out.
I have a video camera preview (which now always shows in the correct orientation), but when I hit record (mediaRecorder.start();), the video orientation changes. I have tried using setOrientationHint, but it doesn't seem to make a difference (as marked in a comment in the code below).
It affects both my test devices (Galaxy and Xperia on Jelly Bean). What should I do to fix this please?
Here's my code:
XML
<RelativeLayout android:id="#+id/surface_camera"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_weight="1"
>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="#+id/videoview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<Button
android:id="#+id/mybutton"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="REC"
android:textSize="12dp"/>
</RelativeLayout>
</RelativeLayout>
Java
package hockeyj.androidlisttesting;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
public class VideoWithSurfaceVw extends Activity{
//Starter Tutorial: http://sandyandroidtutorials.blogspot.co.uk/2013/05/android-video-capture-tutorial.html
private Camera myCamera;
private MyCameraSurfaceView myCameraSurfaceView;
private MediaRecorder mediaRecorder;
Button myButton;
SurfaceHolder surfaceHolder;
boolean recording;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recording = false;
setContentView(R.layout.activity_video_with_surface_vw);
//Get Camera for preview
myCamera = getCameraInstance();
//myCamera.setDisplayOrientation(90); //Doesn't error here, but doesn't affect video.
if(myCamera == null){
Toast.makeText(VideoWithSurfaceVw.this,
"Fail to get Camera",
Toast.LENGTH_LONG).show();
}
myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
FrameLayout myCameraPreview = (FrameLayout)findViewById(R.id.videoview);
myCameraPreview.addView(myCameraSurfaceView);
myButton = (Button)findViewById(R.id.mybutton);
myButton.setOnClickListener(myButtonOnClickListener);
}
Button.OnClickListener myButtonOnClickListener
= new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
if(recording){
// stop recording and release camera
mediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
//Exit after saved
//finish();
myButton.setText("REC");
recording = false;
}else{
//Release Camera before MediaRecorder start
releaseCamera();
if(!prepareMediaRecorder()){
Toast.makeText(VideoWithSurfaceVw.this,
"Fail in prepareMediaRecorder()!\n - Ended -",
Toast.LENGTH_LONG).show();
finish();
}
mediaRecorder.start();
recording = true;
myButton.setText("STOP");
}
}catch (Exception ex){
ex.printStackTrace();
}
}};
private Camera getCameraInstance(){
// TODO Auto-generated method stub
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
private boolean prepareMediaRecorder(){
myCamera = getCameraInstance();
mediaRecorder = new MediaRecorder();
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setOutputFile("/sdcard/myvideo1.mp4");
mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(50000000); // Set max file size 50Mb
//Attempt commented out - Trying to get the recorder to record portrait, but doesn't work....
//mediaRecorder.setOrientationHint(90);
mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
#Override
protected void onPause() {
super.onPause();
releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera(); // release the camera immediately on pause event
}
private void releaseMediaRecorder(){
if (mediaRecorder != null) {
mediaRecorder.reset(); // clear recorder configuration
mediaRecorder.release(); // release the recorder object
mediaRecorder = new MediaRecorder();
myCamera.lock(); // lock camera for later use
}
}
private void releaseCamera(){
if (myCamera != null){
myCamera.release(); // release the camera for other applications
myCamera = null;
}
}
public class MyCameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private Camera mCamera;
public MyCameraSurfaceView(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
try {
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
try{
mCamera.stopPreview();
}catch (Exception e){
// ignore: tried to stop a non-existent preview
}
Camera.Parameters parameters = mCamera.getParameters();
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0)
{
parameters.setPreviewSize(height, width);
mCamera.setDisplayOrientation(90);
}
if(display.getRotation() == Surface.ROTATION_90)
{
parameters.setPreviewSize(width, height);
}
if(display.getRotation() == Surface.ROTATION_180)
{
parameters.setPreviewSize(height, width);
}
if(display.getRotation() == Surface.ROTATION_270)
{
parameters.setPreviewSize(width, height);
mCamera.setDisplayOrientation(180);
}
mCamera.setParameters(parameters);
previewCamera();
} catch (Exception e) {
e.printStackTrace();
}
}
public void previewCamera()
{
try
{
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
}
catch(Exception e)
{
//Log.d(APP_CLASS, "Cannot start preview", e);
}
}
/*#Override
public void surfaceChanged(SurfaceHolder holder, int format, int weight,
int height) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// make any resize, rotate or reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
}
}*/
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
}
I had a similar problem, i used Rotating a Camera SurfaceView to portrait to set the orientation, i modified it to also set the result to a class variable and set the orientation for the media recorder.
Try this:
public class VideoWithSurfaceVw extends Activity{
//Starter Tutorial: http://sandyandroidtutorials.blogspot.co.uk/2013/05/android-video-capture-tutorial.html
private Camera myCamera;
private MyCameraSurfaceView myCameraSurfaceView;
private MediaRecorder mediaRecorder;
public static int orientation;
Button myButton;
SurfaceHolder surfaceHolder;
boolean recording;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recording = false;
setContentView(R.layout.activity_video_with_surface_vw);
//Get Camera for preview
myCamera = getCameraInstance();
//myCamera.setDisplayOrientation(90); //Doesn't error here, but doesn't affect video.
if(myCamera == null){
Toast.makeText(VideoWithSurfaceVw.this,
"Fail to get Camera",
Toast.LENGTH_LONG).show();
}
myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera,this);
FrameLayout myCameraPreview = (FrameLayout)findViewById(R.id.videoview);
myCameraPreview.addView(myCameraSurfaceView);
myButton = (Button)findViewById(R.id.mybutton);
myButton.setOnClickListener(myButtonOnClickListener);
}
Button.OnClickListener myButtonOnClickListener
= new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
if(recording){
// stop recording and release camera
mediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
//Exit after saved
//finish();
myButton.setText("REC");
recording = false;
}else{
//Release Camera before MediaRecorder start
releaseCamera();
if(!prepareMediaRecorder()){
Toast.makeText(VideoWithSurfaceVw.this,
"Fail in prepareMediaRecorder()!\n - Ended -",
Toast.LENGTH_LONG).show();
finish();
}
mediaRecorder.start();
recording = true;
myButton.setText("STOP");
}
}catch (Exception ex){
ex.printStackTrace();
}
}};
private Camera getCameraInstance(){
// TODO Auto-generated method stub
Camera c = null;
try {
c = Camera.open(0); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
private boolean prepareMediaRecorder(){
myCamera = getCameraInstance();
// set the orientation here to enable portrait recording.
setCameraDisplayOrientation(this,0,myCamera);
mediaRecorder = new MediaRecorder();
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setOutputFile("/sdcard/myvideo1.mp4");
mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(50000000); // Set max file size 50Mb
mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
mediaRecorder.setOrientationHint(VideoWithSurfaceVw.orientation);
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
#Override
protected void onPause() {
super.onPause();
releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera(); // release the camera immediately on pause event
}
private void releaseMediaRecorder(){
if (mediaRecorder != null) {
mediaRecorder.reset(); // clear recorder configuration
mediaRecorder.release(); // release the recorder object
mediaRecorder = new MediaRecorder();
myCamera.lock(); // lock camera for later use
}
}
private void releaseCamera(){
if (myCamera != null){
myCamera.release(); // release the camera for other applications
myCamera = null;
}
}
public class MyCameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private Camera mCamera;
private Activity mActivity;
public MyCameraSurfaceView(Context context, Camera camera,Activity activity) {
super(context);
mCamera = camera;
mActivity=activity;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
try {
setCameraDisplayOrientation(mActivity,0,mCamera);
previewCamera();
} catch (Exception e) {
e.printStackTrace();
}
}
public void previewCamera()
{
try
{
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
}
catch(Exception e)
{
//Log.d(APP_CLASS, "Cannot start preview", e);
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.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;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
VideoWithSurfaceVw.orientation=result;
camera.setDisplayOrientation(result);
}
}
I am making a Camera example on Android, currently it just takes a picture and saves it on the device's (internal) memory.
After taking a picture, the screen stays frozen on the last clicked picture and I have to exit the app and open it again to take another picture.
Here is my code:
MainActivity.java:
package com.example.camera;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.Surface;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends Activity
{
private Camera mCamera;
private ShowCamera showCamera;
public Camera isCameraAvailable()
{
Camera camera=null;
Log.d("isCameraAvailable","Camera object set to null.");
try
{
camera = Camera.open();
Log.d("isCameraAvailable","Camera opened.");
}
catch (Exception e)
{
Log.d("isCameraAvailable",e.toString());
}
return camera;
}
private static File getOutputMediaFile()
{
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"com.example.camera");
if (!mediaStorageDir.exists())
{
if (!mediaStorageDir.mkdirs())
{
Log.d("getOutputMediaFile","Failed to create directory.");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "IMG_" + timeStamp + ".jpg");
Log.d("getOutputMediaFile","Filename created.");
return mediaFile;
}
private PictureCallback capturedIt = new PictureCallback()
{
#Override
public void onPictureTaken(byte[] data, Camera camera)
{
File pictureFile = getOutputMediaFile();
if (pictureFile == null)
{
Log.d("onPictureTaken","pictureFile=null");
return;
}
try
{
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
Log.d("onPictureTaken","File written.");
fos.close();
Log.d("onPictureTaken","File closed.");
Toast.makeText(MainActivity.this,pictureFile.toString()+" saved!", Toast.LENGTH_LONG).show();
return;
}
catch (FileNotFoundException e)
{
Log.e("onPictureTaken",e.toString());
}
catch (IOException e)
{
Log.d("onPictureTaken",e.toString());
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCamera = isCameraAvailable();
setCameraDisplayOrientation(this,0,mCamera);
Log.d("onCreate","Camera instance taken.");
showCamera = new ShowCamera(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.flCameraPreview);
preview.addView(showCamera);
Log.d("onCreate","Camera view in initialized");
preview.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
mCamera.takePicture(null, null, capturedIt);
Log.d("onClick","Button Clicked.");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public static void setCameraDisplayOrientation(Activity activity,int cameraId, android.hardware.Camera camera)
{
android.hardware.Camera.CameraInfo info=new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId,info);
int rotation = activity.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;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
{
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360;
}
else
{
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
}
ShowCamera.java:
package com.example.camera;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback
{
private SurfaceHolder holdMe;
private Camera mCamera;
public ShowCamera(Context context,Camera camera)
{
super(context);
this.mCamera = camera;
this.holdMe = getHolder();
this.holdMe.addCallback(this);
this.holdMe.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,int width, int height)
{
try
{
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
}
catch (Exception e)
{
Log.d("surfaceChanged",e.toString());
}
}
#Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
}
catch (IOException e)
{
Log.e("surfaceCreated",e.toString());
}
}
#Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder)
{
mCamera.stopPreview();
mCamera.release();
}
}
After taking a picture and saving it to memory, if I click the screen once more, the app crashes.
You have stopPreview in surfaceDestroyed. but when it cames back. you have to restart the Camera in onResume . which is missing
public void onResume()
{
mCamera = getCameraInstance(); // i have created this i
// static method here
//mCamera.setPreviewCallback(previewCallback);//it depends on you need
try
{
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
}
catch (IOException ioexception)
{
Log.e("MyCamera",
(new StringBuilder()).append("PreviewDisplay Error => ")
.append(ioexception.getMessage()).toString());
}
}
And
public static Camera getCameraInstance()
{
Camera camera1;
try
{
if (android.os.Build.VERSION.SDK_INT >= 9)
{
return Camera.open(cameraId);
}
camera1 = Camera.open();
}
catch (Exception exception)
{
Log.e("NightVision",
(new StringBuilder()).append("ErrorCameraInit: ")
.append(exception.getMessage()).toString());
return null;
}
return camera1;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I don't know that I'm doing wrong, everyrthings seems to be ok, but no picture is being saved in the telephone when I press capture.
the message picture has being save appears, but there's nothing on my gallery.
package com.mamlambo.mirror;
import java.io.File;
import java.io.FileOutputStream;
import com.mamlambo.mirror.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
public class MirrorActivity extends Activity implements PictureCallback {
private final static String DEBUG_TAG = "MirrorActivity";
private Camera mCam;
private MirrorView mCamPreview;
private int mCameraId = 0;
private FrameLayout mPreviewLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// do we have a camera?
if (!getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(this, "No camera feature on this device",
Toast.LENGTH_LONG).show();
} else {
mCameraId = findFirstFrontFacingCamera();
if (mCameraId >= 0) {
mPreviewLayout = (FrameLayout) findViewById(R.id.camPreview);
mPreviewLayout.removeAllViews();
startCameraInLayout(mPreviewLayout, mCameraId);
Button takePic = (Button) findViewById(R.id.capture);
takePic.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCam.takePicture(null, null, MirrorActivity.this);
}
});
} else {
Toast.makeText(this, "No front facing camera found.",
Toast.LENGTH_LONG).show();
}
}
}
#SuppressLint("NewApi")
private int findFirstFrontFacingCamera() {
int foundId = -1;
// find the first front facing camera
int numCams = Camera.getNumberOfCameras();
for (int camId = 0; camId < numCams; camId++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(camId, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
Log.d(DEBUG_TAG, "Found front facing camera");
foundId = camId;
break;
}
}
return foundId;
}
private void startCameraInLayout(FrameLayout layout, int cameraId) {
// TODO pull this out of the UI thread.
mCam = Camera.open(cameraId);
if (mCam != null) {
mCamPreview = new MirrorView(this, mCam);
layout.addView(mCamPreview);
}
}
#Override
protected void onResume() {
super.onResume();
if (mCam == null && mPreviewLayout != null) {
mPreviewLayout.removeAllViews();
startCameraInLayout(mPreviewLayout, mCameraId);
}
}
#Override
protected void onPause() {
if (mCam != null) {
mCam.release();
mCam = null;
}
super.onPause();
}
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFileDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"SimpleSelfCam");
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
Log.d(DEBUG_TAG, "Can't create directory to save image");
Toast.makeText(this, "Can't make path to save pic.",
Toast.LENGTH_LONG).show();
return;
}
String filename = pictureFileDir.getPath() + File.separator
+ "latest_mug.jpg";
File pictureFile = new File(filename);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
Toast.makeText(this, "Image saved as latest_mug.jpg",
Toast.LENGTH_LONG).show();
} catch (Exception error) {
Log.d(DEBUG_TAG, "File not saved: " + error.getMessage());
Toast.makeText(this, "Can't save image.", Toast.LENGTH_LONG).show();
}
}
public class MirrorView extends SurfaceView implements
SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public MirrorView(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (Exception error) {
Log.d(DEBUG_TAG,
"Error starting mPreviewLayout: " + error.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
public void surfaceChanged(SurfaceHolder holder, int format, int w,
int h) {
if (mHolder.getSurface() == null) {
return;
}
// can't make changes while mPreviewLayout is active
try {
mCamera.stopPreview();
} catch (Exception e) {
}
try {
// set rotation to match device orientation
setCameraDisplayOrientationAndSize();
// start up the mPreviewLayout
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception error) {
Log.d(DEBUG_TAG,
"Error starting mPreviewLayout: " + error.getMessage());
}
}
public void setCameraDisplayOrientationAndSize() {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(mCameraId, info);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
int degrees = rotation * 90;
/*
* // the above is just a shorter way of doing this, but could break
* if the values change 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;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360;
} else {
result = (info.orientation - degrees + 360) % 360;
}
mCamera.setDisplayOrientation(result);
Camera.Size previewSize = mCam.getParameters().getPreviewSize();
if (result == 90 || result == 270) {
// swap - the physical camera itself doesn't rotate in relation
// to the screen ;)
mHolder.setFixedSize(previewSize.height, previewSize.width);
} else {
mHolder.setFixedSize(previewSize.width, previewSize.height);
}
}
}
}
Pictures do not automatically save to the gallery even if you store them to the correct file directory. To make the pictures appeare in the gallery you need to have the android MediaScanner add them. You can access the MediaScanner either by Intent or with a method/callback that will notify you when the picture has been added. Here are some examples but a seach of S/O should turn up even more results.
Good day! I'm learning how to record video with MediaRecorder but recorded video is corrupted when I play it. See this screenshot: http://www.4shared.com/photo/QtmJCHRi/corrupted-video.html. Picture marked with red rectangle in the left upper corner is what camera can see. But it is so small, it's repeating and there is many green areas. Please advise what am I doing wrong. HW is Samsung Galaxy S2 (GT-I9100, Android 2.3.5). I tried to follow this tutorial: http://developer.android.com/guide/topics/media/camera.html#saving-media
Thank you in advance!
CameraRecorderActivity.java
package cz.ryvo.android.camerarecorder;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
public class CameraRecorderActivity extends Activity
implements SurfaceHolder.Callback, OnClickListener {
private static final String TAG = "CameraRecorderActivity";
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
private Camera mCamera;
private CameraPreview mPreview;
private MediaRecorder mMediaRecorder;
private Button captureButton;
private boolean isRecording = false;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create an instance of Camera.
mCamera = getCameraInstance();
// Create preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
int i = R.id.camera_preview;
Object o = this.findViewById(i);
FrameLayout preview = (FrameLayout) o;
preview.addView(mPreview);
// Add a listener to the Capture button
captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isRecording) {
// stop recording and release camera
mMediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
mCamera.lock(); // take camera access back from MediaRecorder
// inform the user that recording has stopped
setCaptureButtonText("Capture");
isRecording = false;
} else {
// initialize video camera
if (prepareVideoRecorder()) {
// Camera is available and unlocked, MediaRecorder is prepared,
// now you can start recording
mMediaRecorder.start();
// inform the user that recording has started
setCaptureButtonText("Stop");
isRecording = true;
} else {
// prepare didn't work, release the camera
releaseMediaRecorder();
// inform user
}
}
}
}
);
}
public void setCaptureButtonText(String s) {
captureButton.setText(s);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
private boolean prepareVideoRecorder(){
//mCamera = getCameraInstance();
mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setVideoSize(720, 480);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
// Step 4: Set output file
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}
#Override
public void onClick(View v) {
/*
Log.i("onClick", "BEGIN");
if(!recording) {
recording = startRecording();
} else {
stopRecording();
recording = false;
}
Log.i("onClick", "END");
*/
}
#Override
protected void onPause() {
super.onPause();
releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera(); // release the camera immediately on pause event
}
private void releaseMediaRecorder(){
if (mMediaRecorder != null) {
mMediaRecorder.reset(); // clear recorder configuration
mMediaRecorder.release(); // release the recorder object
mMediaRecorder = null;
mCamera.lock(); // lock camera for later use
}
}
private void releaseCamera(){
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
private Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
//c = this.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
public Camera open() {
int numberOfCameras = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
return Camera.open(i);
}
}
return null;
}
}
CameraPreview.java
package cz.ryvo.android.camerarecorder;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "CameraPreview";
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// make any resize, rotate or reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
I read about it somewhere (I think it was in SO). The problem is that apparently, the Samsung Galaxy doesn't support recording in HIGH QUALITY (CamcorderProfile.QUALITY_HIGH).
Try with CamcorderProfile.QUALITY_LOW. to see if it works.
EDIT: I found the question with the same issue here