/******************************* MAIN FUNCTIONS *******************************/ public override void ViewDidLoad () { base.ViewDidLoad (); var captureDevice = AVCaptureDevice.DefaultDeviceWithMediaType (AVMediaType.Video); var input = AVCaptureDeviceInput.FromDevice (captureDevice); CaptureSession = new AVCaptureSession(); CaptureSession.AddInput (input as AVCaptureInput); var captureMetadataOutput = new AVCaptureMetadataOutput(); metadataDelegate = new MetadataObjectsDelegate(); metadataDelegate.outer = this; captureMetadataOutput.SetDelegate(metadataDelegate, DispatchQueue.MainQueue); CaptureSession.AddOutput(captureMetadataOutput); captureMetadataOutput.MetadataObjectTypes = AVMetadataObjectType.QRCode; VideoPreviewLayer = new AVCaptureVideoPreviewLayer (CaptureSession); VideoPreviewLayer.VideoGravity = AVLayerVideoGravity.ResizeAspectFill; VideoPreviewLayer.Frame = View.Layer.Bounds; View.Layer.AddSublayer (VideoPreviewLayer); View.BringSubviewToFront (messageLabel); QRCodeFrameView = new UIView (); QRCodeFrameView.Layer.BorderColor = UIColor.Green.CGColor; QRCodeFrameView.Layer.BorderWidth = 2; View.AddSubview (QRCodeFrameView); View.BringSubviewToFront (QRCodeFrameView); CaptureSession.StartRunning(); cancelButton.Clicked += (sender, e) => { this.DismissViewController (true, null); }; }
void setupCaptureSession() { if (CaptureSession != null) { return; } CaptureSession = new AVCaptureSession(); NSNotificationCenter.DefaultCenter.AddObserver(null, captureSessionNotification, CaptureSession); applicationWillEnterForegroundNotificationObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.WillEnterForegroundNotification.ToString(), UIApplication.SharedApplication, NSOperationQueue.CurrentQueue, delegate(NSNotification notification) { applicationWillEnterForeground(); }); videoDevice = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Video); NSError error; videoInput = new AVCaptureDeviceInput(videoDevice, out error); if (CaptureSession.CanAddInput(videoInput)) { CaptureSession.AddInput(videoInput); } metadataOutput = new AVCaptureMetadataOutput(); var metadataQueue = new DispatchQueue("com.AVCam.metadata"); metadataObjectsDelegate = new MetadataObjectsDelegate { DidOutputMetadataObjectsAction = DidOutputMetadataObjects }; metadataOutput.SetDelegate(metadataObjectsDelegate, metadataQueue); if (CaptureSession.CanAddOutput(metadataOutput)) { CaptureSession.AddOutput(metadataOutput); } }
void setupCaptureSession () { if (CaptureSession != null) return; CaptureSession = new AVCaptureSession (); NSNotificationCenter.DefaultCenter.AddObserver (null, captureSessionNotification, CaptureSession); applicationWillEnterForegroundNotificationObserver = NSNotificationCenter.DefaultCenter.AddObserver (UIApplication.WillEnterForegroundNotification.ToString (), UIApplication.SharedApplication, NSOperationQueue.CurrentQueue, delegate(NSNotification notification) { applicationWillEnterForeground (); }); videoDevice = AVCaptureDevice.DefaultDeviceWithMediaType (AVMediaType.Video); NSError error; videoInput = new AVCaptureDeviceInput (videoDevice, out error); if (CaptureSession.CanAddInput (videoInput)) CaptureSession.AddInput (videoInput); metadataOutput = new AVCaptureMetadataOutput (); var metadataQueue = new DispatchQueue ("com.AVCam.metadata"); metadataObjectsDelegate = new MetadataObjectsDelegate { DidOutputMetadataObjectsAction = DidOutputMetadataObjects }; metadataOutput.SetDelegate (metadataObjectsDelegate, metadataQueue); if (CaptureSession.CanAddOutput (metadataOutput)) CaptureSession.AddOutput (metadataOutput); }