Java Reference
In-Depth Information
To capture the image, the
capture
method invokes the
VideoControl
instance's
getSnapshot
method. This method takes the name of the desired image format and
encoding options and returns the image encoded in the desired image format as a
byte
array. By default,
getSnapshot
uses the system's default encoding, which is the first field in
the value of the
video.snapshot.encoding
system property.
You can specify the encoding options as a URL-encoded string, with the following
arguments:
•
encoding
: Indicates the desired encoding, such as JPEG, PNG, or GIF
•
width
: Indicates the desired width for the encoded image in pixels
•
height
: Indicates the desired height for the encoded image in pixels
For example, you might pass
encoding=jpeg&width=160&height=120
to
getSnapshot
to
receive a stamp-sized image encoded as a JPEG.
getSnapshot
can throw an exception; expect a
SecurityException
if the application
does not have permission to take the snapshot, or a
MediaException
if the desired format
isn't supported or if the
Player
doesn't support taking snapshots. You might also get an
IllegalStateException
, but only if you try to invoke
getSnapshot
on a
VideoControl
instance whose
initDisplayMode
hasn't been called.
Unfortunately, while the code itself is simple, in practice, this operation is fraught
with difficulties. First are the obvious problems you might expect: your application might
run on a device without an imaging sensor or without integration between the MMAPI
and the imaging sensor. That's usually easy to find out; as you remember from Table 16-2,
a call to
System.getProperty
passing the value
supports.video.capture
tells you if the tar-
get device can in fact perform this operation. However, you may discover other problems,
including the following:
•
Different locators
: Some devices reserve the locator
capture://image
for capturing
still images, and
capture://video
for capturing video using a
RecordControl
instance
(I say more about that in a moment).
•
Rendering a preview of the camera viewfinder doesn't operate predictably
: On some
devices, you can only obtain a camera viewfinder if you use the
initDisplayMode
to
specify a GUI primitive using
VideoControl.USE_GUI_PRIMITIVE
. Other devices only
work if you use
VideoControl.USE_DIRECT_VIDEO
and provide a
Canvas
object on
which the MMAPI draws the view that the sensor provides.
•
Finding a common encoding scheme
: Don't build your application assuming that
all Java ME devices will provide the same image format or resolution, or the format
or resolution that you request. Some devices can encode in multiple formats, and
some can't. Some can scale the encoded image, and some can't—and some will
only scale your image if you provide
both
the width and height parameters.