android自定义Camera拍照并查看图片
程序员文章站
2022-06-04 21:40:57
本文实例为大家分享了android自定义camera拍照并查看图片的具体代码,供大家参考,具体内容如下
1、打开相机
a.预览拍摄图片,需用到surfaceview,...
本文实例为大家分享了android自定义camera拍照并查看图片的具体代码,供大家参考,具体内容如下
1、打开相机
a.预览拍摄图片,需用到surfaceview,并且实现其回调函数implements surfaceholder.callback;
activity_camera.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <surfaceview android:id="@+id/sv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <button android:id="@+id/btn_camera" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="拍照" /> </linearlayout>
2、获取相机并开启预览
/** * 获取系统相机 * * @return */ private camera getccamera() { camera camera = null; try { camera = camera.open(); } catch (exception e) { camera = null; } return camera; }
/** * 显示相机预览图片 * * @return */ private void showcameraview(camera camera,surfaceholder holder) { try { camera.setpreviewdisplay(holder); camera.setdisplayorientation(90); camera.startpreview(); }catch (exception e){ e.printstacktrace(); }; }
/** ** 实现界面回调处理 */ @override public void surfacecreated(surfaceholder holder) { showcameraview(mcamera, holder); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { mcamera.stoppreview(); showcameraview(mcamera, holder); } @override public void surfacedestroyed(surfaceholder holder) { clearcamera(); } }
3、相机主页面处理
public class cameraactivity extends appcompatactivity implements surfaceholder.callback{ private surfaceview sv; private camera mcamera; private surfaceholder holder; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_camera); sv = (surfaceview)findviewbyid(r.id.sv); holder = sv.getholder(); holder.addcallback(this); findviewbyid(r.id.btn_camera).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // 获取当前相机参数 camera.parameters parameters = mcamera.getparameters(); // 设置相片格式 parameters.setpictureformat(imageformat.jpeg); // 设置预览大小 parameters.setpreviewsize(800, 480); // 设置对焦方式,这里设置自动对焦 parameters.setfocusmode(camera.parameters.focus_mode_auto); mcamera.autofocus(new camera.autofocuscallback() { @override public void onautofocus(boolean success, camera camera) { // 判断是否对焦成功 if (success) { // 拍照 第三个参数为拍照回调 mcamera.takepicture(null, null, new camera.picturecallback() { @override public void onpicturetaken(byte[] data, camera camera) { // data为完整数据 file file = new file("/sdcard/photo.png"); // 使用流进行读写 try { fileoutputstream fos = new fileoutputstream(file); try { fos.write(data); // 关闭流 fos.close(); // 查看图片 intent intent = new intent(); // 传递路径 intent.putextra("path", file.getabsolutepath()); setresult(0,intent); finish(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } } }); } } }); } }); }
@override protected void onresume() { super.onresume(); if (mcamera == null) { mcamera = getcamera(); if (holder != null) { showcameraview(mcamera, holder); } } } @override protected void onpause() { // todo auto-generated method stub super.onpause(); // activity暂停时我们释放相机内存 clearcamera(); } @override protected void ondestroy() { super.ondestroy(); } /** * 释放相机的内存 */ private void clearcamera() { // 释放hold资源 if (mcamera != null) { // 停止预览 mcamera.stoppreview(); mcamera.setpreviewcallback(null); // 释放相机资源 mcamera.release(); mcamera = null; } }
4、启动activity(设置拍照完图片路径返回显示图片处理,一定要对图片进行采样率操作(很可能拍照的图片太多而无法显示,又不报任何异常))
public class mainactivity extends appcompatactivity { private imageview cameraiv; @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if(resultcode == 0 && requestcode == 100) { string path = data.getstringextra("path"); bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(path, options); imagesize imagesize = getimageviewsize(cameraiv); options.insamplesize = caculateinsamplesize(options, imagesize.width, imagesize.height); // 使用获得到的insamplesize再次解析图片 options.injustdecodebounds = false; bitmap bitmap = bitmapfactory.decodefile(path, options); cameraiv.setimagebitmap(bitmap); } super.onactivityresult(requestcode, resultcode, data); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button openbutton = (button) findviewbyid(r.id.button); cameraiv = (imageview) findviewbyid(r.id.imageview); openbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(mainactivity.this,cameraactivity.class); startactivityforresult(intent,100); } }); } /* 根据需求的宽和高以及图片实际的宽和高计算samplesize * * @param options * @param width * @param height * @return */ public static int caculateinsamplesize(bitmapfactory.options options, int reqwidth, int reqheight) { int width = options.outwidth; int height = options.outheight; int insamplesize = 1; if (width > reqwidth || height > reqheight) { int widthradio = math.round(width * 1.0f / reqwidth); int heightradio = math.round(height * 1.0f / reqheight); insamplesize = math.max(widthradio, heightradio); } return insamplesize; } public static imagesize getimageviewsize(imageview imageview) { imagesize imagesize = new imagesize(); displaymetrics displaymetrics = imageview.getcontext().getresources() .getdisplaymetrics(); linearlayout.layoutparams lp = (linearlayout.layoutparams)imageview.getlayoutparams(); int width = imageview.getwidth();// 获取imageview的实际宽度 if (width <= 0) { width = lp.width;// 获取imageview在layout中声明的宽度 } if (width <= 0) { width = displaymetrics.widthpixels; } int height = imageview.getheight();// 获取imageview的实际高度 if (height <= 0) { height = lp.height;// 获取imageview在layout中声明的宽度 } if (height <= 0) { height = displaymetrics.heightpixels; } imagesize.width = width; imagesize.height = height; return imagesize; } public static class imagesize { int width; int height; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。