.net 8 maui blazor 混合 Android 应用程序如何打开相机并从应用程序捕获图像:

问题描述 投票:0回答:1
android android-camera .net-8.0 maui-blazor
1个回答
0
投票

首先请在AndroidManiest中添加相机权限:

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

然后,在运行时需要相机权限:

await Permissions.RequestAsync<Permissions.Camera>();
//run the code at app initialization or before you open camera

然后在剃须刀页面:

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private async void IncrementCount()
    {
        FileResult photo;
        if (DeviceInfo.Current.Platform == DevicePlatform.Android || DeviceInfo.Current.Platform == DevicePlatform.iOS)
        {
            photo = await MediaPicker.CapturePhotoAsync();
        }
        if (photo == null)
        {
            return;
        }
        using (var stream = await photo.OpenReadAsync())
        {
            var dotnetImageStream = new DotNetStreamReference(stream);
            // this is the image stream
            // do what you want here
        }
    }    
}
© www.soinside.com 2019 - 2024. All rights reserved.