2017-01-01 8 views
0

私はxamarinフォームで私の背景としてカメラを作成しようとしています。ここで私は共有コードのカメラビューの上に物事を追加する予定です。電話機のカメラをバックグラウンドとして使用するには?

今は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

は、どのようにクラッシュを修正するかどうか、または、カメラを使用する方法をあなたの質問でありますあなたのアプリケーションの背景?現在、問題が書かれているので、それを伝えるのは難しいです。 – Demitrian

+0

カメラをバックグラウンドとして使用する方法。私は現時点での状況を示すために今どこでクラッシュするのかを言及しました。私は少し明確にするためにスレッドを少し更新します!ヘッドアップありがとう – Martman

答えて

1

Xamarin.Forms.Labs、Xamarin.Forms用MVVMフレームワークは、あなたが使用することができますCameraViewを持っています。両方のプラットフォームでも実装されています。パッケージ全体をインストールしたくない場合は、単にソースを取得するだけです。しかし、そうすることを決めた場合は、プロジェクトに適切なライセンスを追加することを忘れないでください。

CameraViewはそうのようなXamarin.Formsプロジェクトに非常に簡単に消費することができます:

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

+0

その後、 'Xamarin.Forms.Labs'からソースを取得し、あなたのプロジェクトにそのライセンスを含めることができます。次に、必要に応じてレンダラーを変更することもできます。 – Demitrian

+0

ありがとう;)それは働くようになった – Martman

+0

あなたがそれを作ったことを知ってうれしい:) – Demitrian

関連する問題