如何使用 Xamarin Community Toolkit 获取从 CameraView 拍摄的照片?

问题描述 投票:0回答:0

我正在编写一个跨平台应用程序并尝试创建一个相机预览页面,用户可以在其中查看实时相机视图。我正在使用 Xamarin Community Toolkit CameraView,但遇到了一个问题。这是我的 XAML 文件代码。

<ContentPage.Content>
        <ContentView x:Name="contentView">
            <Grid x:Name="cameraGrid">
                <xct:CameraView
                x:Name="xctCameraView"
                CaptureMode="Photo"
                HorizontalOptions="FillAndExpand"
                VerticalOptions="FillAndExpand"/>
                <StackLayout VerticalOptions="EndAndExpand">
                    <StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand" BackgroundColor="Transparent">
                        <Frame CornerRadius="15" x:Name="videoFrame" WidthRequest="48" Padding="7">
                            <Label Text="Video" HorizontalOptions="CenterAndExpand" x:Name="videoLabel" BackgroundColor="Transparent" />
                        </Frame>
                        <Frame CornerRadius="15" x:Name="pictureFrame" WidthRequest="48" Padding="7">
                            <Label Text="Picture" HorizontalOptions="CenterAndExpand" x:Name="pictureLabel" BackgroundColor="Transparent"/>
                        </Frame>
                    </StackLayout>
                    <ImageButton Clicked="CapturePhoto" HeightRequest="120" WidthRequest="120" 
                                 HorizontalOptions="Center" x:Name="captureBtn" BackgroundColor="Transparent"/>
                </StackLayout>
            </Grid>
        </ContentView>
    </ContentPage.Content>

按钮上的 ImageButton 基本上是拍照按钮,我创建了一个 CapturePhoto 方法来拍照。而下面的代码就是CapturePhoto方法的代码。

private void CapturePhoto(object sender, EventArgs e)
        {
            if (isPictureSelected)
            {
                if (xctCameraView != null)
                {
                    xctCameraView.Shutter();

                    using (var stream = xctCameraView.Capture())
                    {
                        using (var memoryStream = new MemoryStream())
                        {
                            stream.CopyTo(memoryStream);
                            byte[] photoData = memoryStream.ToArray();
                            Debug.WriteLine($"navigating to EditPostPage");
                            Navigation.PushAsync(new EditPostPage(userId, textId, photoData));
                        }
                    }
                }
                else
                {
                    DisplayAlert("Error", "Camera view is not available.", "OK");
                }
            }
        }

在 C# 代码中,

isPictureSelected
基本上只是一个布尔值,用于判断用户是在拍照还是在录制视频。但我感到困惑的部分是我把
xctCameraView.Shutter();
从CameraView拍照但不知道如何接收拍摄的照片并将其转换为字节数组以便我可以将它发送到新页面“EditPostPage”与字节数组数据。我使用
using (var stream = xctCameraView.Capture())
来获取从
xctCameraView.Shutter();
拍摄的图像,但这是错误的,因为
Capture()
CameraView
中不存在。

有没有办法获取

xctCameraView.Shutter();
的照片?我之前也使用过
MediaCaptured
并且有
private void MediaCaptured(object sender, MediaCapturedEventArgs e)
但不知道如何接收拍摄的图像并将其转换为字节数组并使用字节数组数据导航到 EditPostPage。

我想知道是否有任何代码基本上从

xctCameraView.Shutter();
接收图像数据,所以我可以将它转换成字节数组。

这是我在代码中加入MediaCaptured时的版本。首先,XAML 代码看起来完全一样,只是我在 CameraView 中添加了

MediaCaptured="MediaCaptured"
,如下所示:

<xct:CameraView
                x:Name="xctCameraView"
                CaptureMode="Photo"
                MediaCaptured="MediaCaptured"
                HorizontalOptions="FillAndExpand"
                VerticalOptions="FillAndExpand"/>

这是 CameraPreview 类的 C# 代码,负责拍摄照片并将照片转换为字节并将其发送到另一个页面:

private void CapturePhoto(object sender, EventArgs e)
        {
            if (isPictureSelected)
            {
                if (xctCameraView != null)
                {
                    Debug.WriteLine($"xctCameraView is not null");
                    xctCameraView.Shutter();
                    Debug.WriteLine($"camera picture taken");
                }
                else
                {
                    DisplayAlert("Error", "Camera view is not available.", "OK");
                }
            }
        }
    private void MediaCaptured(object sender, MediaCapturedEventArgs e)
        {
            switch (xctCameraView.CaptureMode)
            {
                default:
                case CameraCaptureMode.Default:

                case CameraCaptureMode.Photo:
                    Debug.WriteLine($"media captured is passed");
                    if (e.Image != null)
                    {
                        Debug.WriteLine($"e.Image is not null");
                        var imageSource = (StreamImageSource)e.Image;
                        using (var stream = imageSource.Stream(CancellationToken.None).Result)
                        {
                            using (var memoryStream = new MemoryStream())
                            {
                                Debug.WriteLine($"var memoryStream = new MemoryStream() went through");
                                stream.CopyTo(memoryStream);
                                photoData = memoryStream.ToArray();
                                // Use the byte array 'photoData' as needed
                                Debug.WriteLine($"navigating to EditPostPage");
                                Device.BeginInvokeOnMainThread(() =>
                                {
                                    Navigation.PushAsync(new EditPostPage(userId, dareTextId, photoData));
                                });
                            }
                        }
                    }

                    break;


                case CameraCaptureMode.Video:
                    break;
            }
        }

问题是,当我拍照时它起作用了,因为

Debug.WriteLine($"camera picture taken");
显示在输出中。但是,不知何故
MediaCaptured(object sender, MediaCapturedEventArgs e)
没有通过,因为没有任何调试语句显示在输出中。我什至尝试将
xctCameraView.MediaCaptured += MediaCaptured;
进行初始化,但也没有任何改变。为什么
MediaCaptured
不通过?

c# xaml xamarin camera photo
© www.soinside.com 2019 - 2024. All rights reserved.