Xojo Developer Conference
25/27th April 2018 in Denver.
MBS Xojo Conference
6/7th September 2018 in Munich, Germany.

Platforms to show: All Mac Windows Linux Cross-Platform

/AVFoundation/Still Photos
Function:
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/Still Photos
This example is the version from Tue, 3rd Jun 2013.
Project "Still Photos.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
Control BevelButton1 Inherits BevelButton
ControlInstance BevelButton1 Inherits BevelButton
EventHandler Sub Action() // search the video connection dim videoConnection as AVCaptureConnectionMBS dim connections() as AVCaptureConnectionMBS = stillImageOutput.connections for each connection as AVCaptureConnectionMBS in connections dim inputPorts() as AVCaptureInputPortMBS = connection.inputPorts for each port as AVCaptureInputPortMBS in inputPorts if port.mediaType = AVFoundationMBS.AVMediaTypeVideo then videoConnection = connection exit for connection end if next next StillImageOutput.captureStillImageAsynchronously videoConnection, true, 0 exception m as NSExceptionMBS MsgBox m.message End EventHandler
End Control
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 dim device as AVCaptureDeviceMBS = AVCaptureDeviceMBS.defaultDeviceWithMediaType(AVFoundationMBS.AVMediaTypeVideo) if device = nil then device = AVCaptureDeviceMBS.defaultDeviceWithMediaType(AVFoundationMBS.AVMediaTypeMuxed) end if if device = nil then MsgBox "No device found for video!" return end if // Create a device input with the device and add it to the session input = AVCaptureDeviceInputMBS.deviceInputWithDevice(device, error) if error<>Nil then MsgBox "deviceInputWithDevice: "+error.localizedDescription return end if session.addInputWithNoConnections input // Find the video input port dim videoPort as AVCaptureInputPortMBS = input.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 StillImageOutput = new AVCaptureStillImageOutputMBS StillImageOutput.outputSettings = new Dictionary(AVFoundationMBS.AVVideoCodecKey:AVFoundationMBS.AVVideoCodecJPEG) session.addOutput StillImageOutput // 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 StillImageOutput As AVCaptureStillImageOutputMBS
Property connection As AVCaptureConnectionMBS
Property devicesCount As Integer
Property input As AVCaptureDeviceInputMBS
Property rootLayer As CALayerMBS
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

Feedback, Comments & Corrections

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




Links
MBS Xojo Chart Plugins