一、前言
在我们使用Camera时我们应该首先考虑以下几个注意事项
1、我们的设备必须要有摄像头,以及我们应该在AndroidManifest.xml中将需要用到的权限必须加上。
2、我们的应用程序用Camera来干什么?能否考虑使用系统自带的Camera应用,还是需要自定义一个相机。
3、我们的媒体文件怎样存储,是私有,还是共享?以及我们的相册应用可以查看这些文件吗?
我们接下来就针对上面的问题进行讨论。
二、清单文件的声明
1、Camera 权限
<uses-permission android:name="android.permission.CAMERA" />
2、Camera Features
<uses-feature android:name="android.hardware.camera" />
如果使用只使用系统相机可以不声明这个。
3、若果应用程序 不需要 设备提供声明的特性也能正确运行的话,可以这样声明
<uses-feature android:name="android.hardware.camera" android:required="false" />
4、存储权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
5、录制需要的权限
<uses-permission android:name="android.permission.RECORD_AUDIO" />
6、如果拍摄的图片需要有GPS信息
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
若果我们还依赖一些硬件特性 需继续声明< use- fuature >
比如如果我们需要自动对焦,则需加入
<uses-feature android:name="android.hardware.camera.autofocus"></uses-feature>
Camera的其他 硬件特性请参考:
https://developer.android.com/guide/topics/manifest/uses-feature-element.html#hw-features
三、使用系统的相机应用
一个最快的方法使用拍照和摄像同能就是通过Intent来调用已经存在的相机应用来进行拍照和录像。调用存在的相机应用之后三步
1、创建一个Intent加入Action
MediaStore.ACTION_IMAGE_CAPTURE————-请求照相
MediaStore.ACTION_VIDEO_CAPTURE————-请求录像
2、使用 startActivityForResult() 调用相机应用
3、在 onActivityResult() 方法里接收数据
示例:
1、调用系统相机拍照
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
其中MediaStore.EXTRA_OUTPUT - - 它用来指定文件的存储位置,这个选项是个可选项,但强烈建议使用,如果没有设置,则相机应用则将文件保存在一个默认的位置,以及一个默认的名字。将必须在 OnActivityResult 使用Intent.getData()来获取数据。
getOutputMediaFileUri(MEDIA_TYPE_IMAGE) 是一个创建 Uri的工具方法
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
2、使用系统应用录像:
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
private Uri fileUri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//create new Intent
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); // create a file to save the video
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
// start the Video Capture Intent
startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
}
Intent中可以设置几种选项:
MediaStore.EXTRA_OUTPUT ——–它和上文描述的相同,表示文件的存储位置
MediaStore.EXTRA_VIDEO_QUALITY——-这个用来设置摄像的质量 0 为低质量,1为高质量
MediaStore.EXTRA_DURATION_LIMIT—-录制的时长 单位为秒
MediaStore.EXTRA_SIZE_LIMIT——–文件的大小,单位为byte
接收示例:
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Video captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Video saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the video capture
} else {
// Video capture failed, advise user
}
}
}