在 Xamarin 表单中拍摄或选择多个图像 [英] Take or Select Multiple Image in Xamarin Forms

查看:22
本文介绍了在 Xamarin 表单中拍摄或选择多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Plugin.Media.CrossPlatform"nuget 访问相机并在 Xamarin Forms 中拍照.但目前,它一次只拍摄一张照片,并且只上传最后一张拍摄的照片.我想添加多个捕获并允许用户从最近捕获的图像中选择特定图片.这个插件在 Android 和 IOS 平台上怎么可能?

I am using "Plugin.Media.CrossPlatform" nuget for accessing Camera and take pictures in Xamarin Forms. But currently, it takes only a single picture at a time and upload only that last taken picture. I want to add multiple captures and allow a user to select a specific picture from recently captured images. How's that possible with this plugin in Android and IOS both Platform?

此外,我添加了选择模式,如果用户想从图库中选择图片.但也有同样的问题.我想在两个平台上一次选择多个图像并上传.这个在 Xamarin Forms 中是如何实现的?是否有此任务的示例或博客?

Also, I have added selection mode, If a user wants to select a picture from gallery. But there is the same issue. I want to select multiple images at a time and upload it, on both platforms. How's this implement in Xamarin Forms? Is there any example or blog for this task?

我正在使用这个包.

推荐答案

Xamarin Android 表单从 Camera 获取多张照片这是解决方案.

Xamarin Android forms take multiple pictures from Camera this is the solution for it.

public partial class App : Application
{
   // public static App Instance;

    public App ()
    {
        MainPage = new CameraGallery.MainPage();
        InitializeComponent();

    }
}

MainPage.xaml

MainPage.xaml

//请安装 FlowListView 和 ffimageloading nuget pckg

// please install FlowListView and ffimageloading nuget pckg

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:CameraGallery"
             x:Class="CameraGallery.MainPage"
             xmlns:flv="clr-namespace:DLToolkit.Forms.Controls;assembly=DLToolkit.Forms.Controls.FlowListView"
             xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"> 

            <StackLayout x:Name="CameraLayout">
                <flv:FlowListView FlowColumnCount="3" x:Name="listItemsCam" 
                        SeparatorVisibility="None"
                        HasUnevenRows="false" RowHeight="100" >
                    <flv:FlowListView.FlowColumnTemplate>
                        <DataTemplate >
                            <ffimageloading:CachedImage  DownsampleToViewSize="true" AbsoluteLayout.LayoutFlags="All" HeightRequest="100" AbsoluteLayout.LayoutBounds="0,0,1,1" Source="{Binding .}"  Aspect="AspectFill" HorizontalOptions="FillAndExpand">
                            </ffimageloading:CachedImage>
                        </DataTemplate>
                    </flv:FlowListView.FlowColumnTemplate>
                </flv:FlowListView>
                <!--<Image x:Name="image" IsVisible="False"></Image>-->
            </StackLayout>
</ContentPage>  

MainPage.xaml.cs

MainPage.xaml.cs

public partial class MainPage : ContentPage
    {
        ObservableCollection<string> camImageCollection;
        public static MainPage Instance;

        public MainPage()
        {
            InitializeComponent();
            Instance = this;

            var btn = new Button
            {
                Text = "Snap!",
                Command = new Command(o => ShouldTakePicture()),
            };
            CameraLayout.Children.Add(btn);  

            camImageCollection = new ObservableCollection<string>();
        }
        public event Action ShouldTakePicture = () => { };

        public void ShowImage(string[] filepath)
        {
           foreach(var item in filepath)
             camImageCollection.Add(item);
             listItemsCam.FlowItemsSource = camImageCollection;
        }
    }

现在转到你的android项目里面的MainActivity.cs

now go to your android project inside it MainActivity.cs

[Activity(Label = "CameraGallery", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        public static int OPENCAMERACODE = 102;
        //inside OnCreate after LoadApplication(new App()); add these lines
         protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            UserDialogs.Init(this);
            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);

            FlowListView.Init();
            CachedImageRenderer.Init(false);
            LoadApplication(new App());
            MainPage.Instance.ShouldTakePicture += () =>
            {
                ICursor cursor = loadCursor();
                image_count_before = cursor.Count;
                cursor.Close();
                Intent intent = new Intent(MediaStore.IntentActionStillImageCamera);
                IList<ResolveInfo> activities = PackageManager.QueryIntentActivities(intent, 0);
                if(activities.Count >0)
                    StartActivityForResult(Intent.CreateChooser(intent, "Camera Capture"), OPENCAMERACODE);
            };
        }
        public ICursor loadCursor()
        {
            string[] columns = new string[] { MediaStore.Images.ImageColumns.Data, MediaStore.Images.ImageColumns.Id };
            string orderBy = MediaStore.Images.ImageColumns.DateAdded;
            return ContentResolver.Query(MediaStore.Images.Media.ExternalContentUri, columns, null, null, orderBy);
        }
        private void exitingCamera()
        {
            ICursor cursor = loadCursor();
            string[] paths = getImagePaths(cursor, image_count_before);
            MainPage.Instance.ShowImage(paths);// this parameter pass to MainPage.xaml.cs
            cursor.Close();
        }
        public string[] getImagePaths(ICursor cursor, int startPosition)
        {
            int size = cursor.Count - startPosition;
            if (size <= 0) return null;
            string[] paths = new string[size];

            int dataColumnIndex = cursor.GetColumnIndex(MediaStore.Images.ImageColumns.Data);
            for (int i = startPosition; i < cursor.Count; i++)
            {
                cursor.MoveToPosition(i);

                paths[i - startPosition] = cursor.GetString(dataColumnIndex);
            }
            return paths;
        }
        //inside OnActivityResult method do this
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            switch (requestCode)
            {
                case 102:
                        exitingCamera();
                    break;
            }
        }
    }   

这篇关于在 Xamarin 表单中拍摄或选择多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆