2

我只需要从前置摄像头录制视频,我搜索了很多,但无法找到解决方案(简单)

如果我将相机类型设置为 1,则应用程序崩溃..

这是我的代码

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

    }
}

有没有办法简单地记录到前置摄像头?谢谢!

编辑

这是我的实际代码(如果我尝试打开前置摄像头,它就不起作用)

      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);





      }

}

我不知道为什么它不打开前置摄像头...错误是“在无效状态下开始调用”(因为它找到了前置摄像头,并尝试设置)

4

2 回答 2

4

您需要找到前置摄像头的ID。要做到这一点,去

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;
    }
}

如果 found 为真,则i为前置摄像头 ID。然后你需要打开那个相机并把它传进去

recorder.setCamera(Camera.open(i));
于 2013-07-06T12:30:45.007 回答
1

现在回答为时已晚,但我遇到了同样的问题,最后我在堆栈上发现了问题。您可以为后置摄像头设置高配置。

//For Front Camera
     CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);

但你不能为前置摄像头设置高调。我在我的情况下使用了 480P

//For back camera
     CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);

你可以在这篇文章中找到详细信息。Android 无法使用前置摄像头录制视频,MediaRecorder 启动失败:-19

于 2016-02-19T03:56:55.273 回答