一、 Android Camera實現方式
Android提供兩種實現方式給開發員來實現拍照功能:Camera API和Camera Intent.下面的章節主要是講解如何使用Camera Intent進行Camera開發.
Camera Intent
通過”Camera Intent”方式可以快速,方便以及更少的代碼來完成Camera功能.其通過Android的Intent調用系統現有的Camera應用程序來實現此功能.並實現”onActivityResult()”獲取並處理拍照返回的結果.
調用Camera Intent流程主要分為如下幾個步驟:
構建一個Camera Intent來向Android系統請求圖片和視頻
MediaStore.ACTION_IMAGE_CAPTURE 請求圖像
MediaStore.ACTION_VIDEO_CAPTURE 請求視頻
啟動Camera Intent
使用startActivityForResult()方法啟動Camera Intent
接收Camera Intent返回的結果
創建onActivityResult()方法接收回調和返回圖像數據
圖像拍照意圖(Image capture intent)
首先來看一下Demo的運行效果圖,具體操作是點擊”Take Photo!”按鈕打開系統自帶的Camera界面,拍攝完畢后將拍攝的照片顯示ImageView中.
開發步驟:
A. 新建一個Android項目,取名為”CameraIntentDemo”.
B. 構建主界面元素.
main.xml文件內容如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/btnTakePhoto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Take Photo!" />
<ImageView android:id="@+id/imgvTakePhoto" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFFF"/>
</LinearLayout> |
C. 使用Intent來啟動Camera功能.
首先聲明一個Intent,並以MediaStore.ACTION_IMAGE_CAPTURE實例化該Intent.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
然后設置Intent的MediaStore.EXTRA_OUTPUT屬性可以將拍攝的圖像保存到指定路徑.
// set the image file name
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
最后調用startActivityForResult方法來啟動系統的Camera功能.
D. 捕獲系統Camera的回調
重寫onActivityResult()方法,通過requestCode參數可以知道是否Camera Intent的Result,然后再根據Intent參數對接收到圖像數據進行處理或者保存.如果通過
MediaStore.EXTRA_OUTPUT屬性自動將圖像數據保存到指定的位置,那么在
onActivityResult()得到的Intent的數據是為NULL的.這一點大家需要注意一下.
E. 在AndroidManifest.xml添加訪問SD權限聲明.
<!-- 在SDCard中創建與刪除文件權限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 往SDCard寫入數據權限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
CameraIntentDemoActivity.java文件內容:
public class CameraIntentDemoActivity extends Activity {
private static final int MEDIA_TYPE_IMAGE = 1; private static final int MEDIA_TYPE_VIDEO = 2; private Uri fileUri;
private static Uri getOutputMediaFileUri(int type) { return Uri.fromFile(getOutputMediaFile(type)); }
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; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
Button btnTakePhoto = (Button)this.findViewById(R.id.btnTakePhoto); btnTakePhoto.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // create a file to save the image fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // set the image file name cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // start the image capture Intent startActivityForResult(cameraIntent, 0); } });
}
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { try { if (requestCode != 0) { return; } Bitmap bmp = null; super.onActivityResult(requestCode, resultCode, data); if (data != null) { Bundle extras = data.getExtras(); bmp = (Bitmap)extras.get("data"); } else { FileInputStream fis = new FileInputStream(fileUri.getPath()); bmp = BitmapFactory.decodeStream(fis); }
ImageView imag = (ImageView)this.findViewById(R.id.imgvTakePhoto); imag.setImageBitmap(bmp);
} catch (Exception e){ System.out.println(e.getMessage()); }
} } |
視頻拍攝意圖(Video capture intent)
使用Camera Intent同樣可以快速,方便以及更少代碼實現視頻拍攝.它還支持多種擴展特性.
• MediaStore.EXTRA_OUTPUT – 設置視頻輸出位置.
• MediaStore.EXTRA_VIDEO_QUALITY – 設置視頻質量,0為最差,1為最清晰.
• MediaStore.EXTRA_DURATION_LIMIT – 設置視頻的限制長度.
• MediaStore.EXTRA_SIZE_LIMIT – 設置視頻的限制大小.
開發步驟:
A. 新建一個Android Project命名為VideoIntentDemo,指定最低版本號.
B. 構建界面元素
同樣是放置一個Button來觸發視頻拍攝,下面放置一個TextView用來存放返回信息.
界面布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/btTakeVideo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Take Video" />
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:id="@+id/tvActivityResult" android:minLines="5" />
</LinearLayout> |
C. 代碼實現
首先聲明一個Uri對象用來保存視頻文件保存的路徑.
private Uri fileUri;
然后實現Video功能代碼.
// create new Intent
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
// create a file to save the video
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
// set the video file name
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// set the video image quality to high
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
// start the Video Capture Intent
startActivityForResult(intent, 1);
VideoIntentDemoActivity.java文件內容:
public class VideoIntentDemoActivity extends Activity {
private Uri fileUri;
private static final int MEDIA_TYPE_IMAGE = 1; private static final int MEDIA_TYPE_VIDEO = 2;
private static Uri getOutputMediaFileUri(int type) { return Uri.fromFile(getOutputMediaFile(type)); }
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; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
Button btnTakeVideo = (Button)this.findViewById(R.id.btTakeVideo); btnTakeVideo.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { // create new Intent Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
// create a file to save the video fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); // set the video file name intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the video image quality to high intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
// start the Video Capture Intent startActivityForResult(intent, 1); } }); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { TextView tvVideo = (TextView)this.findViewById(R.id.tvActivityResult); try { if (requestCode == 1) { tvVideo.setText("視頻保存成功,路徑為:" + fileUri.getPath()); } super.onActivityResult(requestCode, resultCode, data); } catch (Exception e) { tvVideo.setText(e.getLocalizedMessage()); } } } |
D. 在AndroidManifest.xml添加訪問SD權限聲明.
<!-- 在SDCard中創建與刪除文件權限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 往SDCard寫入數據權限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
最后附上源碼下載: