Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Linux Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Linux/Raspberry Pi Camera
This example is the version from Mon, 24th Sep 2017.
Project "Raspberry Pi Camera.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
#if RBVersion < 2016 then
MsgBox "Please build with newer Xojo version!"
#elseif TargetX86 then
MsgBox "Please build for Linux ARM 32-bit."
#else
// ok
#endif
End EventHandler
End Class
Class MainWindow Inherits Window
Control Canvas1 Inherits Canvas
ControlInstance Canvas1 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect)
if CurrentPicture <> nil then
g.DrawPicture CurrentPicture, 0, 0, g.Width, g.Height, 0, 0, CurrentPicture.Width, CurrentPicture.Height
end if
End EventHandler
End Control
Control InitButton Inherits PushButton
ControlInstance InitButton Inherits PushButton
EventHandler Sub Action()
camera = nil
camera = new RaspberryPiCameraMBS
// open
if not camera.Open then
MsgBox "Failed to open camera device." + EndOfLine + EndOfLine + camera.ErrorMessage
camera = nil
Return
end if
log "Driver", camera.Driver
log "Card", camera.Card
log "BusInfo", camera.BusInfo
log "Version", camera.Version
log "Capabilities", hex(camera.Capabilities)
dim AvailableFormats() as RaspberryPiCameraFormatDescriptionMBS = camera.AvailableFormats
for each f as RaspberryPiCameraFormatDescriptionMBS in AvailableFormats
log "Format "+str(f.Index), f.PixelformatString
next
dim f as RaspberryPiCameraFormatMBS = camera.CurrentFormat
if f <> nil then
log "Pixelformat", f.PixelformatString
log "Width", str(f.Width)
log "Height", str(f.Height)
end if
if checkJPEG.value then
// init for JPEG capture
if not camera.SetJPEGSize(Canvas1.Width, Canvas1.Height) then
MsgBox "Failed to set jpeg size."+ EndOfLine + EndOfLine + camera.ErrorMessage
camera = nil
Return
end if
else
// init for RGB picture capture
if not camera.SetSize(Canvas1.Width, Canvas1.Height) then
MsgBox "Failed to set size."+ EndOfLine + EndOfLine + camera.ErrorMessage
camera = nil
Return
end if
end if
f = camera.CurrentFormat
if f <> nil then
log "Pixelformat", f.PixelformatString
log "Width", str(f.Width)
log "Height", str(f.Height)
end if
// optionally init buffer
'if not camera.InitBuffer then
'MsgBox "Failed to init buffer."+ EndOfLine + EndOfLine + camera.ErrorMessage
'camera = nil
'Return
'end if
mytimer.mode = 2
Capture
End EventHandler
End Control
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
Control MyTimer Inherits Timer
ControlInstance MyTimer Inherits Timer
EventHandler Sub Action()
Capture
End EventHandler
End Control
Control SaveButton Inherits PushButton
ControlInstance SaveButton Inherits PushButton
EventHandler Sub Action()
if JPEGData.lenb > 0 then
dim f as FolderItem = GetSaveFolderItem(MyFileTypes.Jpeg, "photo.jpg")
if f = nil then Return
dim b as BinaryStream = BinaryStream.Create(f, true)
b.Write JPEGData
elseif CurrentPicture <> nil then
dim f as FolderItem = GetSaveFolderItem(MyFileTypes.Png, "photo.png")
if f = nil then Return
CurrentPicture.Save(f, CurrentPicture.SaveAsPNG)
else
MsgBox "no current picture?"
end if
End EventHandler
End Control
Control checkJPEG Inherits CheckBox
ControlInstance checkJPEG Inherits CheckBox
End Control
Control DoneButton Inherits PushButton
ControlInstance DoneButton Inherits PushButton
EventHandler Sub Action()
MyTimer.Mode = 0
camera = nil
End EventHandler
End Control
Sub Capture()
if camera <> nil then
if camera.Capture then
// if we capture to picture
CurrentPicture = camera.Picture
if CurrentPicture <> nil then
log "Got picture", str(CurrentPicture.Width)+" x "+str(CurrentPicture.Height)
end if
// or if you need JPEGs
JPEGData = camera.JPEG
if lenb(JPEGData) > 0 then
log "Got JPEG", str(lenb(JPEGData))+" bytes"
CurrentPicture = Picture.FromData(JPEGData)
end if
canvas1.Invalidate
end if
end if
End Sub
Sub log(s as string, t as string)
List.AddRow s,t
List.ScrollPosition = List.ListCount
End Sub
Note "Requirements"
You need
* a Raspberry Pi computer
* to attach the camera module
* to enable the video camera
* run a command: sudo modprobe bcm2835-v4l2
* now you should see /dev/video0 when doing "ls /dev/v*" command
Property CurrentPicture As Picture
Property JPEGData As string
Property camera As RaspberryPiCameraMBS
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
MyFileTypes
Filetype image/png
Filetype image/jpeg
End MyFileTypes
End Project
The items on this page are in the following plugins: MBS Linux Plugin.