Android开发实现ImageView加载摄像头拍摄的大图功能
程序员文章站
2023-01-01 16:14:51
本文实例讲述了android开发实现imageview加载摄像头拍摄的大图功能。分享给大家供大家参考,具体如下:
这个方法是从官方demo中摘录的,在此记录学习。
权限...
本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《android拍照与图片处理技巧总结》、《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。
上一篇: 除夕夜必吃的菜,这些讨喜菜品你会做吗