我有一个面部检测应用程序,因此我需要在应用程序启动时打开相机,我已经查看了类似的问题但我找不到解决我现在遇到的问题。问题是没有调用函数 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;
}
答案 0 :(得分:0)
您正在创建一个没有布局参数的SurfaceView,因此它的大小为0,0因此永远不需要创建表面。
var layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
var view = new SurfaceView(someContext)
{
LayoutParameters = layoutParams
};
var aViewGroup = FindViewById<ViewGroup>(Resource.Id.SomeContainer);
aViewGroup.AddView(yourSurfaceView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
答案 1 :(得分:0)
修正了问题:
我在我的下面创建了另一种类型的视图 的 AppName.Droid.CameraControls.CameraLiveStream 强>
所以它就像这样
<AppName.Droid.Camera.Controls.CameraLiveStream>
<AppName.Droid.Camera.Controls.Overlay>
</AppName.Droid.Camera.Controls.Overlay>
</AppName.Droid.Camera.Controls.CameraLiveStream>
其中,覆盖层继承了实现视图宽度/高度的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; }