Sensors and Camera

advertisement
CHAPTER 8
Sensors and
Camera
Chapter objectives:
• Understand Motion Sensors, Environmental
Sensors and Positional Sensors
• Learn how to acquire measurement data from
a sensor
• Explore the SensorEvent class and
SensorEventListener interface
• Create apps using the accelerometer
• Learn to utilize the built-in camera application
8.1 Sensors and Mobile Devices
• A sensor is simply a device that measures a
physical quantity, such as the tilt of a device,
or sudden movement, and converts it into a
signal that can be interpreted by an
application
• Android users typically experience sensors by
means of touchscreens, accelerometers,
gyroscopes, cameras, and GPS
8.2 Android Sensors
Motion Sensors
• The Android platform can determine which
way is down based on the accelerometer
• Android's built-in three-axis accelerometer is
often used to control context-aware elements
in an application
• The gyroscope measures the rate of rotation
around three axes
Environmental Sensors
• Measure specific environmental parameters,
such as ambient air temperature and
pressure, illumination, and humidity
• Sensors are hardware-based and are
available only if a device manufacturer has
built them into a device
• With the exception of the light sensor, which
most device manufacturers use to control
screen brightness, environment sensors are
not always available on devices.
• Positional Sensors
• A magnetometer, such as a geomagnetic
field sensor, is built into most Android
devices
• A proximity sensor is used to determine
how close an object is to the device
• Proximity data is particularly important
when creating applications that must
determine when a device is being held close
to a user’s face, such as during a phone call
8.3 Working with Sensors
• Android also has software-based sensors
• Software-based sensors imitate hardware-based
sensors by deriving their data from one or more
of the embedded physical sensorsthe
accelerometer.
• Most devices include an accelerometer and a
magnetometer
• Some devices have more than one sensor of a
given type, such as the inclusion of two gravity
sensors, each one having a different range
8.4 Coordinate System
• Defined relative to the screen of the device in
its default orientation
• As shown in Figure 8-3, the x-axis runs in the
direction of the short side of the screen
• The y-axis runs in the direction of the long
side of the screen and the z-axis points out of
the screen
• The natural orientation for many tablet
devices is landscape
• The coordinate system of the world, the
inertial frame of reference, defines the xaxis as the cross-product of the y-axis with
the z-axis
• The y-axis is tangential to the ground and
points toward the North Pole
• The z-axis points perpendicular to the
ground toward the sky
8.5 Accelerometer and Force
• Given a fixed mass, an Android device will
experience an increase in force when the
acceleration of the device is increased
• When the accelerometer measures a zero force,
the device is either still or moving at a constant
speed
• When the acceleration of the device is increased,
such as a quick jerk of the hand, the
accelerometer registers an increase in force
• The accelerometer is made up of three
accelerometers, one for each axis—x, y, and z
• Each one measures changes in velocity over
time along a linear path
• Combining all three accelerometers lets you
detect accelerated force in any direction
• While acceleration is a vector quantity, g-force
is often expressed as a scalar, with positive gforces pointing upward (indicating upward
acceleration), and negative g-forces pointing
downward.
8.6 Sensor Batching
• Hardware sensor batching is a technique that
is designed to reduce the power consumed by
sensors commonly used in fitness, location
tracking, and monitoring service applications
• By using sensor batching, hardware sensors
can collect and deliver sensor-related data
more efficiently in batches, rather than
individually
• Consider an accelerometer driven application
used for healthcare purposes by tracking a
user’s physical activity level
• To track steps, the application must capture
readings from the accelerometer and
distinguish the step pattern
• Such an application needs to run in the
background, counting steps and tracking
location remaining in an active state, which
can cause the battery life to to lose power at a
faster rate
• Sensor batching provides optimization to
decrease power consumed by ongoing sensor
activities
8.7 Composite Sensors
8.8 Camera
• Not all Android devices contain a camera
• The necessity of a camera on a device was
relaxed when Android began to be used on settop boxes
• Set-top boxes are devices that allow users to
listen to music, view content, and play games on
their television sets.
• To prevent an application that requires a camera
from being installed on devices that do not
feature a camera component, a <uses-feature>
tag should be placed in the AndroidManifest file
• It is important to check the availability of
the camera within an activity that uses a
camera feature
• The existence of camera components can be
verified by using PackageManager
• A front-facing camera may also be available
on a device because strategically it is highly
suitable for making video callsThe
availability of camera features can be
identified with three constants:
• FEATURE_CAMERA_ANY: The device has
at least one camera or can support an
external camera being connected to it.
• FEATURE_CAMERA: The device has a
camera facing away from the screen. This
is also referred to as a rear-facing camera.
• FEATURE_CAMERA_FRONT: The device
has a front facing camera.
8.9 Manipulating Photos
•
Pure red contains the full intensity of red (255) and no intensity of
green and blue. R: 255 G: 0 B: 0
•
Pure yellow contains the full intensity of red and green, but not
blue. R: 255, G: 255, B: 0
•
Pure white is the combination of all three colors at full intensity.
R: 255, G: 255, B: 255
•
Pure black contains the weakest intensity of wavelengths that
correspond to red, green, and blue. R: 0, G: 0, B: 0
Added filters
Download