我正在尝试创建相机作为我在xamarin表单中的背景,我计划在共享代码中在cameraview上添加内容。
现在我正在为iOS设备做这件事。我现在在这行代码上遇到了崩溃:Frame = liveCameraStream.Bounds
错误:对象引用未设置为对象的实例。
所以我的问题是如何调整当前代码以将相机作为xamarin表单的背景?
这是我在iOS中的渲染器:
[assembly: ExportRenderer(typeof(ICameraBackground), typeof(CameraBackground_iOS))]
namespace project.iOS
{
public class CameraBackground_iOS : ViewRenderer
{
AVCaptureSession captureSession;
AVCaptureDeviceInput captureDeviceInput;
AVCaptureStillImageOutput stillImageOutput;
UIView liveCameraStream;
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
SetupLiveCameraStream();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
SetupLiveCameraStream();
}
public async void SetupLiveCameraStream()
{
await AuthorizeCameraUse();
captureSession = new AVCaptureSession();
var videoPreviewLayer = new AVCaptureVideoPreviewLayer(captureSession)
{
Frame = liveCameraStream.Bounds
};
liveCameraStream.Layer.AddSublayer(videoPreviewLayer);
var captureDevice = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Video);
ConfigureCameraForDevice(captureDevice);
captureDeviceInput = AVCaptureDeviceInput.FromDevice(captureDevice);
var dictionary = new NSMutableDictionary();
dictionary[AVVideo.CodecKey] = new NSNumber((int)AVVideoCodec.JPEG);
stillImageOutput = new AVCaptureStillImageOutput()
{
OutputSettings = new NSDictionary()
};
captureSession.AddOutput(stillImageOutput);
captureSession.AddInput(captureDeviceInput);
captureSession.StartRunning();
}
public void ConfigureCameraForDevice(AVCaptureDevice device)
{
var error = new NSError();
if (device.IsFocusModeSupported(AVCaptureFocusMode.ContinuousAutoFocus))
{
device.LockForConfiguration(out error);
device.FocusMode = AVCaptureFocusMode.ContinuousAutoFocus;
device.UnlockForConfiguration();
}
else if (device.IsExposureModeSupported(AVCaptureExposureMode.ContinuousAutoExposure))
{
device.LockForConfiguration(out error);
device.ExposureMode = AVCaptureExposureMode.ContinuousAutoExposure;
device.UnlockForConfiguration();
}
else if (device.IsWhiteBalanceModeSupported(AVCaptureWhiteBalanceMode.ContinuousAutoWhiteBalance))
{
device.LockForConfiguration(out error);
device.WhiteBalanceMode = AVCaptureWhiteBalanceMode.ContinuousAutoWhiteBalance;
device.UnlockForConfiguration();
}
}
public async Task AuthorizeCameraUse()
{
var authorizationStatus = AVCaptureDevice.GetAuthorizationStatus(AVMediaType.Video);
if (authorizationStatus != AVAuthorizationStatus.Authorized)
{
await AVCaptureDevice.RequestAccessForMediaTypeAsync(AVMediaType.Video);
}
}
答案 0 :(得分:1)
Xamarin.Forms.Labs是Xamarin.Forms的MVVM框架,可以使用CameraView
。它也已经在两个平台上实现。如果您不想安装整个软件包,可以直接获取源代码。但是,如果您决定将相应的许可证添加到项目中,请不要忘记。
可以在Xamarin.Forms项目中轻松地使用CameraView
,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="XLabs.Samples.Pages.Controls.CameraViewPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms">
<StackLayout>
<controls:CameraView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</StackLayout>