void ChangeCamera() { MetadataObjectTypesButton.Enabled = false; SessionPresetsButton.Enabled = false; CameraButton.Enabled = false; ZoomSlider.Enabled = false; // Remove the metadata overlay layers, if any. RemoveMetadataObjectOverlayLayers(); DispatchQueue.MainQueue.DispatchAsync(() => { var currentVideoDevice = videoDeviceInput.Device; var currentPosition = currentVideoDevice.Position; var preferredPosition = AVCaptureDevicePosition.Unspecified; switch (currentPosition) { case AVCaptureDevicePosition.Unspecified: case AVCaptureDevicePosition.Front: preferredPosition = AVCaptureDevicePosition.Back; break; case AVCaptureDevicePosition.Back: preferredPosition = AVCaptureDevicePosition.Front; break; } var videoDevice = DeviceWithMediaType(AVMediaType.Video, preferredPosition); if (videoDevice != null) { NSError err; var vDeviceInput = AVCaptureDeviceInput.FromDevice(videoDevice, out err); if (err != null) { Console.WriteLine($"Error occured while creating video device input: {err}"); return; } session.BeginConfiguration(); // Remove the existing device input first, since using the front and back camera simultaneously is not supported. session.RemoveInput(videoDeviceInput); // When changing devices, a session preset that may be supported // on one device may not be supported by another. To allow the // user to successfully switch devices, we must save the previous // session preset, set the default session preset (High), and // attempt to restore it after the new video device has been // added. For example, the 4K session preset is only supported // by the back device on the iPhone 6s and iPhone 6s Plus. As a // result, the session will not let us add a video device that // does not support the current session preset. var previousSessionPreset = session.SessionPreset; session.SessionPreset = AVCaptureSession.PresetHigh; if (session.CanAddInput(vDeviceInput)) { session.AddInput(vDeviceInput); videoDeviceInput = vDeviceInput; } else { session.AddInput(videoDeviceInput); } // Restore the previous session preset if we can. if (session.CanSetSessionPreset(previousSessionPreset)) { session.SessionPreset = previousSessionPreset; } session.CommitConfiguration(); } MetadataObjectTypesButton.Enabled = true; SessionPresetsButton.Enabled = true; CameraButton.Enabled = true; ZoomSlider.Enabled = true; ZoomSlider.MaxValue = (float)NMath.Min(videoDeviceInput.Device.ActiveFormat.VideoMaxZoomFactor, 8); ZoomSlider.Value = (float)videoDeviceInput.Device.VideoZoomFactor; }); }
private NSString[] AvailableSessionPresets() { return(GetAllSessionPresets().Where(preset => session.CanSetSessionPreset(preset)).ToArray()); }
public void Initialize() { this.Frame = new CGRect(new CGPoint(0, 0), new CGSize(mc_iPreviewWidth, mc_iPreviewHeight)); m_AVCapSession = new AVCaptureSession(); //m_AVCapDevice = AVCaptureDevice.GetDefaultDevice(AVMediaTypes.Video); var arCamDevice = AVCaptureDevice.DevicesWithMediaType(AVMediaType.Video); if (arCamDevice.Length != 0) { m_AVCapDevice = arCamDevice[0]; //フロントカメラを取得 foreach (AVCaptureDevice camDevice in arCamDevice) { if (camDevice.Position == AVCaptureDevicePosition.Back) { m_AVCapDevice = camDevice; } /* * if (camDevice.Position == AVCaptureDevicePosition.Back && m_iCameraDevice == 1) * { * m_AVCapDevice = camDevice; * } */ } if (m_AVCapDevice == null) { m_AVCapDevice = arCamDevice[0]; } } NSError device_error; m_AVCapDevice.LockForConfiguration(out device_error); if (device_error != null) { Console.WriteLine($"Error: {device_error.LocalizedDescription}"); m_AVCapDevice.UnlockForConfiguration(); return; } //フレームレート設定 m_AVCapDevice.ActiveVideoMinFrameDuration = new CMTime(1, 24); m_AVCapDevice.UnlockForConfiguration(); if (m_AVCapDevice == null) { return; } NSError error = null; try{ //m_AVInput = new AVCaptureDeviceInput(m_AVCapDevice, out error); m_AVInput = AVCaptureDeviceInput.FromDevice(m_AVCapDevice); if (error != null) { Console.WriteLine(error.ToString()); } else { m_AVCapSession.AddInput(m_AVInput); m_AVCapSession.BeginConfiguration(); m_AVCapSession.CanSetSessionPreset(AVCaptureSession.PresetHigh); m_AVCapSession.CommitConfiguration(); m_AVVideoOutput = new AVCaptureVideoDataOutput() { AlwaysDiscardsLateVideoFrames = true, WeakVideoSettings = new CVPixelBufferAttributes { PixelFormatType = CVPixelFormatType.CV32BGRA }.Dictionary }; m_OutputRecorder = new OutputRecorder() { m_CustomCamera = m_CustomCamera }; var Queue = new DispatchQueue("myQueue"); m_AVVideoOutput.SetSampleBufferDelegateQueue(m_OutputRecorder, Queue); m_AVCapSession.AddOutput(m_AVVideoOutput); } m_prevAVLayer = new AVCaptureVideoPreviewLayer(m_AVCapSession) { Frame = new CGRect(new CGPoint(0, 0), new CGSize(mc_iPreviewWidth, mc_iPreviewHeight)), VideoGravity = AVLayerVideoGravity.ResizeAspectFill }; Layer.AddSublayer(m_prevAVLayer); m_AVCapSession.StartRunning(); } catch (Exception e) { Console.WriteLine(e.ToString()); } return; }