安卓讀取視頻的幾種方式:
詳細講述請參考網址:http://www.cnblogs.com/over140/archive/2011/11/16/2251344.html
一、准備工作
1.用戶權限
首先要確保在manifest中聲明了對攝像頭的使用及其他相關的feature;
manifest中定義:
(1)Camera權限——應用程序必須對請求攝像頭的使用權限,代碼:
<uses-permission android:name="android.permission.CAMERA" />
(2) Camera Feature——應用程序必須同時聲明對camera feature的使用,代碼:
<uses-feature android:name="android.hardware.camera" />
(3)以上是兩個主要的設置,如果有其他額外功能,比如使用自動對焦,還需要使用:
<uses-feature android:name="android.hardware.camera.autofocus" />
詳細內容可以參考文章前面的網站內容;
二、實現方法
1.使用intent通知安卓操作系統
(1)在主activity中使用intent通知內置的攝像機應用;用戶在使用攝像進行拍照過后返回主activity圖片數據,在主activity
中加入接收數據的方法,對返回的圖像進行操縱;
優點:這種方法比較簡單,且利用了android內置的應用,使用於一般性的應用程序開發;
缺點:不夠靈活,不適用自定義的方法,可擴展性較差;
(2)調用步驟
通常按以下步驟來提交一個攝像頭 intent:
首先,構建一個攝像頭 Intent —— 用以下意圖類型之一,創建一個請求圖像或視頻的Intent,
MediaStore.ACTION_IMAGE_CAPTURE —— 向內置攝像頭程序請求圖像的意圖活動類型。
MediaStore.ACTION_VIDEO_CAPTURE —— 向內置攝像頭程序請求視頻的意圖活動類型。
然后,啟動攝像頭 Intent ——用startActivityForResult()方法執行攝像頭 intent。啟動完畢后攝像頭應用的用戶界面就會顯
示在屏幕上,用戶就可以拍照或攝像了。
最后,接收Intent結果 —— 在應用程序中設置onActivityResult()方法,用於接收從攝像頭 intent返回的數據。當用戶拍攝完
畢后(或者取消操作),系統會調用此方法。
(3)實例
詳細代碼可見文章頂部網站,本文提供了一個簡單的程序:
package cn.edu.zjut.androidcam; import android.os.Bundle; import android.provider.MediaStore; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.view.View; import android.widget.Button; import android.widget.ImageView; /** * * @ClassName: MainActivity * @Description:通過intent調用android內置的攝像應用程序 * @author Alfred.M * @date 2012-9-1 下午2:35:12 * */ public class MainActivity extends Activity { private static final int CAMERA_REQUEST = 1888; private ImageView imageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.imageView = (ImageView) this.findViewById(R.id.imageView1); Button photoButton = (Button) this.findViewById(R.id.button1); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST); photoButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//構造intent startActivityForResult(cameraIntent, CAMERA_REQUEST);//發出intent,並要求返回調用結果 } }); } /** * 接收intent傳回的信息 */ protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST) { //System.exit(0); Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo); } } }
layout為:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" tools:context=".MainActivity" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/app_name" > </ImageView> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/text2" android:layout_below="@+id/imageView1" android:layout_marginRight="14dp" android:layout_marginTop="79dp" android:text="@string/button" /> </RelativeLayout>