I have an app for face detection, thus I need to open the camera when the app is launched, I have looked into the similar problems yet I can't find a solution to the problem I currently have now. The problem is that the function SurfaceCreated(ISurfaceHolder holder) is not being called, thus, the camera is not being launched. Thanks for the response
CameraLayout.axml
<AppName.Droid.CameraControls.CameraLiveStream
android:id="@+id/cameraPreview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</App2.Droid.CameraControls.CameraLiveStream>
The CameraLiveStream.cs inherits 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");
}
}
Here's my 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
Function
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
Function
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;
}
Fixed the issue:
I have created another type of view underneath my AppName.Droid.CameraControls.CameraLiveStream
So it became like this
<AppName.Droid.Camera.Controls.CameraLiveStream>
<AppName.Droid.Camera.Controls.Overlay>
</AppName.Droid.Camera.Controls.Overlay>
</AppName.Droid.Camera.Controls.CameraLiveStream>
Wherein I the overlay inherited the View which implements the width/height of the 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; }