Android开发实现ImageView加载摄像头拍摄的大图功能

本文实例讲述了Android开发实现ImageView加载摄像头拍摄的大图功能。分享给大家供大家参考,具体如下:

目前创新互联公司已为成百上千家的企业提供了网站建设、域名、网络空间、网站托管、企业网站设计、达孜网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

这个方法是从官方demo中摘录的,在此记录学习。

权限




另:关于权限控制还可参考:Android Manifest功能与权限描述大全

设置变量保存文件存储路径

private String mCurrentPhotoPath;
/**
* 拍照flag
*/
private static final int REQUEST_IMAGE_CAPTURE_O = 2;

创建存储路径及文件名

 /**
* 创建拍摄的图片的存储路径及文件名
* @return
* @throws IOException
*/
private File createImageFile() throws IOException{
  String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  String imageFileName = "JPEG_" + timeStamp + "_";
  File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  Log.d("TrainingFirstActivity", "storageDir:" + storageDir);
  File image = File.createTempFile(imageFileName, ".jpg", storageDir);
  mCurrentPhotoPath = image.getAbsolutePath();
  Log.d("image.getAbsolutePath()", image.getAbsolutePath() + "");
  return image;
}

拍摄图片并保存

Intent takePictureOintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureOintent.resolveActivity(getPackageManager()) != null){
 File photoFile = null;
 try {
  photoFile = createImageFile();
 } catch (IOException e) {
  e.printStackTrace();
 }
 if (photoFile != null){
  takePictureOintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
  startActivityForResult(takePictureOintent, REQUEST_IMAGE_CAPTURE_O);
 }
}

处理并压缩拍照结果,takePhotoThenToShowImg是一个ImageView控件

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == REQUEST_IMAGE_CAPTURE_O && resultCode == RESULT_OK){
   int targetW = takePhotoThenToShowImg.getWidth();
   int targetH = takePhotoThenToShowImg.getHeight();
  /* Get the size of the image */
   BitmapFactory.Options bmOptions = new BitmapFactory.Options();
   bmOptions.inJustDecodeBounds = true;
   BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
   int photoW = bmOptions.outWidth;
   int photoH = bmOptions.outHeight;
  /* Figure out which way needs to be reduced less */
   int scaleFactor = 1;
   if ((targetW > 0) || (targetH > 0)) {
    scaleFactor = Math.min(photoW/targetW, photoH/targetH);
   }
  /* Set bitmap options to scale the image decode target */
   bmOptions.inJustDecodeBounds = false;
   bmOptions.inSampleSize = scaleFactor;
   bmOptions.inPurgeable = true;
  /* Decode the JPEG file into a Bitmap */
   Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
  /* Associate the Bitmap to the ImageView */
   takePhotoThenToShowImg.setImageBitmap(bitmap);
   galleryAddPic();
  }
}

最后可以将拍摄到的照片添加到Media Provider的数据库中,以便图库或者其他程序读取照片

/**
* 将拍摄到的照片添加到Media Provider的数据库中
*/
private void galleryAddPic(){
  Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  File f = new File(mCurrentPhotoPath);
  Uri contentUri = Uri.fromFile(f);
  mediaScanIntent.setData(contentUri);
  this.sendBroadcast(mediaScanIntent);
}

如果只需要缩略图的话,只要调摄像头拍摄直接处理结果就行

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){//展示图片
   Bundle extras = data.getExtras();
   Bitmap imageBitmap = (Bitmap) extras.get("data");
   takePhotoThenToShowImg.setImageBitmap(imageBitmap);
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android拍照与图片处理技巧总结》、《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。


当前名称:Android开发实现ImageView加载摄像头拍摄的大图功能
网页URL:http://azwzsj.com/article/pjgseo.html