首页 > 解决方案 > 相机预览在后台 Android 上运行

问题描述

我有一个使用 Java 中的 Firebase ML kit Detector 检测眼睛的应用程序,但我希望相机预览在后台运行并检测眼睛的位置而不显示在我的屏幕上。

xml文件在下面提到。我已将可见性更改为不可见或消失,但没有成功。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:visibility="visible">



        <com.example.eyetrackerdemo.CameraSourcePreview
            android:id="@+id/preview"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.example.eyetrackerdemo.GraphicOverlay
                android:id="@+id/barcodeOverlay"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </com.example.eyetrackerdemo.CameraSourcePreview>
    </LinearLayout>

你知道我应该遵循什么实现。此外,我想录制在后台运行的视频并将其保存在设备中。

标签: androidfirebaseandroid-layoutbackground-service

解决方案


一种可能的方法是在布局中使用相机视图,但不在可见屏幕中使用。喜欢,

<LinearLayout
      1...
      
      2...
         android:layout_height="match_parent"
 
      3.<org.opencv.android.JavaCameraView
          android:id="@+id/myCameraView"
          android:layout_width="match_parent"
          android:layout_height="200dp"
          app:show_fps="false" />

 </LinearLayout>

注意第二个小部件中的android:layout_height="match_parent"。这使第二个小部件能够覆盖剩余屏幕,并且不会为相机视图提供任何空间。


推荐阅读