简体   繁体   中英

Is there an alternative for the Shutter method for cameraview in xamarin?

I'm creating a cross platform app with Xamarin for iOS and Android which uses the camera. I didn't want to use the MediaPicker to access the camera because I wanted the app to display other information while taking a picture. I therefore used CameraView. I wrote the app and tested it with an Android emulator which was running on Android 11. However, I didn't think to test the app for previous versions of Android. The targeted version of Android is 11, but the minimum android version which works is 5.1. Therefore, the app functions on a phone which uses Android 9, but there is a bug when I try and take a picture. The method which is called upon when the user wants to take a picture is the following:

private void CaptureImage(object sender, EventArgs e)
        {
            xctCameraView.Shutter();
        }

When I try and take a picture using Android 9, an error is thrown: System.NullReferenceException Message=Object reference not set to an instance of an object. I understand there may be some differences between android 9 and 11 and therefore the code doesn't work, but I've been searching for an alternative with no outcome. Can anyone help? Thanks!

EDIT:

<?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>

I use the code below to take a picture on Android 9 on emulator. It works. You could refer to it.

 <StackLayout>
        <xct:CameraView
                  Grid.Column="0"
                  x:Name="xctCameraView"
                  CaptureMode="Photo"
                  FlashMode="Off"
                  HorizontalOptions="FillAndExpand" 
                  VerticalOptions="FillAndExpand" />
        <Button Clicked="Button_Clicked"></Button>
    </StackLayout>

Code behind:

  private void Button_Clicked(object sender, EventArgs e)
    {
        xctCameraView.Shutter();
    }

Permissions:

    <uses-permission android:name="android.permission.CAMERA" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM