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

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

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

本文实例讲述了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控件

@OverrIDeprotected 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);}

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

@OverrIDeprotected voID onActivityResult(int requestCode,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加载摄像头拍摄的大图功能全部内容,希望文章能够帮你解决Android开发实现ImageView加载摄像头拍摄的大图功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1143835.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存