在App中使用相机的两种方式: java
一、调用系统相机、或者是具备相机功能的应用 android
二、自定义相机 code
调用系统相机的方式: 图片
打开相机 get
//默认的返回图片,返回的只是缩略图 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQ_CAMERA);
返回路径: it
if(resultCode != RESULT_OK){ return; } if(requestCode == REQ_CAMERA){ if(data != null){ Bundle bundle = data.getExtras(); //Bundle里返回的是缩略图 Bitmap bitmap = (Bitmap)bundle.get("data"); mImageView.setImageBitmap(bitmap); } }
//指定图片返回路径 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Uri uri = Uri.fromFile(new File(mFilePath)); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(intent, REQ_CAMERA_ORIGINAL_IMAGE);
else if(requestCode == REQ_CAMERA_ORIGINAL_IMAGE){ //获取原图 Bitmap bitmap = BitmapFactory.decodeFile(mFilePath); mImageView.setImageBitmap(bitmap); }
三、注册Camera功能: io
新建一个module,在Activity里添加以下注册代码: class
<activity android:name=".MyCameraActivity"> <intent-filter> <action android:name="android.media.action.IMAGE_CAPTURE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
即可以开启自定义相机的第一步了。 module