Xojo Conferences

Platforms to show: All Mac Windows Linux Cross-Platform

/AVFoundation/Screen Input
Required plugins for this example: MBS AVFoundation Plugin, MBS MacBase Plugin, MBS MacCG Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /AVFoundation/Screen Input
This example is the version from Tue, 3rd Jun 2013.
Project "Screen Input.rbp"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub Open() if AVFoundation.available = false then MsgBox "Please run on Mac OS X 10.7 or newer." quit end if AVFoundation = new MyAVFoundationMBS End EventHandler
Property AVFoundation As MyAVFoundationMBS
End Class
Class MainWindow Inherits Window
EventHandler Sub Close() if videoPreviewLayer<>Nil then videoPreviewLayer.session = nil end if if session<>nil then session.stopRunning session = nil end if window = nil End EventHandler
EventHandler Sub Open() configure End EventHandler
EventHandler Sub Resized() Resize End EventHandler
EventHandler Sub Resizing() Resize End EventHandler
Sub Configure() // Create a screen-sized window and Core Animation layer createWindowAndRootLayer // Create a capture session session = new AVCaptureSessionMBS // Set the session preset dim preset as string = AVFoundationMBS.AVCaptureSessionPreset640x480 session.SessionPreset = preset dim error as NSErrorMBS // Find video devices screenInput = new AVCaptureScreenInputMBS(CGDisplayMBS.MainDisplay) // some options you can try: screenInput.removesDuplicateFrames = true screenInput.capturesCursor = true screenInput.capturesMouseClicks = true 'screenInput.cropRect = CGMakeRectMBS(0, 0, 1000, 500) session.addInputWithNoConnections screenInput // Find the video input port dim videoPort as AVCaptureInputPortMBS = screenInput.portWithMediaType(AVFoundationMBS.AVMediaTypeVideo) videoPreviewLayer = AVCaptureVideoPreviewLayerMBS.layerWithSessionWithNoConnection(session) // Create a connection with the input port and the preview layer // and add it to the session connection = AVCaptureConnectionMBS.connectionWithInputPort(videoPort,videoPreviewLayer) session.addConnection connection CATransactionMBS.begin // Disable implicit animations for this transaction CATransactionMBS.setValue(true, CATransactionMBS.kCATransactionDisableActions) // Set the layer frame videoPreviewLayer.Frame = rootLayer.frame // We want the video content to always fill the entire layer regardless of the layer size, // so set video gravity to ResizeAspectFill videoPreviewLayer.VideoGravity = AVFoundationMBS.AVLayerVideoGravityResizeAspectFill // move to back videoPreviewLayer.zPosition = -1 // Add the preview layer to the root layer rootLayer.addSublayer videoPreviewLayer CATransactionMBS.commit session.startRunning End Sub
Sub Resize() CATransactionMBS.begin // Disable implicit animations for this transaction CATransactionMBS.setValue(true, CATransactionMBS.kCATransactionDisableActions) dim rootBounds as CGRectMBS = rootlayer.bounds dim curLayerFrame as CGRectMBS = rootBounds videoPreviewLayer.Frame = curLayerFrame CATransactionMBS.commit End Sub
Sub createWindowAndRootLayer() window = MainWindow.NSWindowMBS // Make the content view layer-backed dim windowContentView as NSViewMBS = window.contentView windowContentView.wantsLayer = true // Grab the Core Animation layer rootLayer = windowContentView.layer rootLayer.autoresizingMask = rootLayer.kCALayerHeightSizable + rootLayer.kCALayerWidthSizable 'rootLayer = window1.canvas1.calayermbs // Set its background color to opaque black dim colorspace as CGColorSpaceMBS = CGColorSpaceMBS.CreateDeviceRGB dim black as new MemoryBlock(8*4) black.SingleValue(0) = 0 // red black.SingleValue(4) = 0 // green black.SingleValue(8) = 0 // blue black.SingleValue(12) = 1.0 // alpha dim blackcolor as CGColorMBS = CGColorMBS.Create(colorspace, black) rootLayer.backgroundColor = blackColor // Show the window MainWindow.show End Sub
Property connection As AVCaptureConnectionMBS
Property rootLayer As CALayerMBS
Property screenInput As AVCaptureScreenInputMBS
Property session As AVCaptureSessionMBS
Property videoPreviewLayer As AVCaptureVideoPreviewLayerMBS
Property window As NSWindowMBS
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
Class PhotoWindow Inherits Window
End Class
Class MyAVFoundationMBS Inherits AVFoundationMBS
EventHandler Sub captureStillImageAsynchronouslyCompleted(prepareJpegStillImage as boolean, tag as integer, error as NSErrorMBS, imageDataSampleBuffer as CMSampleBufferMBS, JpegStillImage as memoryblock) dim pic as Picture = Picture.FromData(JpegStillImage) dim p as new PhotoWindow p.Backdrop = pic p.Width = pic.Width p.Height = pic.Height p.Left = MainWindow.Left + 20 + MainWindow.Width p.top = MainWindow.top p.show End EventHandler
End Class
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS AVFoundation Plugin.

MBS Xojo PDF Plugins