Inheritance: UIView, IUIGestureRecognizerDelegate
Ejemplo n.º 1
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Disable UI. The UI is enabled if and only if the session starts running.
            MetadataObjectTypesButton.Enabled = false;
            SessionPresetsButton.Enabled      = false;
            CameraButton.Enabled = false;
            ZoomSlider.Enabled   = false;

            // Add the open barcode gesture recognizer to the region of interest view.
            PreviewView.AddGestureRecognizer(OpenBarcodeURLGestureRecognizer);

            // Set up the video 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))
            {
            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.
                sessionQueue.Suspend();
                AVCaptureDevice.RequestAccessForMediaType(AVMediaType.Video, granted => {
                    if (!granted)
                    {
                        setupResult = SessionSetupResult.NotAuthorized;
                    }
                    sessionQueue.Resume();
                });
                break;

            default:
                // The user has previously denied access.
                setupResult = SessionSetupResult.NotAuthorized;
                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 ReleaseDesignerOutlets()
        {
            if (CameraButton != null)
            {
                CameraButton.Dispose();
                CameraButton = null;
            }

            if (CameraUnavailableLabel != null)
            {
                CameraUnavailableLabel.Dispose();
                CameraUnavailableLabel = null;
            }

            if (MetadataObjectTypesButton != null)
            {
                MetadataObjectTypesButton.Dispose();
                MetadataObjectTypesButton = null;
            }

            if (PreviewView != null)
            {
                PreviewView.Dispose();
                PreviewView = null;
            }

            if (SessionPresetsButton != null)
            {
                SessionPresetsButton.Dispose();
                SessionPresetsButton = null;
            }

            if (ZoomSlider != null)
            {
                ZoomSlider.Dispose();
                ZoomSlider = null;
            }
        }
Ejemplo n.º 3
0
        public override void ViewWillTransitionToSize(CGSize toSize, IUIViewControllerTransitionCoordinator coordinator)
        {
            base.ViewWillTransitionToSize(toSize, coordinator);

            var videoPreviewLayerConnection = PreviewView.VideoPreviewLayer.Connection;

            if (videoPreviewLayerConnection != null)
            {
                var deviceOrientation = UIDevice.CurrentDevice.Orientation;
                if (!deviceOrientation.IsPortrait() && !deviceOrientation.IsLandscape())
                {
                    return;
                }

                var newVideoOrientation = VideoOrientationFor(deviceOrientation);
                var oldSize             = View.Frame.Size;
                var oldVideoOrientation = videoPreviewLayerConnection.VideoOrientation;
                videoPreviewLayerConnection.VideoOrientation = newVideoOrientation;

                // When we transition to the new size, we need to adjust the region
                // of interest's origin and size so that it stays anchored relative
                // to the camera.
                coordinator.AnimateAlongsideTransition(context => {
                    var oldRegion = PreviewView.RegionOfInterest;
                    var newRegion = new CGRect();

                    if (oldVideoOrientation == LandscapeRight && newVideoOrientation == LandscapeLeft)
                    {
                        newRegion = oldRegion.WithX(oldSize.Width - oldRegion.X - oldRegion.Width);
                    }
                    else if (oldVideoOrientation == LandscapeRight && newVideoOrientation == Portrait)
                    {
                        newRegion.X      = toSize.Width - oldRegion.Y - oldRegion.Height;
                        newRegion.Y      = oldRegion.X;
                        newRegion.Width  = oldRegion.Height;
                        newRegion.Height = oldRegion.Width;
                    }
                    else if (oldVideoOrientation == LandscapeLeft && newVideoOrientation == LandscapeRight)
                    {
                        newRegion = oldRegion.WithX(oldSize.Width - oldRegion.X - oldRegion.Width);
                    }
                    else if (oldVideoOrientation == LandscapeLeft && newVideoOrientation == Portrait)
                    {
                        newRegion.X      = oldRegion.Y;
                        newRegion.Y      = oldSize.Width - oldRegion.X - oldRegion.Width;
                        newRegion.Width  = oldRegion.Height;
                        newRegion.Height = oldRegion.Width;
                    }
                    else if (oldVideoOrientation == Portrait && newVideoOrientation == LandscapeRight)
                    {
                        newRegion.X      = oldRegion.Y;
                        newRegion.Y      = toSize.Height - oldRegion.X - oldRegion.Width;
                        newRegion.Width  = oldRegion.Height;
                        newRegion.Height = oldRegion.Width;
                    }
                    else if (oldVideoOrientation == Portrait && newVideoOrientation == LandscapeLeft)
                    {
                        newRegion.X      = oldSize.Height - oldRegion.Y - oldRegion.Height;
                        newRegion.Y      = oldRegion.X;
                        newRegion.Width  = oldRegion.Height;
                        newRegion.Height = oldRegion.Width;
                    }

                    PreviewView.SetRegionOfInterestWithProposedRegionOfInterest(newRegion);
                }, context => {
                    sessionQueue.DispatchAsync(() => {
                        metadataOutput.RectOfInterest = PreviewView.VideoPreviewLayer.MapToLayerCoordinates(PreviewView.RegionOfInterest);
                    });
                    // Remove the old metadata object overlays.
                    RemoveMetadataObjectOverlayLayers();
                });
            }
        }