我正在为Android开发Xamarin移动应用程序,我尝试使用Camera2类。 一切看起来都很好,但是这一行在转换类型上出现问题。它说(Java.Lang.Object -> Android.Hardware.Camera2.Params.Face[](这一行适用于Android Studio,但不适用于C#。
这是我在 Xamarin 上使用的代码。除了人脸识别,所有构建的请求都可以正常工作。https://github.com/xamarin/monodroid-samples/tree/master/android5.0/Camera2Basic
Face[] faces = result.Get(CaptureResult.StatisticsFaces);
public class CameraCaptureListener : CameraCaptureSession.CaptureCallback
{
public FaceTrainActivityFragment Owner { get; set; }
public File File { get; set; }
public override void OnCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result)
{
Process(result);
}
public override void OnCaptureProgressed(CameraCaptureSession session, CaptureRequest request, CaptureResult partialResult)
{
Process(partialResult);
}
private void Process(CaptureResult result)
{
switch (Owner.mState)
{
case FaceTrainActivityFragment.STATE_PREVIEW:
{
if (result.Get(CaptureResult.StatisticsFaces) != null) {
//Face[] faces = result.Get(CaptureResult.StatisticsFaces);
//Face[] faces = (Face[])result.Get(CaptureResult.StatisticsFaces);
}
break;
}
}
}
}
即使我使用硬强制转换到(Face[](进行编译,它也不允许我进行编译,它给了我相同的Java.Lang.Object错误。
public void CreateCameraPreviewSession()
{
try
{
SurfaceTexture texture = mTextureView.SurfaceTexture;
if (texture == null)
{
throw new IllegalStateException("texture is null");
}
if (null == mCameraDevice) {
return;
}
// We configure the size of default buffer to be the size of camera preview we want.
texture.SetDefaultBufferSize(mPreviewSize.Width, mPreviewSize.Height);
// This is the output Surface we need to start preview.
Surface surface = new Surface(texture);
// We set up a CaptureRequest.Builder with the output Surface.
mPreviewRequestBuilder = mCameraDevice.CreateCaptureRequest(CameraTemplate.Preview);
mPreviewRequestBuilder.AddTarget(surface);
// Here, we create a CameraCaptureSession for camera preview.
List<Surface> surfaces = new List<Surface>();
surfaces.Add(surface);
//surfaces.Add(mImageReader.Surface);
setFaceDetect(mPreviewRequestBuilder, mFaceDetectMode);
mCameraDevice.CreateCaptureSession(surfaces, new CameraCaptureSessionCallback(this), null);
}
catch (CameraAccessException e)
{
e.PrintStackTrace();
}
我在相机状态侦听器中调用创建相机预览会话,就像那样
public class CameraStateListener : CameraDevice.StateCallback
{
public FaceTrainActivityFragment owner;
public override void OnOpened(CameraDevice cameraDevice)
{
// This method is called when the camera is opened. We start camera preview here.
owner.mCameraOpenCloseLock.Release();
owner.mCameraDevice = cameraDevice;
owner.CreateCameraPreviewSession();
}
它说(Java.Lang.Object -> Android.Hardware.Camera.Params.Face[](这一行适用于Android Studio,但不适用于C#。
从您收到的错误来看,您可能使用了错误的命名空间来Face
。代替Android.Hardware.Camera.Params.Face
,请使用Android.Hardware.Camera2.Params.Face
。