首页 > 代码库 > 19、照相机技术
19、照相机技术
照相机有哪些功能
Android SDK支持操作Android设备内置的照相机。从Android2.3开始,支持操作多个摄像头(主要指前置摄像头和后置照相机)。通过照相机可以拍照和录像。
编写拍照程序需要考虑哪些方面
是否支持照相机
快速拍照和定制拍照
存储
照相机涉及到的主要API
Camera
SurfaceView、
MediaRecorder
Intent
拍照和摄像程序可能涉及到的权限和特性
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature android:name="android.hardware.camera" />
DEMO1
拍照Demo
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/btnTakePicture" 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:text="拍照" />12 13 <ImageView14 android:id="@+id/imageview"15 android:layout_width="320dp"16 android:layout_height="240dp" />17 18 </LinearLayout>
1 import android.app.Activity; 2 import android.content.Intent; 3 import android.graphics.Bitmap; 4 import android.os.Bundle; 5 import android.provider.MediaStore; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 import android.widget.ImageView;10 11 public class Main extends Activity implements OnClickListener {12 private ImageView imageView;13 14 @Override15 public void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17 setContentView(R.layout.main);18 19 Button btnTakePicture = (Button) findViewById(R.id.btnTakePicture);20 btnTakePicture.setOnClickListener(this);21 22 imageView = (ImageView) findViewById(R.id.imageview);23 }24 25 public void onClick(View view) {26 // 调用系统拍照27 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);28 // 显示拍照窗口29 startActivityForResult(intent, 1);30 }31 32 protected void onActivityResult(int requestCode, int resultCode, Intent data) {33 if (requestCode == 1) {34 if (resultCode == Activity.RESULT_OK) {35 Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");36 imageView.setImageBitmap(cameraBitmap);37 }38 }39 }40 41 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="cn.eoe.system.camera" android:versionCode="1" 4 android:versionName="1.0"> 5 <application android:icon="@drawable/icon" android:label="@string/app_name"> 6 <activity android:name=".Main" android:label="@string/app_name"> 7 <intent-filter> 8 <action android:name="android.intent.action.MAIN" /> 9 <category android:name="android.intent.category.LAUNCHER" />10 </intent-filter>11 </activity> 12 </application>13 <uses-sdk android:minSdkVersion="7" />14 </manifest>
录像Demo
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/btnTakePicture" 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:text="录像" />12 13 <VideoView14 android:id="@+id/videoview"15 android:layout_width="wrap_content"16 android:layout_height="wrap_content" />17 18 </LinearLayout>
1 import android.app.Activity; 2 import android.content.Intent; 3 import android.database.Cursor; 4 import android.net.Uri; 5 import android.os.Bundle; 6 import android.provider.MediaStore; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button;10 import android.widget.MediaController;11 import android.widget.VideoView;12 13 public class Main extends Activity implements OnClickListener {14 public VideoView videoView;15 16 @Override17 public void onCreate(Bundle savedInstanceState) {18 super.onCreate(savedInstanceState);19 setContentView(R.layout.main);20 Button btnTakePicture = (Button) findViewById(R.id.btnTakePicture);21 btnTakePicture.setOnClickListener(this);22 23 videoView = (VideoView) findViewById(R.id.videoview);24 25 }26 27 public void onClick(View view) {28 Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);29 startActivityForResult(intent, 1);30 }31 32 protected void onActivityResult(int requestCode, int resultCode, Intent data) {33 if (requestCode == 1) {34 if (resultCode == Activity.RESULT_OK) {35 Uri uri = data.getData();36 Cursor cursor = this.getContentResolver().query(uri, null,37 null, null, null);38 39 if (cursor.moveToFirst()) {40 // 取出路径41 String videoPath = cursor.getString(cursor42 .getColumnIndex("_data"));43 // 加载44 videoView.setVideoURI(Uri.parse(videoPath));45 // 设置视频控制控件(停止,快进等)。46 videoView.setMediaController(new MediaController(this));47 // 播放。48 videoView.start();49 }50 }51 }52 }53 54 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="cn.eoe.record.video" android:versionCode="1" 4 android:versionName="1.0"> 5 <application android:icon="@drawable/icon" android:label="@string/app_name"> 6 <activity android:name="Main" android:label="@string/app_name"> 7 <intent-filter> 8 <action android:name="android.intent.action.MAIN" /> 9 <category android:name="android.intent.category.LAUNCHER" />10 </intent-filter>11 </activity> 12 </application>13 <uses-sdk android:minSdkVersion="7" />14 </manifest>
定制拍照程序的步骤
打开照相机:Camera.open
创建SurfaceView对象
添加回调事件监听器(SurfaceHolder.addCallback)
预览(Camera.startPreview)
拍照(Camera.takePicture)
检测Android设备是否支持照相机
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
{ return true; } else { return false; }
}
DEMO2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。