根据Xam.Plugin.Media拍摄的照片创建图库

时间:2018-05-30 14:43:31

标签: c# plugins xamarin.forms gallery media

我是Xamarin.forms和c#以及App Development的新手。 目前我正在开发一个可以拍照的应用程序然后你可以将它们发送到服务器,这样你就可以在WebApp上查看它们了。我正在使用Xamarin.Forms。

事情是,我正在疯狂地尝试创建一个带有你拍摄的照片的画廊。有点像以“无限Scrollview + 2x2网格”的方式显示保存在系统文件夹(不是设备的默认图库)中的图片。

如果有人能指出我正确的方向或给我一个提示,链接,示例,无论如何,我真的很感激! ><

This is how the App looks like right now:

And this is my goal:

我的XAML非常简单:

 <ScrollView>
            <StackLayout
                Margin="20">

                <Image
                    x:Name="PhotoImage" />

                <StackLayout
                    Orientation="Horizontal" >

                    <Button 
                        x:Name="CameraButton"
                        BackgroundColor="#95c241"
                        Text="Foto aufnehmen"
                        TextColor="White"
                        Clicked="CameraButton_Clicked"
                        HorizontalOptions="Start" 
                        />

                    <!--<Button
                        x:Name="selectimagebutton"
                        BackgroundColor="#4ab8ae"
                        Clicked="selectimagebutton_clicked"
                        Text="aus der gallerie"
                        TextColor="white"
                        HorizontalOptions="endandexpand" 

                        />-->

                    <Button
                        x:Name="SyncButton"
                        BackgroundColor="#95c241"
                        Clicked="SyncButton_Clicked"
                        Text="Synchronisierung"
                        TextColor="White"
                        HorizontalOptions="EndAndExpand" />

                </StackLayout>

我的MainPage.xampl.cs。我抽搐了很多,不知道它是否对你有所帮助:

//Camera Logic
    public async Task<bool> CheckPhotoPermissionsAsync()
    {
        var cameraStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
        var storageStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);

        if (cameraStatus != PermissionStatus.Granted || storageStatus != PermissionStatus.Granted)
        {
            var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Camera, Permission.Storage });
            cameraStatus = results[Permission.Camera];
            storageStatus = results[Permission.Storage];
        }

        return cameraStatus == PermissionStatus.Granted || storageStatus == PermissionStatus.Granted;
    }

        private async void CameraButton_Clicked(object sender, EventArgs e)
    {
        //Toma la foto y la muestra directamente en la pagina
        //Macht ein Foto und zeigt es auf diese Seite

        bool photoPermissions = await CheckPhotoPermissionsAsync();

        if (!photoPermissions)
        {
            await DisplayAlert("Achtung!", "Damit die App einwandfrei läuft, mussen Sie den Zulassungen zustimmen", "ok");
            return;
        }
        else
        {
            await CrossMedia.Current.Initialize();

            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                //If camera option not available
                //Falls das Handy keine Kamera hat
                await DisplayAlert("Hmmm...!", "Das Gerät hat kein Zugriff auf die Kamera...", "ok");
                return;
            }

            var mediaFile = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
            {
                SaveToAlbum = false,
                //Directory = "FotoApp_Folder",
                Name = fileName(),

                PhotoSize = PhotoSize.Custom,

                //SaveMetaData = false,

                //Nurr bei PhotoSize.Custom
                CustomPhotoSize = 20,
                CompressionQuality = 70,

                //Only for iOS
                AllowCropping = true

            });

            //var mediaFile = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
            //{
            //    SaveToAlbum = false,
            //});

            //await saveScaleAsync(mediaFile);
            //Debug.WriteLine("It work baby! The mediaFile is: " + mediaFile);

            await saveMediaFile(mediaFile);

            var x = ImageSource.FromStream(() =>
            {
                var stream = mediaFile.GetStream();
                //mediaFile.Dispose();
                return stream;                                     

            });

            Debug.WriteLine("It works baby! The mediaFile is: " + mediaFile);

            //await ToBase64(mediaFile);

        }
    }

    private async Task saveMediaFile(MediaFile mediaFile)
    {
        if (mediaFile == null)
        {
            //Falls kein Bild vorhanden ist
            await DisplayAlert("Ups!", "Es gibt kein Bild vorhanden", "ok");
            return;
        }
        try
        {
            var xxx = mediaFile.Path;

            //PhotoTable ist eine Klasse in diesem Fall , die die Attribute vom DB hat.
            var s = new PhotoTable
            {
                Filename = mediaFile.Path,
                Created_At = DateTime.Now,
                Sync_At = null
            };

            await HelperDB.Instance.conn.InsertAsync(s);
            PhotoImage.Source = ImageSource.FromStream(() => mediaFile.GetStream());

        }
        catch (Exception ee)
        {
            //Falls was nicht mit dem DB klappt....
            await DisplayAlert("Ups!", "Bitte versuchen Sie das nochmal. Es gab ein problem mit dem DB" + ee.ToString(), "ok");
        }

        updateText();

    }

    public string fileName()
    {
        DateTime d = DateTime.Now;

        string fileName = $"IMAGE_{d.ToString("yyyyMMdd_HHmmss")}_.jpg";

        return fileName;
    }

0 个答案:

没有答案