REALbasic Development
Volume Number: 16 (2000)
Issue Number: 4
Column Tag: Programming
Rapid Development with REALbasic
by Erick J. Tejkowski
Extend REALbasic to suit your needs
Introduction
Long the "Holy Grail" of computing, video is finally coming of age in the computer world, but Mac users have known this for some time. QuickTime has been around for awhile now and so has sophisticated video hardware for the Mac. With the explosive growth of the web in recent years, hundreds have wired their web servers with a variety of video camera devices, broadcasting to the worldviews of local scenery, photos from home, or pictures of the family pet.
Luckily, Apple has been involved in one way or another with desktop video from the start. With pioneering technologies in software and hardware, multimedia has usually appeared first, not to mention the best, on Macintosh desktops.
This article will continue in that tradition and describe how to make a web-based camera that will periodically post photos to a web site for the world to see. The reader will also discover how to incorporate these abilities into their own designs, including stop-motion photography, home surveillance, and other fun experiments.
Requirements
Since this project is a bit more hardware related than usual, a brief discussion about the programs requirements might be in order. First, there is a question of video hardware. For a camera, any typical video composite signal is fine. Video hardware that produces composite video usually has S-Video and/or RCA jacks like those on your home stereo. Since most people have a video camcorder these days, we will focus on them. Web cams also make a fine choice, as do digital cameras (if they have composite video output). If you have no camera, but would still like to experiment, a VCR will do as a video source. Unfortunately, QuickCams© will not work for this project, nor will USB devices. These have more sophisticated software requirements, and as such, do not fit into this beginner's style article.
In your computer, you will need a video-input card. Any AV Mac will do, as will as any third party video cards available on the market. Again, it will need a composite video input to interface with the camera described above. To set up your hardware, simply connect the composite video output of the camera to the composite video input of the video card.
To create the software, a few decisions had to be made. To better illustrate the beauty of REALbasic's simplicity, it was decided that the reader should be able to create a working application with only a modicum of effort and expense. Thus, to expedite the capture of video, we will use Apple's own video technology. The Apple Video Player, available with most versions of the MacOS including compatible 68K versions, will bear the responsibility of capturing the video. True capture capabilities can be added to REALbasic, but they are not free and are a bit more complex to program. They will be discussed later, however, for those programmers wishing to expand their video horizons. To interface with the Apple Video player, the REALbasic application will use AppleScript. Since AppleScript is installed with the operating system, this requirement should also not be much of a problem for most users.
The final consideration before embarking on this project was the subject of FTP. Once the photos are captured, we will want them to be transferred to a web site for viewing by a web browser. FTP is not really a beginner's topic. Although several classes already exist that will help you in your FTP endeavors, this project will remove the added complexity of adding FTP to your application. Furthermore, the solution will preclude the need to purchase space on a web server. Like it or not, many beginners do not have FTP accounts at their disposal. How will all of these objectives be met? (.... Enter drum roll....) iDisk! In case you haven't heard, iDisk is part of the greater iTools services offered by Apple on their web site. Registration affords you an email account, 20MB of disk space on the server, personalized greeting cards, and a host of other features. The best part, however, may be that it's completely free! For those of you who have not made the jump to MacOS 9 (a requirement for registration), check the reference section at the end of this article for suggestions about how to use iTools with older operating systems.
For this project, we will utilize iDisk. iDisk is a 20MB partition on Apple's web servers for you to store files. On the disk, you can store text, video, pictures, and more just as you normally do with web servers. iDisk is a bit different though in that you can also mount your iDisk directly on the desktop! The mounted iDisk acts just like a hard drive in the Finder. You can copy, delete, and "Get Info" to your heart's content. Now, the issue of FTP merely becomes a matter of saving a file to a disk, something that can be accomplished in REALbasic with only one line of code. The operating system takes care of the rest for us. To reiterate, Listing 1 details what you will need for the project. One final note to make is that this project can even be completed using a demo version of REALbasic, though your applications will expire after five minutes of use.
Listing 1. Project Requirements
A video camera.
A Mac with a video input card.
REALbasic version 1 or 2
AppleScript
Apple Video Player
Apple iTools/iDisk Account
Building the Project
The code for our video capture will be built in two parts. To begin, we will create an AppleScript that will tell the Apple Video Player to take a photo. Next, we will build a REALbasic project that uses the script as well as adds functionality of its own. The REALbasic project will be responsible for controlling how often the video capture takes place, converting the PICT file to a JPEG, and finally creating the web page and saving it to the iDisk.
AppleScript
To start, launch the AppleScript Script Editor and enter the code in Listing 2. The script is fairly self-explanatory with two possible exceptions. First, the entire script is enclosed within an on run{x} . . . end run handler. The handler is necessary for the script to work with REALbasic. To test the script in the Script Editor it is necessary to remove or comment out the on run handler. The {x} variable is used to indicate the path for the destination file. By default, the Apple Video Player creates PICT files only. This is not really a problem, because REALbasic can help us. To make the photo compatible with the web and to slim down the file size, we will convert the PICT file into a JPEG from within the REALbasic application.
The second topic that might require some explanation is the replacing yes statement. This means that when the file is saved it will replace a file with the same name. For the purposes of this project, this will suffice. A new photo will be captured periodically and replace the previous photo. If you would like to take a series of photos then the code would read replacing yes.
Listing 2. The Apple Video Player AppleScript.
on run (x)
tell application "Apple Video Player"
save video in x replacing yes
end tell
end run
It should be noted that the Apple Video Player has a relatively extensive list of scriptable features. For example, instead of capturing a photo periodically, the script could easily be set to capture QuickTime movies. Be sure to take a look at the Apple Video Player dictionary by selecting the File...Open Dictionary and menu in the Script Editor to custom tailor the script to your needs. Before closing the Script Editor, be sure to save the file as a Compiled Script, naming it something functional like CapturePhoto.
REALbasic
The final step in the video surveillance system is to build a REALbasic project. Begin by starting the REALbasic application. A new project is created automatically. Go to the Finder and drag the AppleScript created earlier into the REALbasic Project window. Next, open Window1 and create two variables by selecting the Edit..New Property menu. Name the variables as shown in Listing 3. Create a new method called CreateWebPage and enter the code also shown in Listing 3.
Listing 3. Window1 Properties and Methods.
Properties
timersetting as integer
thepict as picture
Methods
Window1.CreateWebPage:
Sub CreateWebPage()
Dim f As FolderItem
Dim TStream As TextOutputStream
Dim d as date
Dim myURL as string
d=new date
//create the web page here
//with the current time on it
//replace "myidisk" below with your own iDisk name!!!
f=GetFolderItem("myidisk:Sites:mysnapshot.html")
TStream=f.CreateTextFile
TStream.WriteLine "<html><head>"
TStream.WriteLine "<title>My Surveillance Camera
</title>"TStream.WriteLine "</head>"
TStream.WriteLine "<body>"
TStream.WriteLine "Current Time:"+d.abbreviateddate+" "+
d.longtime+"<br>"//this code belongs to the above line
//replace "myidisk" below with your own iDisk name!!!
myURL="http://"
myURL=myURL+"homepage.mac.com"
myURL=myURL+"/myidisk/.Pictures/MySnapshot.JPG"
TStream.WriteLine "<img src="+chr(34)+myurl+chr(34)+">"
TStream.WriteLine "</body></html>"
TStream.Close
End Sub
Creating the Interface
The next step in creating the project is to create the interface. From the Project Window open Window1. From the toolbar, drag three PushButtons onto Window1. Name them ManualButton, StartButton, and StopButton respectively. Next, drag a PopupMenu onto the window. With the PopupMenu selected, change the InitialValue property in the Properties Window to match the values in Figure 1.
![](fig01.gif)
Figure 1.PopupMenu1.InitalValue Property Editor
Finally, drag a Canvas and a Timer control onto Window1. Leave the default names for these controls as they are. Set the Mode property of Timer1 to 0 (i.e. zero=off) and change the dimensions of Canvas1 to 320 (Width) and 240 (Height). By now, your window might look something like Figure 2.
![](fig02.gif)
Figure 2.Window1 layout
The Code
The final step in creating the REALbasic project is to add some code to each of the controls. The ManualButton will allow the user to take a photo, create a web page, and post them both online. The StartButton initiates the same thing, but does it at regular intervals. The frequency of these intervals is decided by the PopuMenu1 selection. Once the image has been captured using the Apple Video Player it will be displayed in the Canvas1 control. Finally, Timer1 is the control that performs the periodic functions initiated by the StartButton. As before, it is essential that you alter the pathnames to fit your iDisk account. Listing 4 shows the remaining source code for the project.
Listing 4. Interface Source Code
Window1.Canvas1.Paint:
Sub Paint(g As Graphics)
//first fill the canvas with black
g.foreColor=rgb(0,0,0)
//then draw the picture in the canvas
g.drawpicture thepict,0,0
End Sub
Window1.ManualButton.Action:
Sub Action()
Dim f,ff as folderItem
Dim i as string
Dim d as date
f=GetFolderItem("MySnapshot.PICT")
ff=GetFolderItem("myidisk:Pictures:MySnapshot.JPG")
i=CapturePhoto(f.absolutepath)
thepict=f.openaspicture
canvas1.refresh
ff.SaveAsJPEG thepict
CreateWebPage
End Sub
Window1.PopupMenu1.Open:
Sub Open()
me.listindex=0
End Sub
Window1.StartButton.Action:
Sub Action()
//disable this StartButton
//and enable the StopButton
me.enabled=false
StopButton.enabled=true
//Assign the photo timer's period here
Select Case PopupMenu1.ListIndex
Case 0
// timer period is in millisecs.
//thus 1 min=60 sec.*1000
timer1.period=60000
Case 1
timer1.period=60000*5
Case 2
timer1.period=60000*10
Case 3
timer1.period=60000*30
Case 4
timer1.period=60000*60
End
//Start the photo timer
timer1.mode=2
End Sub
Window1.StopButton.Action:
Sub Action()
//disable this button
//and enable on the other
me.enabled=false
StartButton.enabled=true
//stop the timer here
timer1.mode=0
End Sub
Window1.Timer1.Action:
Sub Action()
Dim f,ff as folderItem
Dim i as string
f=GetFolderItem("MySnapshot.PICT")
ff=GetFolderItem("myidisk:Pictures:MySnapshot.JPG")
//capture the photo with our AppleScript
i=CapturePhoto(f.absolutepath)
//now open the PICT that the AppleScript created
thepict=f.openaspicture
canvas1.refresh
ff.SaveAsJPEG thepict
CreateWebPage
End Sub
Once the interface has been built and the appropriate code entered, select the File..Build Application menu and create a Macintosh version of the project called "MyVideoCapture.
Using the Software
To begin using the software, launch the "MyVideoCapture" application. Next launch Apple Video Player and turn on the camera. Adjust the software and hardware settings so that an image appears in the window. Finally, mount the iDisk disk on the desktop. This step is normally accomplished through a web interface, but it is helpful to create an alias to the iDisk once it has been mounted. Later, when you wish to mount the disk, you can simply double-click the alias. To test the application, click on the ManualButton. If everything goes smoothly, a picture will be captured, it will appear in the "MyVideoCapture" window. The picture will then be converted to a JPEG image and a web page will be created. The resulting image and html file will then be saved to the iDisk. Voila' ! Web-based video surveillance in less than an hour! If something did not work properly, do not panic. Go back and recheck your code. Are all of the file paths correct? Was all code entered correctly? Were all Properties and Methods added to the project? If all else fails, the source code can always be downloaded from MacTech's web site. The completed application is shown in action in Figure 3.
![](fig03.gif)
Figure 3.The completed application.
More Ideas
Hopefully this project will spawn some of your own ideas. For example, instead of photos the project could capture QuickTime movies for posting to the web. Be sure to take into account the length of transfer time involved with saving to an iDisk account. The current project worked fine over a 33.6 modem, but would likely fail if QuickTime movies were involved. It would simply take longer than one minute to transfer a QuickTime movie. In this instance, perhaps a video capture time of fifteen minutes would work better.
Another thing to consider is that iDisks mounted to the desktop will automatically unmount after a period of inactivity in the neighborhood of five minutes. If the application is set to take photos every thirty minutes, the disk might be unmounted by the time the transfer is to take place. One possible work around is to incorporate another timer into the project. The timer would be set to repeat infinitely at a rate of about four minutes. Every four minutes, the timer could create or destroy a the same text file repeatedly on the iDisk. This would ensure that the iDisk stays mounted.
Besides posting photos to the web, the application could easily be altered for fun stop-motion movies. Point the camera out the window, set the timer, and start recording. At the end of the day import the folder full of photos and create a QuickTime movie. This will no doubt amaze and impress your friends!
Conclusion
This article attempted to show how well REALbasic functions as a rapid development environment. Using existing Apple technologies together with a home camcorder and a copy of REALbasic, we were able to construct a functional web camera with features matching some commercial products. Another interesting point to mention is how nicely REALbasic compliments AppleScript. AppleScript could have been used exclusively to do much of this project, but REALbasic makes it very easy to construct an interface and control the AppleScript with much less code.
References
Erick J. Tejkowski is a web developer with The Zipatoni Company in St. Louis, Missouri. He can be reached by email at ejt@norcom2000.com.