Esempio n. 1
0
        // method to send the photo to Custom Vision
        public async void SendPhoto(byte[] image)
        {
            var current = CrossConnectivity.Current.IsConnected;

            // check for the internet connection
            if (!current)
            {
                await App.Current.MainPage.DisplayAlert("Connection Error", "Please connect to the internet", "OK");
            }
            else
            {
                var results = await CustomVisionService.PredictImageContentsAsync(image);

                String resultInString = results.ToString();

                if (resultInString.Length > 0)
                {
                    if (Geolocation.HasMoreOptions(resultInString))
                    {
                        DialogService.ShowLoading("More Restaurants Available");
                        resultInString = await Geolocation.GetCloserOptionAsync(resultInString);
                    }
                    var navigationPage = new NavigationPage(new RestaurantPage(resultInString));

                    DialogService.HideLoading();

                    await App.Current.MainPage.Navigation.PushModalAsync(navigationPage, true);

                    var error  = new NSError();
                    var device = captureDeviceInput.Device;
                    device.LockForConfiguration(out error);
                    device.FlashMode = AVCaptureFlashMode.Off;
                    device.UnlockForConfiguration();
                }
                else
                {
                    DialogService.HideLoading();
                    await App.Current.MainPage.DisplayAlert("Restaurant Not Found", "Please rescan the Logo", "OK");
                }
            }
        }
Esempio n. 2
0
        private async void TakePhotoButtonTapped(object sender, EventArgs e)
        {
            var current = CrossConnectivity.Current.IsConnected;

            // check the internet connection to use the ResDiary API
            if (!current)
            {
                await App.Current.MainPage.DisplayAlert("Connection Error", "Please connect to the internet", "OK");
            }
            else
            {
                try
                {
                    var parameters = camera.GetParameters();
                    parameters.FlashMode = global::Android.Hardware.Camera.Parameters.FlashModeOff;
                    camera.SetParameters(parameters);
                    camera.StopPreview();
                    DialogService.ShowLoading("Scanning Logo");

                    // crop the image into the sqaure in order to make the prediction more accuracy
                    var image = CropImage(textureView.Bitmap);
                    using (var imageStream = new MemoryStream())
                    {
                        await image.CompressAsync(Bitmap.CompressFormat.Jpeg, 50, imageStream);

                        image.Recycle();
                        imageBytes = imageStream.ToArray();
                    }

                    // send the image to CustomVision in form of bytes
                    var results = await CustomVisionService.PredictImageContentsAsync(imageBytes);

                    String resultInString = results.ToString();

                    if (resultInString.Length > 0)
                    {
                        // if the logo appeared more than 1 result than use the Geolocation
                        if (Geolocation.HasMoreOptions(resultInString))
                        {
                            DialogService.ShowLoading("More Restaurants Available");
                            resultInString = await Geolocation.GetCloserOptionAsync(resultInString);
                        }
                        var navigationPage = new NavigationPage(new RestaurantPage(resultInString));

                        DialogService.HideLoading();
                        camera.StartPreview();
                        await App.Current.MainPage.Navigation.PushModalAsync(navigationPage, true);
                    }
                    else
                    {
                        DialogService.HideLoading();
                        camera.StartPreview();

                        await App.Current.MainPage.DisplayAlert("Restaurant Not Found", "Please re-scan the Logo", "OK");
                    }
                }
                catch (Exception)
                {
                    camera.StopPreview();
                    camera.Release();
                    camera = global::Android.Hardware.Camera.Open((int)cameraType);
                    camera.SetPreviewTexture(surfaceTexture);

                    PrepareAndStartCamera();
                }
            }
        }