标签:
很多应用程序都可能会使用到调用摄像头拍照和从相册选取图片的功能,今天我们开始android中摄像头与相册调用的学习。
目录导航
大致流程:调用相机拍照,然后剪辑,之后再在imageView中显示。项目结构如下:
一、 一些初始化的代码:
public static final int TAKE_PHOTO = 1; public static final int CROP_PHOTO = 2; public static final int CHOOSE_PHOTO = 3; private ImageView picture; private Uri imageUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); picture = (ImageView) findViewById(R.id.picture); }
二、 创建文件:
// 创建File对象,用于存储拍照后的图片 private File createImageFile(String fileName) { File outputImage = new File(Environment.getExternalStorageDirectory(), fileName); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } return outputImage; }
三、 调用系统相机拍照,
// 调用系统相机拍照,并创建一张jpg文件 public void takePhone(View view) { imageUri = Uri.fromFile(createImageFile("takephone.jpg")); Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, TAKE_PHOTO); // 启动相机程序 }
四、 onActivityResult方法:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case TAKE_PHOTO: phoneTake(resultCode); break; case CROP_PHOTO: phoneCrop(resultCode); break; case CHOOSE_PHOTO: phoneChoose(resultCode, data); break; default: break; } }
五、 phoneTake方法:
// 拍照的回调 private void phoneTake(int resultCode) { if (resultCode == RESULT_OK) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(imageUri, "image/*"); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CROP_PHOTO); // 启动裁剪程序 } }
六、 phoneCrop方法,裁剪的回调
// 裁剪的回调 private void phoneCrop(int resultCode) { if (resultCode == RESULT_OK) { try { BufferedInputStream inputStream = new BufferedInputStream(getContentResolver().openInputStream(imageUri)); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); picture.setImageBitmap(bitmap); // 将裁剪后的照片显示出来 } catch (FileNotFoundException e) { e.printStackTrace(); } } }
七、 声明权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
一、 从相册中选择照片:
// 从相册中选择照片: public void choosePhone(View view) { imageUri = Uri.fromFile(createImageFile("choosephone.jpg")); Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); intent.putExtra("crop", true); intent.putExtra("scale", true); startActivityForResult(intent, CHOOSE_PHOTO); }
二、 从相册中选取图片,再显示
// 从相册中选取图片,再显示 private void phoneChoose(int resultCode, Intent data) { if (resultCode == RESULT_OK) { ContentResolver resolver = getContentResolver(); Uri originalUri = data.getData(); try { Bitmap photo = MediaStore.Images.Media.getBitmap(resolver, originalUri); if (photo != null) { picture.setImageBitmap(photo); } } catch (IOException e) { e.printStackTrace(); } } }
标签:
原文地址:http://www.cnblogs.com/huhx/p/BaseCamera.html