Пример #1
0
        private async void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
        {
            MenuFlyoutItem menuItem = sender as MenuFlyoutItem;

            switch (menuItem.Tag.ToString())
            {
            case "FromCamera": {
                MainCanvas.Children.Clear();
                StorageFile photo = await doWhat.GetImageFromCamera();

                if (photo != null)
                {
                    var stream = await photo.OpenAsync(FileAccessMode.Read);

                    var image = new BitmapImage();
                    image.SetSource(stream);
                    MainImage.Source = image;
                    size_image       = new Size(image.PixelWidth, image.PixelHeight);

                    //开始请求认知服务
                    LoadingRing.IsActive = true;
                    try
                    {
                        faces = await cognitiveService.GetFaces(photo);

                        emotions = await cognitiveService.GetEmotions(photo);
                    }
                    catch
                    {
                        DoWhat.ShowNotify("获取服务失败,请检查网络状况");
                    }

                    if (faces != null)
                    {
                        DisplayFaces(faces);
                    }
                    if (emotions != null)
                    {
                        DisplayEmotions(emotions);
                    }

                    LoadingRing.IsActive = false;
                }
                break;
            }

            case "FromAlbum": {
                StorageFile file = await doWhat.GetImageFromAlbum();

                if (file != null)
                {
                    var stream = await file.OpenAsync(FileAccessMode.Read);

                    var image = new BitmapImage();
                    image.SetSource(stream);
                    MainImage.Source = image;
                    size_image       = new Size(image.PixelWidth, image.PixelWidth);

                    LoadingRing.IsActive = true;
                    try
                    {
                        faces = await cognitiveService.GetFaces(file);

                        emotions = await cognitiveService.GetEmotions(file);
                    }
                    catch
                    {
                        DoWhat.ShowNotify("获取服务失败,请检查网络状况");
                    }

                    if (faces != null)
                    {
                        DisplayFaces_Me(faces);
                    }
                    if (emotions != null)
                    {
                        DisplayEmotions(emotions);
                    }
                    LoadingRing.IsActive = false;
                }
                break;
            }

            case "FromURL": {
                var content = Clipboard.GetContent();
                if (content != null && content.Contains(StandardDataFormats.Text))
                {
                    var url = await content.GetTextAsync();

                    imageLocation.Text   = url;
                    MainImage.Source     = new BitmapImage(new Uri(imageLocation.Text));
                    LoadingRing.IsActive = true;
                }
                break;
            }

            default:
                break;
            }
        }