[英]Is there an alternative for the Shutter method for cameraview in xamarin?
我正在使用 Xamarin for iOS 和 Android 创建一个使用相机的跨平台应用程序。 我不想使用 MediaPicker 访问相机,因为我希望应用程序在拍照时显示其他信息。 因此,我使用了 CameraView。 我编写了该应用程序并使用在 Android 11 上运行的 Android 模拟器对其进行了测试。但是,我不认为要针对以前的 Android 版本测试该应用程序。 Android 的目标版本是 11,但可以运行的最低 android 版本是 5.1。 因此,该应用程序在使用 Android 9 的手机上运行,但是当我尝试拍照时出现错误。 用户想要拍照时调用的方法如下:
private void CaptureImage(object sender, EventArgs e)
{
xctCameraView.Shutter();
}
当我尝试使用 Android 9 拍照时,会引发错误:System.NullReferenceException Message=Object reference not set to an instance of an object。 我知道 android 9 和 11 之间可能存在一些差异,因此代码不起作用,但我一直在寻找没有结果的替代方案。 任何人都可以帮忙吗? 谢谢!
编辑:
<?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:xct="http://xamarin.com/schemas/2020/toolkit" Title="Prenez une photo" BackgroundColor="Transparent"
x:Class="App1.CameraViewPage">
<Grid x:Name="myGrid">
<xct:CameraView
x:Name="xctCameraView"
CaptureMode="Photo"
MediaCaptured="MediaCaptured"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"/>
<StackLayout VerticalOptions="EndAndExpand" Spacing="2" Padding="6">
<Button WidthRequest="70" HeightRequest="70" CornerRadius="40" BackgroundColor="Transparent" BorderColor ="White" BorderWidth="8" Clicked="CaptureImage" x:Name="captureBtn" HorizontalOptions="Center" VerticalOptions="Center"/>
</StackLayout>
<StackLayout VerticalOptions="StartAndExpand" Spacing="2" Padding="6">
<Label x:Name="labelOfMine" x:FieldModifier="public"
Text="" BackgroundColor="LightYellow">
</Label>
</StackLayout>
</Grid>
</ContentPage>
我使用下面的代码在模拟器上的 Android 9 上拍照。 有用。 你可以参考一下。
<StackLayout>
<xct:CameraView
Grid.Column="0"
x:Name="xctCameraView"
CaptureMode="Photo"
FlashMode="Off"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand" />
<Button Clicked="Button_Clicked"></Button>
</StackLayout>
后面的代码:
private void Button_Clicked(object sender, EventArgs e)
{
xctCameraView.Shutter();
}
权限:
<uses-permission android:name="android.permission.CAMERA" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.