Android调用前后摄像头同时工作实例代码

Android调用前后摄像头同时工作实例代码,第1张

概述硬件环境:小米4Android版本: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;@OverrIDeprotected 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());}@OverrIDeprotected 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 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,"图像出错");    }  }}}

可能出现的问题:

1、没有权限,需要手动打开,代码里由于追求简便,没有主动申请,只在mainfest中注册了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android调用前后摄像头同时工作实例代码全部内容,希望文章能够帮你解决Android调用前后摄像头同时工作实例代码所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1145719.html

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

发表评论

登录后才能评论

评论列表(0条)

保存