در این مقاله قصد داریم نرم افزار Camera را در Xamarin بسازیم
مرحله اول
یک پروژه از نوع Xamarin.Forms ایجاد کنید
مرحله دوم
بعد از ایجاد پروژه وارد Manage Nuget Package بشوید و Nuget زیر را نصب کنید
- Xam.Plugin.Media
مرحله سوم
فایل MainPage.xaml را باز کرده و کد های زیر را وارد کنید
Toolbar Items
- Button - Take a photo with clicked event
- Button - Pick a photo already taken with clicked event
- Button - Take a video with clicked event
- Button - Pick a video already taken with clicked event
- Image - To display a image last taken
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:local="clr-namespace:SEC"
- x:Class="SEC.MainPage"
- Title="Camera Access"
- BackgroundColor="Gray">
- <ScrollView>
- <StackLayout>
- <Label Text="Welcome to Camera Access In Xamarin forms"/>
- <Button Text="Take a Photo"
- x:Name="takePhoto"
- BackgroundColor="Fuchsia"
- Clicked="TakeAPhotoButton_OnClicked"/>
- <Button Text="Pick a Photo"
- x:Name="pickPhoto"
- BackgroundColor="Blue"
- Clicked="PickAPhotoButton_OnClicked"/>
- <Button Text="Take A Video"
- x:Name="takeVideo"
- BackgroundColor="Gray"
- Clicked="TakeAVideoButton_OnClicked"/>
- <Button Text="Pick A Video"
- x:Name="pickVideo"
- BackgroundColor="Green"
- Clicked="PickAVideoButton_OnClicked"/>
- <Image x:Name="image"/>
- </StackLayout>
- </ScrollView>
- </ContentPage>
مرحله چهارم
وارد MainPage.xaml.cs شوید و کد های زیر را بنویسید
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Plugin.Media;
- using Xamarin.Forms;
- namespace SEC
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- takePhoto.Clicked += async (sender, args) =>
- {
- if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
- {
- await DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
- return;
- }
- var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
- {
- PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
- Directory = "Sample",
- Name = "test.jpg"
- });
- if (file == null)
- return;
- await DisplayAlert("File Location", file.Path, "OK");
- image.Source = ImageSource.FromStream(() =>
- {
- var stream = file.GetStream();
- file.Dispose();
- return stream;
- });
- };
- pickPhoto.Clicked += async (sender, args) =>
- {
- if (!CrossMedia.Current.IsPickPhotoSupported)
- {
- await DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
- return;
- }
- var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
- {
- PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium
- });
- if (file == null)
- return;
- image.Source = ImageSource.FromStream(() =>
- {
- var stream = file.GetStream();
- file.Dispose();
- return stream;
- });
- };
- takeVideo.Clicked += async (sender, args) =>
- {
- if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakeVideoSupported)
- {
- await DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
- return;
- }
- var file = await CrossMedia.Current.TakeVideoAsync(new Plugin.Media.Abstractions.StoreVideoOptions
- {
- Name = "video.mp4",
- Directory = "DefaultVideos",
- });
- if (file == null)
- return;
- await DisplayAlert("Video Recorded", "Location: " + file.Path, "OK");
- file.Dispose();
- };
- pickVideo.Clicked += async (sender, args) =>
- {
- if (!CrossMedia.Current.IsPickVideoSupported)
- {
- await DisplayAlert("Videos Not Supported", ":( Permission not granted to videos.", "OK");
- return;
- }
- var file = await CrossMedia.Current.PickVideoAsync();
- if (file == null)
- return;
- await DisplayAlert("Video Selected", "Location: " + file.Path, "OK");
- file.Dispose();
- };
- }
مرحله پنج
namespaces های زیر را using کنید.
- using System;
- using System.Threading.Tasks;
- using Android.App;
- using Android.Content.PM;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.OS;
- using Plugin.Media;
Step 6
کد های زیر را وارد MainActivity کنید
MainActivity.cs
- Using Plugin.Media;
- using System;
- using System.Threading.Tasks;
- using Android.App;
- using Android.Content.PM;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.OS;
- using Plugin.Media;
- namespace SEC.Droid
- {
- [Activity(Label = "SEC", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
- public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
- {
- protected override async void OnCreate(Bundle bundle)
- {
- TabLayoutResource = Resource.Layout.Tabbar;
- ToolbarResource = Resource.Layout.Toolbar;
- base.OnCreate(bundle);
- await CrossMedia.Current.Initialize();
- global::Xamarin.Forms.Forms.Init(this, bundle);
- LoadApplication(new App());
- }
- }
- }
مرحله هفتم
پروژه خود را اجرا کنید
تمام.