摄像头拍照
这里面包含两种调用摄像头的方法,一种是自己调用摄像头 ,一种是直接调用系统的摄像头
Activity
1 package cn.itcast.camera; 2 3 import java.io.File; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.graphics.Bitmap; 8 import android.graphics.BitmapFactory; 9 import android.hardware.Camera; 10 import android.hardware.Camera.Parameters; 11 import android.hardware.Camera.PictureCallback; 12 import android.net.Uri; 13 import android.os.Bundle; 14 import android.os.Parcelable; 15 import android.provider.MediaStore; 16 import android.view.View; 17 import android.widget.ImageView; 18 19 public class DemoActivity extends Activity { 20 private ImageView iv; 21 @Override 22 public void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.main); 25 iv = (ImageView) findViewById(R.id.iv); 26 } 27 28 29 public void click(View view){ 30 /* Camera camera = Camera.open(); 31 Parameters params = camera.getParameters(); 32 params.set("picture-size-values", "640x480"); 33 params.set("jpeg-quality","60"); 34 //String mode = params.flatten(); 35 camera.setParameters(params); 36 // System.out.println(mode); 37 // camera.startPreview(); 38 camera.takePicture(null, null, new PictureCallback() { 39 40 @Override 41 public void onPictureTaken(byte[] data, Camera camera) { 42 System.out.println("pic taked"); 43 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 44 iv.setImageBitmap(bitmap); 45 //camera.stopPreview(); 46 } 47 });*/ 48 /* <intent-filter> 49 <action android:name="android.media.action.IMAGE_CAPTURE" /> 50 <category android:name="android.intent.category.DEFAULT" /> 51 </intent-filter>*/ 52 Intent intent = new Intent(); 53 intent.setAction("android.media.action.IMAGE_CAPTURE"); 54 intent.addCategory("android.intent.category.DEFAULT"); 55 File file = new File("/sdcard/bbb.jpg"); 56 intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(file)); 57 startActivityForResult(intent, 0); 58 59 } 60 @Override 61 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 62 super.onActivityResult(requestCode, resultCode, data); 63 if(data!=null){ 64 Bitmap image = data.getParcelableExtra("data"); 65 iv.setImageBitmap(image); 66 } 67 68 } 69 70 71 }
布局文件
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 8 <Button 9 android:id="@+id/button1" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:onClick="click" 13 android:text="拍照" /> 14 15 <ImageView 16 android:id="@+id/iv" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:src="@drawable/ic_launcher" /> 20 </LinearLayout>
AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="cn.itcast.camera" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk android:minSdkVersion="8" /> 8 9 <uses-permission android:name="android.permission.CAMERA" /> 10 11 <uses-feature android:name="android.hardware.camera" /> 12 <uses-feature android:name="android.hardware.camera.autofocus" /> 13 14 <application 15 android:icon="@drawable/ic_launcher" 16 android:label="@string/app_name" > 17 <activity 18 android:name=".DemoActivity" 19 android:label="@string/app_name" > 20 <intent-filter> 21 <action android:name="android.intent.action.MAIN" /> 22 23 <category android:name="android.intent.category.LAUNCHER" /> 24 </intent-filter> 25 </activity> 26 </application> 27 28 </manifest>