如果你的应用程序要使用Camera,则必须获得使用许可,需要在AndroidManifest.xml中加入如下声明。
<uses-permissionandroid:name="android.permission.CAMERA" />
如果你的应用程序必须有Camera才能使用,则声明如下:
<uses-featureandroid:name="android.hardware.camera" />
否则应声明如下:
<uses-featureandroid:name="android.hardware.camera"android:required="false" />
其他相关声明:
存储:<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />
语音:<uses-permissionandroid:name="android.permission.RECORD_AUDIO" />
位置:<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" />
核心类
Itent:通过它进行Camera编程更简单。
Camera:通过它可以控制相机,构建功能更强大的Camera应用。
SurfaceView:提供实时预览功能。
通过Itent来实现拍照
第一步:在Eclipse中创建一个名为Photo的Android工程,可参见Hello world的例子;
第二步:在AndroidManifest.xml中添加使用Camera相关的声明如下:
<uses-permissionandroid:name="android.permission.CAMERA"/>
第三步:编写MainActivity类。
(1) 添加成员变量
(2) 修改OnCreate方法
(3) 增加onActivityResult方法
(4) 新增两个辅助方法
类的源代码如下:
public class MainActivity extends Activity {
private static final int MEDIA_TYPE_IMAGE = 1;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Intent intent = null;
private Uri fileUri = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//create a intent to take picture
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);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, 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
}
}
}
/** 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.
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 = null;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}
return mediaFile;
}
}
第四步:运行程序。
如果拍完照返回的时候,程序崩溃,查看日志出现如下错误:
java.lang.RuntimeException: Failure delivering resultResultInfo{who=null, request=100, result=-1, data=null} to activity
在AndroidManifest.xml中的activity元素加入android:launchMode="singleInstance"属性即可解决该问题。
拍完照之后,可以在SD卡中的Pictures/MyCameraApp目录下找到保存的照片。
相关阅读:
Android图像处理简介の使用内置Camera应用程序进行图像捕获
Android平台接收基于 PC Camera 直播流的方案