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