我想在一个活动中添加前后摄像头

时间:2016-12-03 09:36:09

标签: android android-camera

我正在创建一个Android应用程序,我想在相同的活动中添加前后摄像头并记录视频并发送到服务器我是新的,我不知道它是否可能请尽可能帮助我请指导我

我看到此应用this,这也是this

此示例代码

public class TestDualCam extends Activity implements View.OnClickListener, SurfaceHolder.Callback{

private Button record_frontCam, record_rearCam;
MediaRecorder mediaRecorder;
int cameraType = 1;
private SurfaceView frontSurface, rearSurface;
private SurfaceHolder surfaceHolder;
private boolean recording;
Camera camera;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    recording = false;
    mediaRecorder = new MediaRecorder();
    init();
    initMediaRecorder();
    initfrontMeadiaRecorder();
    surfaceHolder = rearSurface.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder = frontSurface.getHolder();
    surfaceHolder.addCallback(this);
    record_rearCam.setOnClickListener(this);
    record_frontCam.setOnClickListener(this);
}

private void init(){
    record_frontCam = (Button)findViewById(R.id.record1);
    record_rearCam =(Button)findViewById(R.id.record2);
    frontSurface =(SurfaceView)findViewById(R.id.surfaced1);
    rearSurface =(SurfaceView)findViewById(R.id.surfaced2);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.record1:
            Toast.makeText(TestDualCam.this, "Front Camera Open", Toast.LENGTH_SHORT).show();
            if(recording){
                mediaRecorder.stop();
                mediaRecorder.release();
                finish();
            }else{
                mediaRecorder.start();
                recording = true;
                record_frontCam.setText("STOP");
            }
            break;

        case R.id.record2:
            Toast.makeText(TestDualCam.this, "Rear Camera Open", Toast.LENGTH_SHORT).show();
            if(recording){
                mediaRecorder.stop();
                mediaRecorder.release();
                finish();
            }else{
                mediaRecorder.start();
                recording = true;
                record_rearCam.setText("STOP");
            }
            break;

    }


}

@Override
public void surfaceCreated(SurfaceHolder holder) {

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    prepareMediaRecorder();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}
private void initMediaRecorder(){
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    mediaRecorder.setProfile(camcorderProfile_HQ);
    mediaRecorder.setOutputFile("/sdcard/myvideo1.mp4");
    mediaRecorder.setMaxDuration(30000); // Set max duration 30 sec.
    mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
}

private void prepareMediaRecorder(){
    mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
    try {
        mediaRecorder.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
private void initfrontMeadiaRecorder(){
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile camcorderProfile_HQ =        CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mediaRecorder.setProfile(camcorderProfile_HQ);
mediaRecorder.setOutputFile("/sdcard/myvideo1.mp4");
mediaRecorder.setMaxDuration(30000); // Set max duration 30 sec.
mediaRecorder.setMaxFileSize(5000000);

  }
}

0 个答案:

没有答案