コード例 #1
0
        async Task ExecuteTakePictureCommandAsync()
        {
            try
            {
                if (!await PermissionHelper.Has(Permission.Camera) || !await PermissionHelper.Has(Permission.Storage))
                {
                    await PermissionHelper.Request(Permission.Camera, Permission.Storage);

                    Description = "Access To Camera & Storage Denied.";
                    return;
                }

                if (AppSettings.ComputerVisionRoot == string.Empty || AppSettings.ComputerVisionKey == string.Empty)
                {
                    await PermissionHelper.Request(Permission.Camera, Permission.Storage);

                    Description = "Root and Key are Empty.";
                    return;
                }

                IsNotBusy = false;

                // Take a picture.
                await CrossMedia.Current.Initialize();

                MediaFile photo;

                if (IsTakePicture)
                {
                    if (CrossMedia.Current.IsCameraAvailable)
                    {
                        photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                        {
                            Directory = "DefinePicture",
                            Name      = "pic.jpg"
                        });
                    }
                    else
                    {
                        IsTakePicture = false;
                        await Application.Current.MainPage.DisplayAlert("Alert", "No Camera Available On Your Device.", "OK");

                        photo = await CrossMedia.Current.PickPhotoAsync();
                    }
                }
                else
                {
                    photo = await CrossMedia.Current.PickPhotoAsync();
                }

                DefinedImage = photo.Path;
                ImageVisible = true;

                // Let Computer Vision help on what you're looking at
                Description = "Defining Picture...";

                var            client = new VisionServiceClient(AppSettings.ComputerVisionKey, AppSettings.ComputerVisionRoot);
                AnalysisResult result = await client.DescribeAsync(photo.GetStream());

                // Parse the result
                var caption    = StringExtension.ToTitleCase(result.Description.Captions[0].Text);
                var confidence = Math.Floor(result.Description.Captions[0].Confidence * 100);

                Description = $"Define Picture Result : \n{caption}. \nPercentage : {confidence}%";
                IsNotBusy   = true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"ERROR: {ex.Message}");
                Description = "Defining Picture Failed.";
                IsNotBusy   = true;
            }
        }