Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

android - how to record from front camera

I have to record the video from the front camera only, I Googled a lot, but have not been able to find a solution (simple)

if i set the cameratype to 1, the app crashes ..

here is my code

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class VideoCapture extends Activity implements OnClickListener, SurfaceHolder.Callback {
    MediaRecorder recorder;
    SurfaceHolder holder;
    boolean recording = false;
    String pathVideo;
    private int cameraType = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      /*  requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
               WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); */

        pathVideo = "/reg_" + System.currentTimeMillis() + ".mp4";
        recorder = new MediaRecorder();
        initRecorder();
        setContentView(R.layout.camera);

        SurfaceView cameraView = (SurfaceView) findViewById(R.id.surface_camera);
        holder = cameraView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);



        Button rec =(Button) findViewById(R.id.buttonstart);
        rec.setOnClickListener(new OnClickListener() {
            public void onClick(View v)
            {
                  if (recording) {
                      recorder.stop();
                      recording = false;
                      recorder.release();
                      // Let's initRecorder so we can record again
                    initRecorder();
                    prepareRecorder();
                  } else {
                      recording = true;
                      recorder.start();
                  }
            } 
         });
    }

    private void initRecorder() {

        recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        CamcorderProfile cpHigh = CamcorderProfile
                .get(CamcorderProfile.QUALITY_HIGH);
        recorder.setProfile(cpHigh);


          File Directory = new File("/sdcard/CantaTu/");
       // have the object build the directory structure, if needed.
       Directory.mkdirs();
       File mediaFile = new File(Directory,pathVideo);

        if(mediaFile.exists()){
         mediaFile.delete();
        }
        recorder.setOutputFile(mediaFile.getAbsolutePath());
        recorder.setMaxDuration(400000); // 50 seconds
        recorder.setMaxFileSize(50000000); // Approximately 5 megabytes
    }

    private void prepareRecorder() {
        recorder.setPreviewDisplay(holder.getSurface());

        try {

            recorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
            finish();
        } catch (IOException e) {
            e.printStackTrace();
            finish();
        }
    }



    public void surfaceCreated(SurfaceHolder holder) {
        //camera = Camera.open(cameraType);
        prepareRecorder();
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        if (recording) {
            recorder.stop();
            recording = false;
            recorder.release();
        }
        recorder.release();
        //finish();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
}

there're no way to simply recordo to the front camera? thank's!

EDIT

This is my actual code (and it don't work if i try to open front camera)

      public void inizializzazione(){


          cameraView.setVisibility(0);


          boolean found = false;
             int i;

             for(i=0; i< Camera.getNumberOfCameras(); i++){
                 System.out.println("camera n " +i);
                 Camera.CameraInfo newInfo = new Camera.CameraInfo();
                 Camera. getCameraInfo(i, newInfo);

                 if (newInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                     try {
                         found = true;
                        cam = Camera.open(i);
                        System.out.println("trovata la fotocamera frontale");
                     } catch (RuntimeException e) {
                         Log.e("Your_TAG", "Camera failed to open: " + e.getLocalizedMessage());
                     }


             }



            pathVideo = "/reg_" + System.currentTimeMillis() + ".mp4";
          File Directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/CantaTu/");
           // have the object build the directory structure, if needed.
           Directory.mkdirs();
           File mediaFile = new File(Directory,pathVideo);

            if(mediaFile.exists()){
             mediaFile.delete();
            }



            recorder = new MediaRecorder();

             holder = cameraView.getHolder();
             holder.addCallback(this);
             holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

            recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            recorder.setVideoSource(1);

             CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);

            recorder.setProfile(cpHigh);

             recorder.setOutputFile(mediaFile.getAbsolutePath());
             recorder.setMaxDuration(400000); // 50 seconds

             recorder.setPreviewDisplay(holder.getSurface());




             try {
                 if(found == true){
                     recorder.setCamera(cam);
                 } 
                recorder.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

             recorder.start();

             scritta.setVisibility(0);
             caricamento.setVisibility(4);





      }

}

i have no idea why it don't open the front camera... The error is "start called in an invalid state" (Because it found the front camera, and try to set)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to find the id of the front camera. To do that, go

boolean found = false;
int i;
for (i=0; i< Camera.getNumberOfCameras(); i++) {
    Camera.CameraInfo newInfo = new Camera.CameraInfo();
    Camera.getCameraInfo(i, newInfo);
    if (newInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
        found = true;
        break;
    }
}

If found is true, i is the front camera id. Then you need to open that camera and pass it in

recorder.setCamera(Camera.open(i));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...