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]
// turn the camera on for 640x480 size images[640,480] [160,120] cam.on(1) // produce a dark, contrast rich picture cam.bright(-20); cam.contrast(30) → 0
// display a view finder close to the top left corner0 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 |
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() |
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() |
| ||||||
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 |
Removes the view finder if it is shown, and turns the camera off. Does nothing if the camera is already off.
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.
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]
// video frame sizes[320,240] [160,120] for s in cam.sizes(true) do print s end → [352,288]
[176,144] [128,96] |
| ||||||
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 itgraph.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') |
| ||||||
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 |