Android camera 篇之视频录制
程序员文章站
2022-03-11 22:38:32
本文是camera系列文章第一篇,预计三篇 :一 拍照;二 视频;三 自定义相机相机功能是我们最常用的,属于andorid基础能力。本文讲述一下Android如何调用相机进行录制视频。流程获取权限唤醒相机录视频并播放一、获取权限 /** * 所需权限数组 相机 读 写 */ private String[] permissions = new String[]{"android.permission.CAMERA", "and...
本文是camera系列文章第一篇,预计三篇 :一 拍照;二 视频;三 自定义相机
相机功能是我们最常用的,属于andorid基础能力。本文讲述一下Android如何调用相机进行录制视频。
流程
获取权限
唤醒相机
录视频并播放
一、获取权限
/**
* 所需权限数组 相机 读 写
*/
private String[] permissions = new String[]{"android.permission.CAMERA",
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"};
private int takeCameraCode = 1001; // 获取全尺寸图
private int cameraCode = 10000; // 权限请求码
/**
* 申请权限
*/
private void requestPermission() {
ActivityCompat.requestPermissions(this,permissions,cameraCode);
}
/**
* 权限处理
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
/**
* 权限处理
*/
if (requestCode == cameraCode) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
requestPermission();
return;
}
}
openCamera();
}
}
二、唤醒相机
/**
* 唤醒系统相机
*/
private void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent,takeCameraCode);
}
}
三、获取视频并播放
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == takeCameraCode && resultCode == RESULT_OK && data != null) {
Uri videoUri = data.getData();
video.setVideoURI(videoUri);
video.start();
video.requestFocus();
}
}
本文对视频不做处理。camera写完之后,会准备写一些音视频处理的文章。
完整代码如下:
public class VideoCameraActivity extends AppCompatActivity {
/**
* 所需权限数组 相机 读 写
*/
private String[] permissions = new String[]{"android.permission.CAMERA",
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"};
private int takeCameraCode = 1001; // 获取全尺寸图
private int cameraCode = 10000; // 权限请求码
private VideoView video;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_camera);
video = findViewById(R.id.video);
findViewById(R.id.open_camera).setOnClickListener(v -> {
requestPermission();
});
}
/**
* 申请权限
*/
private void requestPermission() {
ActivityCompat.requestPermissions(this,permissions,cameraCode);
}
/**
* 权限处理
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
/**
* 权限处理
*/
if (requestCode == cameraCode) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
requestPermission();
return;
}
}
openCamera();
}
}
/**
* 唤醒系统相机
*/
private void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent,takeCameraCode);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == takeCameraCode && resultCode == RESULT_OK && data != null) {
Uri videoUri = data.getData();
video.setVideoURI(videoUri);
video.start();
video.requestFocus();
}
}
}
本文完,欢迎评论。
本文地址:https://blog.csdn.net/Naide_S/article/details/108122611