Prev: Module audio: Playing and Recording Sounds
Module cam: Onboard Camera
This module provides access to the onboard camera for still images. Pictures taken can be processed or saved by module module
graph.
Since the camera is a shared resource and consumes battery power, it must be turned on before use by cam.on and turned off afterwards by cam.off. A typical example using the camera might look as follows:
// show the available image sizes
for s in cam.sizes() do
print s
end
→ [1280,960]
[640,480]
[160,120]
// turn the camera on for 640x480 size images
cam.on(1)
// produce a dark, contrast rich picture
cam.bright(-20); cam.contrast(30)
→ 0
0
// display a view finder close to the top left corner
cam.view(10,10)
// take an image
icon=cam.take()
// turn the camera off
cam.off()
// save the image via the graph module
s=graph.size(icon); // get the image size
graph.size(s[0], s[1]); // make graph big enough
graph.put(0,0,icon); // draw the image
graph.save("keyboard.jpg") // save it
|
 | Sample m screen |
cam.bright
• function bright() → Number
• function bright(b) → Number
Gets or sets the brightness of the image taken. The brightness is a number between -100 (very dark) and 100 (very bright). Standard brightness is 0.
Without arguments, returns the currently used brightness. With one argument, returns the old brightness, and sets the new brightness to b.
Throws ErrInUse or ErrNotReady if the camera has not been turned on.
// show the view finder, increasing brightness
cam.on()
cam.view()
for b=-100 to 100 by 10 do
cam.bright(b); sleep(1000)
end;
cam.off()
|
cam.contrast
• function contrast() → Number
• function contrast(c) → Number
Gets or sets the contrast of the image taken. The contrast is a number between -100 (minimum contrast) and 100 (maximum contrast). Standard contrast is 0.
Without arguments, returns the currently used contrast. With one argument, returns the old contrast, and sets the new contrast to c.
Throws ErrInUse or ErrNotReady if the camera has not been turned on.
// show the view finder, increasing contrast
cam.on()
cam.view()
for c=-100 to 100 by 10 do
cam.contrast(c); sleep(1000)
end;
cam.off()
|
cam.index
• function index() → Number
• function index(camIndex) → Number
| | |
| Compatibility of function cam.index |
| Sony Ericsson UIQ3 phones (P990) have no public API for the front camera (index 1) | cam.sizes returns [] |
|
On devices with more than one built-in camera, selects the camera to operate on. The camera index must be greater than or equal to 0 and less than cam.count.
Without arguments, returns the index of the currently used camera. With one argument, turns the old camera off, returns the old index, and sets the new camera index.
By default, the first camera (index 0) is selected.
Throws ExcIndexOutOfRange if the camera does not exist.
// select the 2nd camera, if there is one
if cam.count> 1 then
cam.index(1)
else
print "No second camera"
end
|
cam.off
• function off() → null
Removes the view finder if it is shown, and turns the camera off. Does nothing if the camera is already off.
cam.on
• function on(sizeIndex=0, forVideo=false) → null
Turns the camera on and prepares it for taking images or recording video frames of size cam.sizes(forVideo)[sizeIndex]. If forVideo=false, the camera is set up for taking images; if forVideo=true, the camera is set up for recording videos. For video recording, see module video.
If forVideo=true, a negative sizeIndex will use a default size, which is always supported.
Throws ExcIndexOutOfRange if sizeIndex is less than 0 (if forVideo=false) or greater than cam.sizes(forVideo) - 1.
Throws ErrInUse if the camera is already on, or used by another application.
cam.sizes
• function sizes(forVideo=false) → Array
Returns the available image sizes (forVideo=false), or the available video frame sizes ((forVideo=true), as an array of arrays containing image width and image height. The actual sizes returned are device dependent.
The camera does not have to be on to obtain the image or frame sizes.
On some devices, the setting of the screen mode (ui.mode) has an effect on the available sizes. For instance, maximum resolution might only be available in landscape mode.
// still image sizes
for s in cam.sizes() do
print s
end
→ [640,480]
[320,240]
[160,120]
// video frame sizes
for s in cam.sizes(true) do
print s
end
→ [352,288]
[176,144]
[128,96]
|
cam.take
• function take() → Native Object
• function take(jpegpath, quality=75) → null
Permissions: Write(jpegpath)
| | |
| Compatibility of function cam.take |
| Sony Ericsson UIQ2 phones if cam.view has not been called. | ErrInUse |
|
Without arguments, takes an image of the configured size, brightness and contrast and returns it as an icon (see graph.icon). The icon can be saved, scaled, or analyzed using functions in module graph.
With one or two arguments, takes an image of the configured size, brightness and contrast and saves it directly to file jpegpath, compressing for the given quality. quality must be between 1 and 100. No icon is produced in this case.
Throws ErrInUse or ErrNotReady if the camera has not been turned on.
Throws ExcValueOutOfRange if the JPEG quality is out of range.
i=cam.take();
print i
→ icon@4186d8
// scale the image to one quarter and display it
graph.size(i,0.5)
→ [640,480]
graph.put(0,0,i)
graph.show()
|
 | Sample m screen |
// take an image and save it to snapshot.jpg
cam.take('snapshot.jpg')
|
cam.view
• function view(x=0, y=0, w=160, h=120) → Array
| | |
| Compatibility of function cam.view |
| Sony Ericsson UIQ3 phones blacken the entire window when showing the view finder. |
|
Shows a view finder (the image currently seen by the camera) on the screen at coordinates (x,y), in a rectangle of roughly width w and height h. (0,0) is at the upper left corner of the m application view.
Returns the actual size of the rectangle used.
Throws ErrInUse or ErrNotReady if the camera has not been turned on.
Throws ErrNotSupported if the requested view finder size is not supported.
// show the view centered on the graph view
gs=graph.size();
cam.on();
vs=cam.view();
x=math.trunc((gs[0]-vs[0])/2);
y=math.trunc((gs[1]-vs[1])/2);
// draw a frame around the view
graph.rect(x-2,y-2,vs[0]+4,vs[1]+4);
graph.show();
cam.view(x, y)
|
 | Sample m screen |
cam Constants
• const count =
The number of cameras available. On some devices, some cameras may be unaccessible via this module.
Next: Module cam2: Extended Camera Functions© 2004-2011 airbit AG, CH-8008 Zürich
Document AB-M-LIB-888