如何从前置摄像头录制 [英] how to record from front camera

查看:26
本文介绍了如何从前置摄像头录制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

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

这是我的代码

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!

编辑

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

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





      }

}

我不知道为什么它不打开前置摄像头...错误是start calls in an invalid state"(因为它找到了前置摄像头,并尝试设置)

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)

推荐答案

需要找到前置摄像头的id.要做到这一点,去

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

如果 found 为真,i 是前置摄像头 id.然后你需要打开那个摄像头并传入

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

这篇关于如何从前置摄像头录制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆