如何使用Camera2 API在Android中同时使用前后摄像头预览

本文档介绍如何使用Android的Camera2 API实现在同一设备上同时从前后摄像头获取视频预览。作者分享了遇到挑战并解决的过程,并提供了一个示例应用程序,以帮助开发者更好地理解实现细节。如果对Android相机不熟悉,建议先查看android/camera-samples存储库作为入门。
摘要由CSDN通过智能技术生成

Not so long ago, I was given the task of getting simultaneous feeds from the front and rear cameras from an Android-running device. As usual, I headed to Stack Overflow, then to GitHub, then to other blogs, only to realise that I might be on my own on this one. Tough feeling, right?

不久前,我承担了从运行Android的设备的前后摄像头获取同步提要的任务。 像往常一样,我去了Stack Overflow,然后去了GitHub,然后去了其他博客,才意识到我可能独自一人。 难过的感觉吧?

After being able to solve the problem, I took some time out to help people who might find themselves stuck in the same situation.

在能够解决问题之后,我花了一些时间来帮助可能会陷入同样困境的人们。

I have made a sample application for this tutorial. All the code snippets shared in this tutorial are from the application itself. If you don’t understand a snippet at any point, you can refer to the whole consolidated file. You can find the sample application for this tutorial on GitHub:

我已经为本教程制作了一个示例应用程序。 本教程中共享的所有代码段均来自应用程序本身。 如果您在任何时候都不了解代码段,则可以引用整个合并文件。 您可以在GitHub上找到本教程的示例应用程序:

If you are completely new to Android cameras, the android/camera-samples repository will serve as a good starting point.

如果您是Android相机的新手,则android / camera-samples存储库将是一个很好的起点。

Note: Java implementations have been removed from android/camera-samples. For Java implementations, you can refer to this forked repository.

注意:Java实现已从android / camera-samples中删除。 对于Java实现,您可以参考此分叉存储库

For this tutorial, it is assumed that you are capable of implementing a camera feed on your own. If that is not the case, please visit the repositories mentioned above. The tutorial will make a lot more sense. Otherwise, you might find yourself lost.

在本教程中,假定您能够自己实现相机供稿。 如果不是这种情况,请访问上述存储库。 本教程将更加有意义。 否则,您可能会发现自己迷路了。

设置预览视图 (Set Up Views for Preview)

We will require two separate views to present the preview from two cameras. We will start small by creating views to show the previews:

我们将需要两个单独的视图来展示两个摄像机的预览。 我们将首先创建视图以显示预览:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <!--This is a custom TextureView, generic TextureView can be used as well-->
    <com.ananth.frontrearcamera.view.AutoFitTextureView
        android:layout_weight="1"
        android:id="@+id/texture1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <com.ananth.frontrearcamera.view.AutoFitTextureView
        android:layout_weight="1"
        android:id="@+id/texture2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>

Now, before opening our cameras, we need to make sure both of their TextureViews are avai

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值