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

/NikonCamera/Nikon Live View and Capture
Function: You find this example project in your Plugins Download as a Xojo (Real Studio) project file within the examples folder: /NikonCamera/Nikon Live View and Capture
Project "Nikon Live View and Capture.rbp"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub Close() If NkLvActive and NkLvContinue then LVEnd End If m=Nil End EventHandler
Property LV As NikonLiveImageMBS
Property m As MyNikonMBS
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 MyNIkonMBS Inherits NikonMBS
EventHandler Sub FileDownloadComplete(info as NikonFileInfoMBS, data as Memoryblock, length as Integer) // does only work if file is a JPEG 'Dim f as FolderItem=SpecialFolder.Desktop.Child("Nikon Data.txt") 'Dim TheDataDump as TextOutputStream=TextOutputStream.Create(f) 'If f <> Nil then 'TheDataDump.Write Data 'TheDataDump.Close 'End if dim pic as Picture = Picture.FromData(Data) Window1.pic= pic WIndow1.CapCanvas.Refresh End EventHandler
EventHandler Sub ImageDownloadComplete(info as NikonImageInfoMBS, data as Memoryblock, length as Integer) // maybe does not work if data is raw pixels! dim pic as Picture = Picture.FromData(Data) Window1.pic= pic WIndow1.CapCanvas.Refresh End EventHandler
EventHandler Sub Progress(Command as integer, Param as Integer, Done as UInt32, Total as UInt32, Percent as double) System.DebugLog "Progress: "+Format(Percent/100.0,"0%") If Percent=100 then Window1.Title="" Else Window1.Title="Transferring..." End If End EventHandler
End Class
Module NK
Sub LVEnd() NkLvActive=false Window1.LVTimer.Mode=0 const kNkMAIDCapability_LiveViewStatus = &h823E const kNkMAIDLiveViewStatus_OFF = 0 const kNkMAIDLiveViewStatus_ON = 1 nk.m.SetCapUInt32(nk.m.kDevice, kNkMAIDCapability_LiveViewStatus, kNkMAIDLiveViewStatus_OFF) if nk.m.Lasterror = 0 then // ok else MsgBox "Failed to disable live view: "+str(nk.m.Lasterror) end if End Sub
Sub LVInit() const kNkMAIDCapability_LiveViewProhibit = &h825E dim n as integer = nk.m.GetCapUInt32(nk.m.kDevice, kNkMAIDCapability_LiveViewProhibit) if nk.m.Lasterror<>0 then MsgBox "error: "+str(nk.m.Lasterror) elseif n <> 0 then MsgBox "kNkMAIDCapability_LiveViewProhibit: "+hex(n) else const kNkMAIDCapability_LiveViewStatus = &h823E const kNkMAIDLiveViewStatus_OFF = 0 const kNkMAIDLiveViewStatus_ON = 1 nk.m.SetCapUInt32(nk.m.kDevice, kNkMAIDCapability_LiveViewStatus, kNkMAIDLiveViewStatus_ON) if nk.m.Lasterror = 0 then // ok Window1.LVTimer.Enabled=True Window1.LVTimer.Period=15 Window1.LVTimer.Mode=2 NkLvActive=True else MsgBox "Failed to enable live view: "+str(nk.m.Lasterror) end if end if End Sub
Sub NKCapture() If NkLvActive then LVEnd 'MsgBox CurrentMethodName+" 1" while m.Capture = false // try again 'MsgBox CurrentMethodName+" 2" wend 'MsgBox CurrentMethodName+" 3" // wait some time dim n as integer = 1 do n = n + 1 app.DoEvents 100 'call m.Async 'MsgBox CurrentMethodName+" 4" if m.OpenItem(0) then 'MsgBox CurrentMethodName+" 5" exit // ok else 'MsgBox CurrentMethodName+" 6" if n>20 then MsgBox "Failed to open first image." Return end if end if loop 'MsgBox CurrentMethodName+" 7" 'MsgBox m.GetCapString(m.kItem, kNkMAIDCapability_Name) if m.OpenData(m.kDataObjTypeImage) = false then MsgBox "Failed to open first image's data." Return end if 'MsgBox CurrentMethodName+" 8" if not m.Acquire then MsgBox "Failed to acquire." Return end if If NkLvContinue then LVInit 'MsgBox CurrentMethodName+" 9" ''MsgBox "OK" End Sub
Sub init() m = new MyNikonMBS Dim f as folderitem=app.ExecutableFile.Parent.Child("Nikon Test Libs") If TargetMacOS then if m.LoadLibrary("/Library/Application Support/Nikon/Camera Control Modules/Type0005 Module.bundle") = false then MsgBox "Failed to load module." Return end if Else If m.LoadLibrary("Type0005.md3")=false then MsgBox "Failed to load module. " Return End If End If if m.OpenModule = false then MsgBox "Failed to open module." Return end if const kNkMAIDCapability_Name = 9 'MsgBox "Module open." 'MsgBox "Name of module: "+m.GetCapString(m.kModule, kNkMAIDCapability_Name) MsgBox str(m.GetSourceCount)+" devices." if m.OpenSource(0) = false then MsgBox "Failed to open first device." Return end if LV=New NikonLiveImageMBS System.DebugLog "Set Live View Status" Const kNkMAIDCapability_LiveViewStatus=1 Dim LVStatus as Boolean=m.SetCapEnumUInt32(m.kDevice, 33342,1) If m.GetCapUInt32(m.kDevice, kNkMAIDCapability_LiveViewStatus)<>1 then MsgBox("Live View not running, "+str(m.GetCapUInt32(m.kDevice, kNkMAIDCapability_LiveViewStatus))) End If System.DebugLog "End Set Live View Status" 'MsgBox m.GetCapString(m.kDevice, kNkMAIDCapability_Name) 'NKCapture End Sub
Note "Credits"
Example code created by Christian Schmitz and Christopher Rosado. 64-Bit safe for Mac.
Property LV As NikonLiveImageMBS
Property NkLvActive As Boolean
Property NkLvContinue As Boolean
Property m As MyNikonMBS
End Module
Class Window1 Inherits Window
Control CapCanvas Inherits Canvas
ControlInstance CapCanvas Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) // Calculate scale factor if pic<>Nil then dim faktor as Double = min( g.Height / Pic.Height, g.Width / Pic.Width) // Calculate new size dim w as integer = Pic.Width * faktor dim h as integer = Pic.Height * faktor dim x as integer = (g.width-w)/2 dim y as integer = (g.height-h)/2 // draw picture in the new size g.DrawPicture Pic, x, y, w, h, 0, 0, Pic.Width, Pic.Height end if End EventHandler
End Control
Control BevelButton1 Inherits BevelButton
ControlInstance BevelButton1 Inherits BevelButton
EventHandler Sub Action() NKCapture End EventHandler
End Control
Control LVCanvas Inherits Canvas
ControlInstance LVCanvas Inherits Canvas
End Control
Control LVTimer Inherits Timer
ControlInstance LVTimer Inherits Timer
EventHandler Sub Action() If NkLvActive=False then Return LV = nk.m.GetLiveViewImage(5) if lv<>nil then LVPic = JPEGStringToPictureMBS(lv.JPEGData) else LVPic = nil End If LVCanvas.Backdrop=LVPic Label1.Text=str(Val(Label1.Text)+1) End EventHandler
End Control
Control BevelButton2 Inherits BevelButton
ControlInstance BevelButton2 Inherits BevelButton
EventHandler Sub Action() LVInit NkLvContinue=true End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control BevelButton3 Inherits BevelButton
ControlInstance BevelButton3 Inherits BevelButton
EventHandler Sub Action() Dim j as Boolean Const eNkMAIDShootingMode_RemoteTimer_Instant = 5 j = m.SetCapEnumUInt32(m.kDevice, &h818d, eNkMAIDShootingMode_RemoteTimer_Instant) dim selected as UInt32 dim values() as UInt32 = m.GetCapEnumUInt32(m.kDevice, &h818d, selected) 'MsgBox str(selected)+", "+str(j) End EventHandler
End Control
Control BevelButton4 Inherits BevelButton
ControlInstance BevelButton4 Inherits BevelButton
EventHandler Sub Action() CapWindow.show for each c as NikonCapInfoMBS in nk.m.Capabilities(nk.m.kDevice) CapWindow.List.AddRow "Device", str(c.ID)+" "+hex(c.id), c.Description, c.OperationsString, c.TypeString, c.VisibilityString next for each c as NikonCapInfoMBS in nk.m.Capabilities(nk.m.kModule) CapWindow.List.AddRow "Module", str(c.ID)+" "+hex(c.id), c.Description, c.OperationsString, c.TypeString, c.VisibilityString next End EventHandler
End Control
Control BevelButton5 Inherits BevelButton
ControlInstance BevelButton5 Inherits BevelButton
EventHandler Sub Action() NkLvContinue=False LVEnd End EventHandler
End Control
EventHandler Sub Close() If NkLvActive and NkLvContinue then LVEnd End If m=Nil End EventHandler
EventHandler Sub Open() init End EventHandler
Property LVImage As Picture
Property LVPic As Picture
Property V As Variant
Property V2 As Variant
Property pic As Picture
End Class
Class CapWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
End Class
End Project

See also:

Feedback, Comments & Corrections




Links
MBS FileMaker Plugins