我试图打开我的相机而不点击我的按钮但它不起作用...
我按照此处的说明操作:http://developer.android.com/guide/topics/media/camera.html#custom-camera
这是我的结果:
当我按下“Capture”按钮时,它会激活相机,但我想在此之前激活相机,只需在我点击像Snapchat这样的按钮时进行记录。当我打开相机但我没有发现错误时,我想我会错过某个地方......
这是我的班级:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preview = (FrameLayout) findViewById(R.id.camera_preview);
if (checkCameraHardware(this)) {
mCamera = getCameraInstance();
mPreview = new CameraPreview(this, mCamera);
mCamera.setDisplayOrientation(90);
initButton();
preview.addView(mPreview);
preview.removeAllViews();
preview.addView(mPreview);
preview.addView(captureButton);
}
}
之后,我有启动按钮的方法:
public void initButton () {
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();
releaseMediaRecorder();
mCamera.lock();
// inform the user that recording has stopped
captureButton.setText("Capture");
isRecording = false;
} else {
// initialize video camera
if (prepareVideoRecorder()) {
// Camera is available and unlocked, MediaRecorder is prepared,
// now you can start recording
mMediaRecorder.start();
captureButton.setText("Stop");
isRecording = true;
} else {
releaseMediaRecorder();
Toast.makeText(MainActivity.this, "Camera doesn't work", Toast.LENGTH_LONG).show();
}
}
}
});
}
这是我获取相机实例的方法:
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open();
} catch (Exception e) {
Log.i("CAMERA INFO : >", "Camera doesn't exist");
}
return c;
}
以下是准备记录的方法:
private boolean prepareVideoRecorder() {
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);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mMediaRecorder.setProfile(camcorderProfile);
// Step 4: Set output file
mMediaRecorder.setOutputFile(getOutputMediaFile(CAPTURE_VIDEO_FILE).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("PREPARE MEDIARECORDER : >", ": > > IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d("PREPARE MEDIARECORDER : >", " : > > IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}
然后我尝试在实例化之前打开相机,但我不知道如何...如果有人有想法,对我有用。
答案 0 :(得分:1)
我发现了我的错误。我在CameraPreview类的mCamera.setPreview()
方法中调用了onSurfaceCreated()
。
我需要在mCamera.setPreview()
中致电onSurfaceChanged()
!现在它可以工作
问题解决了!
答案 1 :(得分:0)
您需要在按下捕获按钮之前设置摄像机的预览流。您的代码中的mPreview参考中似乎已经有一个预览类。您需要确保它包含Camera#startPreview() call when the preview surface is created才能显示来自摄像头的实时图像。要记住的另一件事是预览类,它是一个Android视图,需要是可见的,以便正确设置其表面,这意味着你必须确保它正确创建及其visibility设置为VISIBLE。