C#开发Android移动应用系列之调用手机摄像头拍照
By
jerryxjr1220
at 2023-08-17 • 0人收藏 • 330人看过
继续分享通过Xamarin开发Android应用的案例:调用手机摄像头拍照
新建一个内容页Page3并通过路由注册,方法与之前相同,略过
在内容页Page3.xaml上放一个Image和一个Button
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="XamarinApp1.Views.Page3"> <ContentPage.Content> <StackLayout> <Grid> <Grid.RowDefinitions> <RowDefinition Height="500"></RowDefinition> </Grid.RowDefinitions> <Image Source="" x:Name="imageview" Grid.Row="0" HeightRequest="500" WidthRequest="500"/> </Grid> <Button Text="Shoot" Clicked="Button3_Clicked"/> </StackLayout> </ContentPage.Content> </ContentPage>
3. 在Page3.xaml.cs上编写按钮点击事件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; using Xamarin.Essentials; using System.IO; using XamarinApp1.ViewModels; namespace XamarinApp1.Views { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class Page3 : ContentPage { public Page3 () { InitializeComponent (); } public async void Button3_Clicked(object sender, EventArgs e) { // 检查是否支持相机 if (!MediaPicker.IsCaptureSupported) { await DisplayAlert("Error", "相机不可用", "OK"); return; } // 获取图像 var options = new MediaPickerOptions { Title = "拍照" }; var photo = await MediaPicker.CapturePhotoAsync(options); // 获取图像的字节数据 using (var stream = await photo.OpenReadAsync()) { var buffer = new byte[stream.Length]; await stream.ReadAsync(buffer, 0, (int)stream.Length); // 这里可以对图像数据进行处理 // 显示图像 var im = new Image { Source = ImageSource.FromStream(() => new MemoryStream(buffer)) }; // 将图像显示在你的界面上 imageview.Source = im.Source; } } } }
4. 要调用手机摄像头需设置手机权限,在Properties下的AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.xamarinapp1" android:installLocation="preferExternal"> <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" /> <application android:label="XamarinApp1.Android" android:theme="@style/MainTheme"></application> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.BATTERY_STATS" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> </manifest>
5. 在MainActivity.cs中处理权限请求
using System; using Android.App; using Android.Content.PM; using Android.Runtime; using Android.OS; using AndroidX.Core.App; using Android; namespace XamarinApp1.Droid { [Activity(Label = "XamarinApp1", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { const int RequestStorageWriteId = 0; // 这里的值可以是任意整数,只要确保唯一就可以 protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // 请求存储写入权限 if (CheckSelfPermission(Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted) { ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage }, RequestStorageWriteId); } Xamarin.Essentials.Platform.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App()); } public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) { Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults); base.OnRequestPermissionsResult(requestCode, permissions, grantResults); } } }
登录后方可回帖