android开发分享Android实现极简打开摄像头

很多时候忘记android摄像头如何打开,查看google文档的话,发现太复杂(只是单纯的想打开摄像头而已,不想添加那么多设置,添加那么功能),很多博客也是对官方文档的小修小改,连方法名都一样,因此,

很多时候忘记android摄像头如何打开,查看google文档的话,发现太复杂(只是单纯的想打开摄像头而已,不想添加那么多设置,添加那么功能),很多博客也是对官方文档的小修小改,连方法名都一样,因此,我决定完成android相机最简单的打开摄像头(仅仅打开)。很久没用忘掉的话,打开链接复制粘贴一下就完事了。

上述就是android开发分享Android实现极简打开摄像头的全部内容,如果对大家有所用处且需要了解更多关于Android学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

androidmanifest.xml设置camera权限后,在代码中还要设置权限检查,但是因为我连权限检查都懒得加了,装好后直接在手机系统里手动允许权限。

camera1(已废弃):

xml中使用surfaceview作为预览view

<?xml version="1.0" encoding="utf-8"?>  <androidx.constraintlayout.widget.constraintlayout      xmlns:android="https://schemas.android.com/apk/res/android"      xmlns:app="https://schemas.android.com/apk/res-auto"      xmlns:tools="https://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      tools:context=".mainactivity">        <surfaceview          android:id="@+id/surfaceview"          android:layout_width="0dp"          android:layout_height="0dp"          app:layout_constraintbottom_tobottomof="parent"          app:layout_constraintleft_toleftof="parent"          app:layout_constraintright_torightof="parent"          app:layout_constrainttop_totopof="parent" />    </androidx.constraintlayout.widget.constraintlayout>

mainactivity:

public class mainactivity extends appcompatactivity implements surfaceholder.callback{        private surfaceholder holder;      private camera camera;        @override      protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);          requestwindowfeature(window.feature_no_title);          getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,                  windowmanager.layoutparams.flag_fullscreen);          setcontentview(r.layout.activity_main);          surfaceview surfaceview = findviewbyid(r.id.surfaceview);          holder = surfaceview.getholder();          holder.addcallback(this);      }        @override      public void surfacecreated(surfaceholder holder) {          if(camera == null){              try {                  camera = camera.open(camera.camerainfo.camera_facing_back);                  camera.setpreviewdisplay(holder);                  camera.startpreview();                  camera.parameters parameters = camera.getparameters();                  parameters.setfocusmode(camera.parameters.focus_mode_auto);                  parameters.setflashmode(camera.parameters.flash_mode_auto);                  camera.setparameters(parameters);                  camera.setdisplayorientation(90);              }catch (exception e){                  e.printstacktrace();              }          }      }  }

这样就完成了最简单的打开摄像头并在手机中出现画面。(代码里去掉2个接口中未实现的方法)

camera2

android 5.0(api 21)以后,谷歌就决定废弃原有的camera api改用camera2 api,因为功能更强大

Android实现极简打开摄像头

xml使用textureview作为预览(其实surfaceview也行,官方的demo是用textureview的一个子类):

<?xml version="1.0" encoding="utf-8"?>  <androidx.constraintlayout.widget.constraintlayout      xmlns:android="https://schemas.android.com/apk/res/android"      xmlns:app="https://schemas.android.com/apk/res-auto"      xmlns:tools="https://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      tools:context=".mainactivity">        <textureview          android:id="@+id/surfaceview"          android:layout_width="0dp"          android:layout_height="0dp"          app:layout_constraintbottom_tobottomof="parent"          app:layout_constraintleft_toleftof="parent"          app:layout_constraintright_torightof="parent"          app:layout_constrainttop_totopof="parent" />    </androidx.constraintlayout.widget.constraintlayout>

mainactivity:

public class mainactivity extends appcompatactivity implements textureview.surfacetexturelistener {        private textureview textureview;        private capturerequest.builder builder;        @override      protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);          setcontentview(r.layout.activity_main);          textureview = findviewbyid(r.id.surfaceview);          textureview.setsurfacetexturelistener(this);      }        @override      public void onsurfacetextureavailable(final surfacetexture surface, int width, int height) {          cameramanager manager = (cameramanager) getsystemservice(camera_service);          if (activitycompat.checkselfpermission(mainactivity.this, manifest.permission.camera)                  != packagemanager.permission_granted) {              return;          }          try {              manager.opencamera("0", new cameradevice.statecallback() {                  @override                  public void onopened(@nonnull cameradevice camera) {                      try {                      surfacetexture surfacetexture = textureview.getsurfacetexture();                      surfacetexture.setdefaultbuffersize(1440,1080);                      surface surface = new surface(surfacetexture);                      builder = camera.createcapturerequest(cameradevice.template_preview);                      builder.addtarget(surface);                        camera.createcapturesession(arrays.aslist(surface),                              new cameracapturesession.statecallback() {                          @override                          public void onconfigured(@nonnull cameracapturesession session) {                              try {                                  session.setrepeatingrequest(builder.build(), null, null);                              } catch (cameraaccessexception e) {                                  e.printstacktrace();                              }                          }                            @override                          public void onconfigurefailed(@nonnull cameracapturesession session) {                            }                      }, null);                      } catch (cameraaccessexception e) {                          e.printstacktrace();                      }                  }                    @override                  public void ondisconnected(@nonnull cameradevice camera) {                    }                    @override                  public void onerror(@nonnull cameradevice camera, int error) {                    }              }, null);          } catch (cameraaccessexception e) {              e.printstacktrace();          }      }  }

这样就成功使用camera2的api打开并预览了(代码里去掉3个接口中未实现的方法)

以上就是android开发分享Android实现极简打开摄像头的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/1069409.html

(0)
上一篇 2022年3月29日
下一篇 2022年3月29日

精彩推荐