以下是使用Camera 必備的 Activity 架構
public class MainActivity extends Activity implements SurfaceHolder.Callback{
private SurfaceView surfaceview;
private MediaRecorder mediarecorder;
private SurfaceHolder surfaceHolder;
private Button startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
@Override
protected void onResume(){
camera = Camera.open();
if (phoneConfigure.orientation == Configuration.ORIENTATION_PORTRAIT)
camera.setDisplayOrientation(90);
super.onResume();
}
@Override
protected void onPause(){
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
super.onPause();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
Camera.Parameters parameters = camera.getParameters();
parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try {
camera.setPreviewDisplay(surfaceHolder);
} catch (IOException e) {
// TODO Auto-generated catch block
camera.release();
camera = null;
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
下面是錄影的相關重要程式碼
public void startRecord(int videoWidth,int videoHeight){
mediarecorder = new MediaRecorder();
camera.stopPreview();
camera.unlock();
mediarecorder.setCamera(camera);
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediarecorder.setVideoSize(videoWidth,videoHeight);
if (phoneConfigure.orientation == Configuration.ORIENTATION_PORTRAIT)
mediarecorder.setOrientationHint(90);
mediarecorder.setOutputFile(createVideoFilePath(fileExtention));
mediarecorder.setPreviewDisplay(surfaceHolder.getSurface());
try {
mediarecorder.prepare();
mediarecorder.start();
}catch (Exception e) {
mediarecorder.stop();
mediarecorder.release();
e.printStackTrace();
toast(e.getCause().toString());
}
}
public void stopRecord(){
if (mediarecorder != null) {
mediarecorder.stop();
mediarecorder.release();
mediarecorder = null;
}
}
完整相關程式碼:
Github
沒有留言:
張貼留言