Xamarin-Droid(SurfaceView)

Xamarin - Droid (SurfaceView)

我有一个人脸检测的应用程序,因此我需要在应用程序启动时打开相机,我已经调查了类似的问题,但我找不到解决我目前遇到的问题的方法。问题是函数 SurfaceCreated(ISurfaceHolder holder) 没有被调用,因此相机没有被启动。感谢回复

CameraLayout.axml

<AppName.Droid.CameraControls.CameraLiveStream
      android:id="@+id/cameraPreview"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >
</App2.Droid.CameraControls.CameraLiveStream>

CameraLiveStream.cs继承ViewGroup,ISurfaceHolderCallback2

CameraLiveStream.cs

    private Context mContext;
    private SurfaceView mSurfaceView;
    private bool mStartRequested;
    private bool mSurfaceAvailable;
    private CameraSource mCameraSource;

    public CameraLiveStream(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        mContext = context;
        mStartRequested = false;
        mSurfaceAvailable = false;

        mSurfaceView = new SurfaceView(context);
        mSurfaceView.Holder.AddCallback(this);

        AddView(mSurfaceView);
    }
    #region camera-surface
    public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
    {
        throw new NotImplementedException();
    }

    public void SurfaceCreated(ISurfaceHolder holder)
    {
        mSurfaceAvailable = true;

        try
        {
            StartCameraIfReady();
        }
        catch
        {
            Log.Error("CameraLiveStream", "Error when starting camera");
        }
    }

这是我的 MainActivity.cs OnCreate()

    protected override void OnCreate(Bundle bundle)
    {

        base.OnCreate(bundle);

        SetContentView(Resource.Layout.CameraLayout);

        mPreview = FindViewById<CameraLiveStream>(Resource.Id.cameraPreview);

        if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.Camera) == Permission.Granted)
        {
            CreateCameraSource();
        }
        else
        {
            RequestCameraPermissions();
        }

    }

CreateCameraSource函数

private void CreateCameraSource()
    {
        var context = Application.Context;
        FaceDetector detector = new FaceDetector.Builder(context)
                .SetClassificationType(ClassificationType.All)
                .Build();

        if (!detector.IsOperational)
        {
            Log.Warn("MainActivity", "Face detector dependencies are not yet available.");
        }

        mCameraSource = new CameraSource.Builder(context, detector)
                .SetRequestedPreviewSize(200, 200)
                                        .SetFacing(CameraFacing.Back)
                .SetRequestedFps(30.0f)
                .Build();

    }

StartCameraSource函数

private void StartCameraSource()
    {
        int code = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(
                this.ApplicationContext);
        if (code != ConnectionResult.Success)
        {
            Dialog dlg =
                    GoogleApiAvailability.Instance.GetErrorDialog(this, code, RC_HANDLE_GMS);
            dlg.Show();
        }

        if (mCameraSource != null)
        {
            try
            {
                mPreview.StartCamera(mCameraSource);
            }
            catch (System.Exception e)
            {
                Log.Error("MainActivity-StartCameraSource", "Unable to start camera source.", e);
                mCameraSource.Release();
                mCameraSource = null;
            }

您正在创建一个没有布局参数的 SurfaceView,因此它的大小为 0,0,因此永远不需要创建表面。

全屏:

var layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
var view = new SurfaceView(someContext)
{
    LayoutParameters = layoutParams
};

或者在布局

中将 SurfaceView 父级设置为 容器
var aViewGroup = FindViewById<ViewGroup>(Resource.Id.SomeContainer);
aViewGroup.AddView(yourSurfaceView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));

解决了问题:

我在我的下方创建了另一种类型的视图 AppName.Droid.CameraControls.CameraLiveStream

于是就变成了这样

<AppName.Droid.Camera.Controls.CameraLiveStream>
      <AppName.Droid.Camera.Controls.Overlay>
      </AppName.Droid.Camera.Controls.Overlay>
</AppName.Droid.Camera.Controls.CameraLiveStream>

其中我的overlay继承了实现了view的width/height的View。

public int mPrevWidth { get => mWidth; set => mWidth = value; }
    public int mPrevHeight { get => mHeight; set => mHeight = value; }
    public float mPrevWidthScaleFactor { get => mWidthScaleFactor; set => mWidthScaleFactor = value; }
    public float mPrevHeightScaleFactor { get => mHeightScaleFactor; set => mHeightScaleFactor = value; }
    public CameraFacing mCameraFacing { get => mFacing; set => mFacing = value; }