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

作者:AAA啊哈 时间:2023-08-06 19:22:33 

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

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

权限


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
 android:name="android.hardware.camera2"
 android:required="false" />

另:关于权限控制还可参考: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程序设计有所帮助。

来源:http://blog.csdn.net/yang786654260/article/details/49026903

标签:Android,ImageView
0
投稿

猜你喜欢

  • Android UI系列-----ScrollView和HorizontalScrollView的详解

    2022-04-06 14:14:08
  • Springboot2以代码的方式统一配置Jackson教程

    2021-08-30 16:47:33
  • 详解java中float与double的区别

    2021-09-12 11:04:47
  • Spring5学习之基础知识总结

    2021-08-16 02:19:15
  • C#导出Excel的示例详解

    2021-12-03 01:55:51
  • 解析HikariCP一百行代码轻松掌握多线程

    2023-03-21 19:11:00
  • 详解lambda表达式foreach性能分析

    2023-10-28 17:55:21
  • C#串口通讯概念及简单的实现方法

    2021-06-25 13:49:24
  • java isInterrupted()判断线程的实例讲解

    2023-07-21 01:45:53
  • C#中foreach语句使用break暂停遍历的方法

    2022-10-12 20:14:11
  • Android实现的简单蓝牙程序示例

    2021-06-27 01:23:44
  • C#中Write()和WriteLine()的区别分析

    2023-11-04 21:04:23
  • 自定义注解和springAOP捕获Service层异常,并处理自定义异常操作

    2023-04-04 05:26:04
  • RocketMQ消息存储文件的加载与恢复机制源码分析

    2021-12-29 20:23:19
  • Windows同时安装两个版本JDK并实现动态切换JAVA8或JAVA11的方法

    2022-05-20 05:38:41
  • Java redisson实现分布式锁原理详解

    2022-02-18 08:34:10
  • 深入理解Java设计模式之抽象工厂模式

    2023-11-28 12:11:34
  • Android Dialog 动画实例详解

    2022-10-22 22:24:29
  • 浅谈redis key值内存消耗以及性能影响

    2022-11-09 20:35:17
  • OpenCV中C++函数imread读取图片的问题及解决方法

    2023-12-02 11:25:50
  • asp之家 软件编程 m.aspxhome.com