Camera frames in a service / background process

后端 未结 2 1617
囚心锁ツ
囚心锁ツ 2020-12-04 02:40

For scientific measurements, I would like to access the camera frames from a service. The idea is to analyze the images coming from the camera in real-time under some condit

相关标签:
2条回答
  • 2020-12-04 03:01

    I use this code my Spy Application for Live camera stream. (This for Xamarin, convert it to Java)

    Firstly add permission:

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

    Put this code into the your Foreground Service. With this code, SurfaceView works independently from Lifecycle of Activity

    public void CamInService()
            {
                var layoutParams = new ViewGroup.LayoutParams(100, 100);
                var view = new SurfaceView(this)
                {
                    LayoutParameters = layoutParams
                };
                view.Holder.AddCallback(new Prev()); //Replace with your preview callback
                WindowManagerLayoutParams winparam = new WindowManagerLayoutParams(WindowManagerTypes.SystemAlert);
                winparam.Flags = WindowManagerFlags.NotTouchModal;
                winparam.Flags |= WindowManagerFlags.NotFocusable;
                winparam.Format = Android.Graphics.Format.Rgba8888;
                winparam.Width = 1;
                winparam.Height = 1;
    
                IWindowManager windowManager = GetSystemService(WindowService).JavaCast<IWindowManager>();
                windowManager.AddView(view, winparam);
            }  
    

    and call this method in OnCreate() method of Service or anywhere in the Application. For access from anywhere declare a variable in your foreground service class like this: public static ForegroundService _globalService; and access from anywhere like this: ForegroundService._globalService.CamInService();

    Note: replace "ForegroundService" word with your Service class name.

    0 讨论(0)
  • 2020-12-04 03:25

    You can send the preview to a SurfaceTexture instead (setPreviewTexture()). This won't disappear when the app is paused. Requires API 11+.

    You can see various applications of this technique in Grafika, which is generally doing video or GLES manipulation rather than stills, but the idea is similar.

    0 讨论(0)
提交回复
热议问题