Android调用前后摄像头同时工作实例代码
程序员文章站
2022-05-17 22:37:54
硬件环境:小米4
android版本:6.0
咱们先看效果图:
我把代码贴出来:
androidmainfest.xml文件(需要新增camera权限)...
硬件环境:小米4
android版本:6.0
咱们先看效果图:
我把代码贴出来:
androidmainfest.xml文件(需要新增camera权限):
<uses-permission android:name="android.permission.camera" />
activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bihu.qdemopush.mainactivity"> <linearlayout android:layout_width="match_parent" android:layout_height="200dp" android:orientation="horizontal"> <surfaceview android:id="@+id/surfaceview1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent" /> <surfaceview android:id="@+id/surfaceview2" android:layout_width="0dp" android:layout_weight="1" android:layout_marginleft="5dp" android:layout_height="fill_parent" /> </linearlayout> </linearlayout>
mainactivity.java文件:主要是对2个surfaceview的显示渲染
public class mainactivity extends appcompatactivity { surfaceview surfaceview1, surfaceview2; surfaceholder surfaceholder1, surfaceholder2; string tag = "mainactivity"; private camera camera1 = null, camera2; camera.parameters parameters; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); surfaceview1 = (surfaceview) findviewbyid(r.id.surfaceview1); surfaceview2 = (surfaceview) findviewbyid(r.id.surfaceview2); surfaceholder1 = surfaceview1.getholder(); surfaceholder1.settype(surfaceholder.surface_type_push_buffers); surfaceholder1.addcallback(new surfaceholdercallbackback()); surfaceholder2 = surfaceview2.getholder(); surfaceholder2.settype(surfaceholder.surface_type_push_buffers); surfaceholder2.addcallback(new surfaceholdercallbackfont()); } @override protected void ondestroy() { log.e(tag, "ondestroy"); super.ondestroy(); } /** * 后置摄像头回调 */ class surfaceholdercallbackback implements surfaceholder.callback { @override public void surfacecreated(surfaceholder holder) { // 获取camera对象 int cameracount = camera.getnumberofcameras(); if (cameracount > 0) { camera1 = camera.open(0); try { // 设置预览监听 camera1.setpreviewdisplay(holder); camera.parameters parameters = camera1.getparameters(); if (mainactivity.this.getresources().getconfiguration().orientation != configuration.orientation_landscape) { parameters.set("orientation", "portrait"); camera1.setdisplayorientation(90); parameters.setrotation(90); } else { parameters.set("orientation", "landscape"); camera1.setdisplayorientation(0); parameters.setrotation(0); } camera1.setparameters(parameters); // 启动摄像头预览 camera1.startpreview(); system.out.println("camera.startpreview"); } catch (ioexception e) { e.printstacktrace(); camera1.release(); system.out.println("camera.release"); } } } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { camera1.autofocus(new camera.autofocuscallback() { @override public void onautofocus(boolean success, camera camera) { if (success) { initcamera();// 实现相机的参数初始化 camera.cancelautofocus();// 只有加上了这一句,才会自动对焦。 } } }); } @override public void surfacedestroyed(surfaceholder holder) { } // 相机参数的初始化设置 private void initcamera() { parameters = camera1.getparameters(); parameters.setpictureformat(pixelformat.jpeg); parameters.setfocusmode(camera.parameters.focus_mode_continuous_picture);// 1连续对焦 setdispaly(parameters, camera1); camera1.setparameters(parameters); camera1.startpreview(); camera1.cancelautofocus();// 2如果要实现连续的自动对焦,这一句必须加上 } // 控制图像的正确显示方向 private void setdispaly(camera.parameters parameters, camera camera) { if (integer.parseint(build.version.sdk) >= 8) { setdisplayorientation(camera, 90); } else { parameters.setrotation(90); } } // 实现的图像的正确显示 private void setdisplayorientation(camera camera, int i) { method downpolymorphic; try { downpolymorphic = camera.getclass().getmethod("setdisplayorientation", new class[]{int.class}); if (downpolymorphic != null) { downpolymorphic.invoke(camera, new object[]{i}); } } catch (exception e) { log.e("came_e", "图像出错"); } } } class surfaceholdercallbackfont implements surfaceholder.callback { @override public void surfacecreated(surfaceholder holder) { // 获取camera对象 int cameracount = camera.getnumberofcameras(); if (cameracount == 2) { camera2 = camera.open(1); } try { // 设置预览监听 camera2.setpreviewdisplay(holder); camera.parameters parameters = camera2.getparameters(); if (mainactivity.this.getresources().getconfiguration().orientation != configuration.orientation_landscape) { parameters.set("orientation", "portrait"); camera2.setdisplayorientation(90); parameters.setrotation(90); } else { parameters.set("orientation", "landscape"); camera2.setdisplayorientation(0); parameters.setrotation(0); } camera2.setparameters(parameters); // 启动摄像头预览 camera2.startpreview(); system.out.println("camera.startpreview"); } catch (ioexception e) { e.printstacktrace(); camera2.release(); system.out.println("camera.release"); } } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { camera2.autofocus(new camera.autofocuscallback() { @override public void onautofocus(boolean success, camera camera) { if (success) { parameters = camera2.getparameters(); parameters.setpictureformat(pixelformat.jpeg); parameters.setfocusmode(camera.parameters.focus_mode_continuous_picture);// 1连续对焦 setdispaly(parameters, camera2); camera2.setparameters(parameters); camera2.startpreview(); camera2.cancelautofocus();// 2如果要实现连续的自动对焦,这一句必须加上 camera.cancelautofocus();// 只有加上了这一句,才会自动对焦。 } } }); } @override public void surfacedestroyed(surfaceholder holder) { } // 控制图像的正确显示方向 private void setdispaly(camera.parameters parameters, camera camera) { if (integer.parseint(build.version.sdk) >= 8) { setdisplayorientation(camera, 90); } else { parameters.setrotation(90); } } // 实现的图像的正确显示 private void setdisplayorientation(camera camera, int i) { method downpolymorphic; try { downpolymorphic = camera.getclass().getmethod("setdisplayorientation", new class[]{int.class}); if (downpolymorphic != null) { downpolymorphic.invoke(camera, new object[]{i}); } } catch (exception e) { log.e("came_e", "图像出错"); } } } }
可能出现的问题:
1、没有权限,需要手动打开,代码里由于追求简便,没有主动申请,只在mainfest中注册了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。