如何使用Xamarin形式的设备相机自动捕获图像

时间:2018-08-14 06:04:51

标签: xaml xamarin xamarin.forms xamarin.ios xamarin.android

我正在尝试在我的xamarin应用程序中实现面部检测以进行身份​​验证。我想自动捕获图像而无需用户干预,我能够使用捕获按钮捕获图像,但我的要求是图像捕获应自动进行,请帮助我实现这一目标。

1 个答案:

答案 0 :(得分:2)

有两个部分要做。您将需要打开“相机”活动:

关于此,有多个tutorials

Android

App.Instance.ShouldTakePicture += () => {
   var intent = new Intent(MediaStore.ActionImageCapture);
   intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(file));
   StartActivityForResult(intent, 0);
};

IOS

imagePicker.FinishedPickingMedia += (sender, e) => {
            var filepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "tmp.png");
var image = (UIImage)e.Info.ObjectForKey(new NSString("UIImagePickerControllerOriginalImage"));
            InvokeOnMainThread(() => {
                image.AsPNG().Save(filepath, false);
                App.Instance.ShowImage(filepath);
            });
            DismissViewController(true, null);
        };

下一步是使用facial recognition,一旦您拥有用户头像,就可以存储图像。

using (var stream = photo.GetStream())
{
    var faceServiceClient = new FaceServiceClient("{FACE_API_SUBSCRIPTION_KEY}");

    // Step 4a - Detect the faces in this photo.
    var faces = await faceServiceClient.DetectAsync(stream);
    var faceIds = faces.Select(face => face.FaceId).ToArray();

    // Step 4b - Identify the person in the photo, based on the face.
    var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);
    var result = results[0].Candidates[0].PersonId;

    // Step 4c - Fetch the person from the PersonId and display their name.
    var person = await faceServiceClient.GetPersonAsync(personGroupId, result);
    UserDialogs.Instance.ShowSuccess($"Person identified is {person.Name}.");
}