Ejemplo n.º 1
0
        async void Handle_Clicked(object sender, System.EventArgs e)
        {
            loading.IsVisible = true;

            var button = sender as SfButton;

            switch (button.StyleId)
            {
            case "takeImageButton":
                await CommonFunction.StoreImages(Ultis.Settings.SessionUserItem.DriverId, this, "ProfilePic");

                ShowProfilePicture();
                break;

            case "chooseImageButton":
                try
                {
                    await CrossMedia.Current.Initialize();

                    if (!CrossMedia.Current.IsPickPhotoSupported)
                    {
                        //content page refer to the page that call this function (this)
                        await DisplayAlert("No Camera", "No camera available", "OK");

                        return;
                    }

                    var mediaOption = new PickMediaOptions()
                    {
                        PhotoSize = PhotoSize.Medium
                    };

                    var selectedImageFile = await CrossMedia.Current.PickPhotoAsync(mediaOption);

                    if (selectedImageFile == null)
                    {
                        loading.IsVisible = false;
                        return;
                    }
                    else
                    {
                        App.Database.DeleteUserImage(Ultis.Settings.SessionUserItem.DriverId);
                        AppImage image = new AppImage();
                        image.id = Ultis.Settings.SessionUserItem.DriverId;
                        image.photoFileLocation = "";
                        image.Uploaded          = false;

                        string photoFileName = HelperUtil.GetPhotoFileName(image.photoFileLocation);

                        image.photoFileName = photoFileName;
                        image.type          = "ProfilePic";
                        byte[] imagesAsBytes;
                        using (var memoryStream = new MemoryStream())
                        {
                            selectedImageFile.GetStream().CopyTo(memoryStream);
                            selectedImageFile.Dispose();
                            imagesAsBytes = memoryStream.ToArray();
                        }

                        //resize the photo and store in different directory
                        byte[] thumbnailByte;

                        thumbnailByte = DependencyService.Get <IThumbnailHelper>().ResizeImage(imagesAsBytes, 720, 1080, 100);

                        image.imageData = thumbnailByte;
                        App.Database.SaveRecordImageAsync(image);
                    }

                    ShowProfilePicture();
                }
                catch (Exception ex)
                {
                    // await DisplayAlert("Error", ex.Message, "OK");
                }
                break;
            }
        }
Ejemplo n.º 2
0
        //capture image and store local path in db function
        public static async Task StoreImages(string id, ContentPage contentPage, string imageType)
        {
            try
            {
                await CrossMedia.Current.Initialize();

                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {
                    //content page refer to the page that call this function (this)
                    await contentPage.DisplayAlert("No Camera", "No camera available", "OK");

                    return;
                }

                var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions {
                    Directory = "App_Image"
                });

                if (file == null)
                {
                    return;
                }

                if (id == Ultis.Settings.SessionUserItem.DriverId)
                {
                    App.Database.DeleteUserImage(Ultis.Settings.SessionUserItem.DriverId);
                }

                AppImage image = new AppImage();
                image.id = id;
                image.photoFileLocation = file.Path;
                image.Uploaded          = false;

                string photoFileName = HelperUtil.GetPhotoFileName(image.photoFileLocation);

                image.photoFileName = photoFileName;
                image.type          = imageType;
                byte[] imagesAsBytes;
                using (var memoryStream = new MemoryStream())
                {
                    file.GetStream().CopyTo(memoryStream);
                    file.Dispose();
                    imagesAsBytes = memoryStream.ToArray();
                }

                //resize the photo and store in different directory
                byte[] thumbnailByte;
                if (id == Ultis.Settings.SessionUserItem.DriverId)
                {
                    thumbnailByte = DependencyService.Get <IThumbnailHelper>().ResizeImage(imagesAsBytes, 720, 1080, 100);
                }
                else
                {
                    thumbnailByte = DependencyService.Get <IThumbnailHelper>().ResizeImage(imagesAsBytes, 256, 256, 100);
                }
                string thumbnailFolder = HelperUtil.GetThumbnailFolder(image.photoFileLocation);
                if (!Directory.Exists(thumbnailFolder))
                {
                    Directory.CreateDirectory(thumbnailFolder);
                }
                image.photoThumbnailFileLocation = Path.Combine(thumbnailFolder, photoFileName);
                File.WriteAllBytes(image.photoThumbnailFileLocation, thumbnailByte);
                image.imageData = thumbnailByte;
                App.Database.SaveRecordImageAsync(image);
            }
            catch (Exception ex)
            {
                await contentPage.DisplayAlert("Reminder", ex.Message, "OK");
            }
        }