Android SurfaceView拍照录像实现方法攻略
在Android开发中,使用SurfaceView可以实现拍照和录像功能。下面是详细的攻略,包含两个示例说明。
示例一:拍照功能实现
- 在布局文件中添加SurfaceView组件:
<SurfaceView
android:id=\"@+id/surfaceView\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\" />
- 在Activity中获取SurfaceView的引用,并实现SurfaceHolder.Callback接口:
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// 初始化相机
Camera camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// 相机预览尺寸发生变化时的处理
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// 释放相机资源
camera.stopPreview();
camera.release();
}
}
- 在拍照按钮的点击事件中调用相机拍照方法:
public void takePhoto(View view) {
camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// 处理拍照结果
}
});
}
示例二:录像功能实现
- 在布局文件中添加SurfaceView组件:
<SurfaceView
android:id=\"@+id/surfaceView\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\" />
- 在Activity中获取SurfaceView的引用,并实现SurfaceHolder.Callback接口:
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private MediaRecorder mediaRecorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// 初始化MediaRecorder
mediaRecorder = new MediaRecorder();
mediaRecorder.setPreviewDisplay(holder.getSurface());
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediaRecorder.setOutputFile(getOutputMediaFile().toString());
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaRecorder.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// SurfaceView尺寸发生变化时的处理
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// 停止录像并释放资源
mediaRecorder.stop();
mediaRecorder.release();
}
private File getOutputMediaFile() {
// 创建保存录像文件的目录
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM), \"Camera\");
// 创建文件名
String timeStamp = new SimpleDateFormat(\"yyyyMMdd_HHmmss\").format(new Date());
File mediaFile = new File(mediaStorageDir.getPath() + File.separator +
\"VID_\" + timeStamp + \".mp4\");
return mediaFile;
}
}
- 在录像按钮的点击事件中停止录像:
public void stopRecording(View view) {
mediaRecorder.stop();
mediaRecorder.release();
}
以上就是使用SurfaceView实现拍照和录像功能的完整攻略。你可以根据自己的需求进行相应的修改和扩展。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android SurfaceView拍照录像实现方法 - Python技术站