public override void ViewDidLoad() { base.ViewDidLoad(); View.BackgroundColor = UIColor.FromRGBA(0.0f, 0.0f, 0.0f, 1f); // ここにInitUI()の呼び出しを書く // Disable UI. The UI is enabled if and only if the session starts running. CameraButton.Enabled = false; RecordButton.Enabled = false; PhotoButton.Enabled = false; LivePhotoModeButton.Enabled = false; CaptureModeControl.Enabled = false; // Setup the preview view. PreviewView.Session = session; // Check video authorization status. Video access is required and audio access is optional. // If audio access is denied, audio is not recorded during movie recording. switch (AVCaptureDevice.GetAuthorizationStatus(AVMediaType.Video)) { // The user has previously granted access to the camera. case AVAuthorizationStatus.Authorized: break; // The user has not yet been presented with the option to grant video access. // We suspend the session queue to delay session setup until the access request has completed to avoid // asking the user for audio access if video access is denied. // Note that audio access will be implicitly requested when we create an AVCaptureDeviceInput for audio during session setup. case AVAuthorizationStatus.NotDetermined: sessionQueue.Suspend(); AVCaptureDevice.RequestAccessForMediaType(AVMediaType.Video, granted => { if (!granted) { setupResult = AVCamSetupResult.CameraNotAuthorized; } sessionQueue.Resume(); }); break; // The user has previously denied access. default: setupResult = AVCamSetupResult.CameraNotAuthorized; break; } // Setup the capture session. // In general it is not safe to mutate an AVCaptureSession or any of its inputs, outputs, or connections from multiple threads at the same time. // Why not do all of this on the main queue? // Because AVCaptureSession.StartRunning is a blocking call which can take a long time. We dispatch session setup to the sessionQueue // so that the main queue isn't blocked, which keeps the UI responsive. sessionQueue.DispatchAsync(ConfigureSession); }
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- public override void ViewDidLoad() { base.ViewDidLoad(); // Create the AVCaptureSession. session = new AVCaptureSession(); // Set up the preview view. PreviewView.Session = session; setupResult = AVCamSetupResult.Success; switch (AVCaptureDevice.GetAuthorizationStatus(AVMediaType.Video)) { case AVAuthorizationStatus.Authorized: // The user has previously granted access to the camera. break; case AVAuthorizationStatus.NotDetermined: /* * The user has not yet been presented with the option to grant * video access. We suspend the session queue to delay session * setup until the access request has completed. * * Note that audio access will be implicitly requested when we * create an AVCaptureDeviceInput for audio during session setup. */ AVCaptureDevice.RequestAccessForMediaType(AVMediaType.Video, (bool granted) => { if (!granted) { setupResult = AVCamSetupResult.CameraNotAuthorized; } }); break; default: { // The user has previously denied access. setupResult = AVCamSetupResult.CameraNotAuthorized; break; } } ConfigureSession(); }
public AVCamCameraView() { #region UIVIew adds session = new AVCaptureSession(); VideoPreviewLayer = new AVCaptureVideoPreviewLayer(session) { Frame = Bounds, VideoGravity = AVLayerVideoGravity.ResizeAspectFill }; var videoDevices = AVCaptureDevice.DevicesWithMediaType(AVMediaType.Video); var cameraPosition = (cameraOptions == CameraOptions.Front) ? AVCaptureDevicePosition.Front : AVCaptureDevicePosition.Back; var device = videoDevices.FirstOrDefault(d => d.Position == cameraPosition); if (device == null) { return; } NSError error; var input = new AVCaptureDeviceInput(device, out error); session.AddInput(input); Layer.AddSublayer(VideoPreviewLayer); //session.StartRunning(); IsPreviewing = true; #endregion // Create a device discovery session. videoDeviceDiscoverySession = AVCaptureDeviceDiscoverySession.Create( new AVCaptureDeviceType[] { AVCaptureDeviceType.BuiltInWideAngleCamera, AVCaptureDeviceType.BuiltInDualCamera }, AVMediaType.Video, AVCaptureDevicePosition.Unspecified ); // Communicate with the session and other session objects on this queue. sessionQueue = new DispatchQueue("session queue", false); setupResult = AVCamSetupResult.Success; }
// Call this on the session queue. void ConfigureSession() { if (setupResult != AVCamSetupResult.Success) { return; } NSError error = null; session.BeginConfiguration(); /* * We do not create an AVCaptureMovieFileOutput when setting up the session because the * AVCaptureMovieFileOutput does not support movie recording with AVCaptureSessionPresetPhoto. */ session.SessionPreset = AVCaptureSession.PresetPhoto; // Add video input. // Choose the back dual camera if available, otherwise default to a wide angle camera. var videoDevice = AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInDualCamera, AVMediaType.Video, AVCaptureDevicePosition.Back); if (videoDevice == null) { // If the back dual camera is not available, default to the back wide angle camera. videoDevice = AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInWideAngleCamera, AVMediaType.Video, AVCaptureDevicePosition.Back); // In some cases where users break their phones, the back wide angle camera is not available. In this case, we should default to the front wide angle camera. if (videoDevice == null) { videoDevice = AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInWideAngleCamera, AVMediaType.Video, AVCaptureDevicePosition.Front); } } var lVideoDeviceInput = AVCaptureDeviceInput.FromDevice(videoDevice, out error); if (lVideoDeviceInput == null) { Console.WriteLine($"Could not create video device input: {error}"); setupResult = AVCamSetupResult.SessionConfigurationFailed; session.CommitConfiguration(); return; } if (session.CanAddInput(lVideoDeviceInput)) { session.AddInput(lVideoDeviceInput); videoDeviceInput = lVideoDeviceInput; DispatchQueue.MainQueue.DispatchAsync(() => { /* * Why are we dispatching this to the main queue? * Because AVCaptureVideoPreviewLayer is the backing layer for AVCamPreviewView and UIView * can only be manipulated on the main thread. * Note: As an exception to the above rule, it is not necessary to serialize video orientation changes * on the AVCaptureVideoPreviewLayer’s connection with other session manipulation. * * Use the status bar orientation as the initial video orientation. Subsequent orientation changes are * handled by -[AVCamCameraViewController viewWillTransitionToSize:withTransitionCoordinator:]. */ var statusBarOrientation = UIApplication.SharedApplication.StatusBarOrientation; var initialVideoOrientation = AVCaptureVideoOrientation.Portrait; if (statusBarOrientation != UIInterfaceOrientation.Unknown) { initialVideoOrientation = (AVCaptureVideoOrientation)statusBarOrientation; } VideoPreviewLayer.Connection.VideoOrientation = initialVideoOrientation; }); } else { Console.WriteLine(@"Could not add video device input to the session"); setupResult = AVCamSetupResult.SessionConfigurationFailed; session.CommitConfiguration(); return; } // Add audio input. var audioDevice = AVCaptureDevice.GetDefaultDevice(AVMediaType.Audio); var audioDeviceInput = AVCaptureDeviceInput.FromDevice(audioDevice, out error); if (audioDeviceInput == null) { Console.WriteLine($"Could not create audio device input: {error}"); } if (session.CanAddInput(audioDeviceInput)) { session.AddInput(audioDeviceInput); } else { Console.WriteLine(@"Could not add audio device input to the session"); } // Add photo output. var lPhotoOutput = new AVCapturePhotoOutput(); if (session.CanAddOutput(lPhotoOutput)) { session.AddOutput(lPhotoOutput); photoOutput = lPhotoOutput; photoOutput.IsHighResolutionCaptureEnabled = true; photoOutput.IsLivePhotoCaptureEnabled = photoOutput.IsLivePhotoCaptureSupported; //photoOutput.IsDepthDataDeliveryEnabled(photoOutput.IsDepthDataDeliverySupported()); livePhotoMode = photoOutput.IsLivePhotoCaptureSupported ? AVCamLivePhotoMode.On : AVCamLivePhotoMode.Off; //depthDataDeliveryMode = photoOutput.IsDepthDataDeliverySupported() ? AVCamDepthDataDeliveryMode.On : AVCamDepthDataDeliveryMode.Off; inProgressPhotoCaptureDelegates = new Dictionary <long, AVCamPhotoCaptureDelegate>(); inProgressLivePhotoCapturesCount = 0; } else { Console.WriteLine(@"Could not add photo output to the session"); setupResult = AVCamSetupResult.SessionConfigurationFailed; session.CommitConfiguration(); return; } backgroundRecordingId = UIApplication.BackgroundTaskInvalid; session.CommitConfiguration(); }
void ConfigureSession() { if (setupResult != AVCamSetupResult.Success) { return; } session.BeginConfiguration(); // We do not create an AVCaptureMovieFileOutput when setting up the session because the // AVCaptureMovieFileOutput does not support movie recording with AVCaptureSessionPresetPhoto. session.SessionPreset = AVCaptureSession.PresetPhoto; // Add video input. // Choose the back dual camera if available, otherwise default to a wide angle camera. var defaultVideoDevice = AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInDuoCamera, AVMediaType.Video, AVCaptureDevicePosition.Back) ?? AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInWideAngleCamera, AVMediaType.Video, AVCaptureDevicePosition.Back) ?? AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInWideAngleCamera, AVMediaType.Video, AVCaptureDevicePosition.Front); NSError error; var input = AVCaptureDeviceInput.FromDevice(defaultVideoDevice, out error); if (error != null) { Console.WriteLine($"Could not create video device input: {error.LocalizedDescription}"); setupResult = AVCamSetupResult.SessionConfigurationFailed; session.CommitConfiguration(); return; } if (session.CanAddInput(input)) { session.AddInput(input); videoDeviceInput = input; DispatchQueue.MainQueue.DispatchAsync(() => { // Why are we dispatching this to the main queue? // Because AVCaptureVideoPreviewLayer is the backing layer for PreviewView and UIView // can only be manipulated on the main thread. // Note: As an exception to the above rule, it is not necessary to serialize video orientation changes // on the AVCaptureVideoPreviewLayer’s connection with other session manipulation. // Use the status bar orientation as the initial video orientation. Subsequent orientation changes are handled by // ViewWillTransitionToSize method. var statusBarOrientation = UIApplication.SharedApplication.StatusBarOrientation; var initialVideoOrientation = AVCaptureVideoOrientation.Portrait; AVCaptureVideoOrientation videoOrientation; if (statusBarOrientation != UIInterfaceOrientation.Unknown && TryConvertToVideoOrientation(statusBarOrientation, out videoOrientation)) { initialVideoOrientation = videoOrientation; } PreviewView.VideoPreviewLayer.Connection.VideoOrientation = initialVideoOrientation; }); } else { Console.WriteLine("Could not add video device input to the session"); setupResult = AVCamSetupResult.SessionConfigurationFailed; session.CommitConfiguration(); return; } // Add audio input. //var audioDevice = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Audio); var audioDevice = AVCaptureDevice.GetDefaultDevice(AVMediaType.Audio); var audioDeviceInput = AVCaptureDeviceInput.FromDevice(audioDevice, out error); if (error != null) { Console.WriteLine($"Could not create audio device input: {error.LocalizedDescription}"); } if (session.CanAddInput(audioDeviceInput)) { session.AddInput(audioDeviceInput); } else { Console.WriteLine("Could not add audio device input to the session"); } // Add photo output. if (session.CanAddOutput(photoOutput)) { session.AddOutput(photoOutput); photoOutput.IsHighResolutionCaptureEnabled = true; photoOutput.IsLivePhotoCaptureEnabled = photoOutput.IsLivePhotoCaptureSupported; livePhotoMode = photoOutput.IsLivePhotoCaptureSupported ? LivePhotoMode.On : LivePhotoMode.Off; } else { Console.WriteLine("Could not add photo output to the session"); setupResult = AVCamSetupResult.SessionConfigurationFailed; session.CommitConfiguration(); return; } session.CommitConfiguration(); }
public override void ViewDidLoad () { base.ViewDidLoad (); // Disable UI. The UI is enabled if and only if the session starts running. CameraButton.Enabled = false; RecordButton.Enabled = false; PhotoButton.Enabled = false; LivePhotoModeButton.Enabled = false; CaptureModeControl.Enabled = false; // Setup the preview view. PreviewView.Session = session; // Check video authorization status. Video access is required and audio access is optional. // If audio access is denied, audio is not recorded during movie recording. switch (AVCaptureDevice.GetAuthorizationStatus (AVMediaType.Video)) { // The user has previously granted access to the camera. case AVAuthorizationStatus.Authorized: break; // The user has not yet been presented with the option to grant video access. // We suspend the session queue to delay session setup until the access request has completed to avoid // asking the user for audio access if video access is denied. // Note that audio access will be implicitly requested when we create an AVCaptureDeviceInput for audio during session setup. case AVAuthorizationStatus.NotDetermined: sessionQueue.Suspend (); AVCaptureDevice.RequestAccessForMediaType (AVMediaType.Video, granted => { if (!granted) setupResult = AVCamSetupResult.CameraNotAuthorized; sessionQueue.Resume (); }); break; // The user has previously denied access. default: setupResult = AVCamSetupResult.CameraNotAuthorized; break; } // Setup the capture session. // In general it is not safe to mutate an AVCaptureSession or any of its inputs, outputs, or connections from multiple threads at the same time. // Why not do all of this on the main queue? // Because AVCaptureSession.StartRunning is a blocking call which can take a long time. We dispatch session setup to the sessionQueue // so that the main queue isn't blocked, which keeps the UI responsive. sessionQueue.DispatchAsync (ConfigureSession); }
void ConfigureSession () { if (setupResult != AVCamSetupResult.Success) return; session.BeginConfiguration (); // We do not create an AVCaptureMovieFileOutput when setting up the session because the // AVCaptureMovieFileOutput does not support movie recording with AVCaptureSessionPresetPhoto. session.SessionPreset = AVCaptureSession.PresetPhoto; // Add video input. // Choose the back dual camera if available, otherwise default to a wide angle camera. AVCaptureDevice defaultVideoDevice = AVCaptureDevice.GetDefaultDevice (AVCaptureDeviceType.BuiltInDuoCamera, AVMediaType.Video, AVCaptureDevicePosition.Back) ?? AVCaptureDevice.GetDefaultDevice (AVCaptureDeviceType.BuiltInWideAngleCamera, AVMediaType.Video, AVCaptureDevicePosition.Back) ?? AVCaptureDevice.GetDefaultDevice (AVCaptureDeviceType.BuiltInWideAngleCamera, AVMediaType.Video, AVCaptureDevicePosition.Front); NSError error; var input = AVCaptureDeviceInput.FromDevice (defaultVideoDevice, out error); if (error != null) { Console.WriteLine ($"Could not create video device input: {error.LocalizedDescription}"); setupResult = AVCamSetupResult.SessionConfigurationFailed; session.CommitConfiguration (); return; } if (session.CanAddInput (input)) { session.AddInput (input); videoDeviceInput = input; DispatchQueue.MainQueue.DispatchAsync (() => { // Why are we dispatching this to the main queue? // Because AVCaptureVideoPreviewLayer is the backing layer for PreviewView and UIView // can only be manipulated on the main thread. // Note: As an exception to the above rule, it is not necessary to serialize video orientation changes // on the AVCaptureVideoPreviewLayer’s connection with other session manipulation. // Use the status bar orientation as the initial video orientation. Subsequent orientation changes are handled by // ViewWillTransitionToSize method. var statusBarOrientation = UIApplication.SharedApplication.StatusBarOrientation; var initialVideoOrientation = AVCaptureVideoOrientation.Portrait; AVCaptureVideoOrientation videoOrientation; if (statusBarOrientation != UIInterfaceOrientation.Unknown && TryConvertToVideoOrientation(statusBarOrientation, out videoOrientation)) initialVideoOrientation = videoOrientation; PreviewView.VideoPreviewLayer.Connection.VideoOrientation = initialVideoOrientation; }); } else { Console.WriteLine ("Could not add video device input to the session"); setupResult = AVCamSetupResult.SessionConfigurationFailed; session.CommitConfiguration (); return; } // Add audio input. var audioDevice = AVCaptureDevice.DefaultDeviceWithMediaType (AVMediaType.Audio); var audioDeviceInput = AVCaptureDeviceInput.FromDevice (audioDevice, out error); if (error != null) Console.WriteLine ($"Could not create audio device input: {error.LocalizedDescription}"); if (session.CanAddInput (audioDeviceInput)) session.AddInput (audioDeviceInput); else Console.WriteLine ("Could not add audio device input to the session"); // Add photo output. if (session.CanAddOutput (photoOutput)) { session.AddOutput (photoOutput); photoOutput.IsHighResolutionCaptureEnabled = true; photoOutput.IsLivePhotoCaptureEnabled = photoOutput.IsLivePhotoCaptureSupported; livePhotoMode = photoOutput.IsLivePhotoCaptureSupported ? LivePhotoMode.On : LivePhotoMode.Off; } else { Console.WriteLine ("Could not add photo output to the session"); setupResult = AVCamSetupResult.SessionConfigurationFailed; session.CommitConfiguration (); return; } session.CommitConfiguration (); }