Search code examples
androidandroid-sensorssensormanager

Take picture when phone is straight


I just want to ask a question because I've searched through Google and I can't seem to find anything useful, and read about this and this.

The Question is, can I take a picture from camera only when the phone is straight using sensors in android device?

Just looking for advice on how to do it?


Solution

  • You should be able to use the orientation sensor and SensorEvents.

    The orientation sensor lets you monitor the position of a device relative >to the earth's frame of reference (specifically, magnetic north).

    The orientation sensor derives its data by using a device's geomagnetic >field sensor in combination with a device's accelerometer. Using these two >hardware sensors, an orientation sensor provides data for the following >three dimensions:

    Azimuth (degrees of rotation around the z axis). This is the angle between magnetic north and the device's y axis. For example, if the device's y axis is aligned with magnetic north this value is 0, and if the device's y axis is pointing south this value is 180. Likewise, when the y axis is pointing east this value is 90 and when it is pointing west this value is 270.

    Pitch (degrees of rotation around the x axis). This value is positive when the positive z axis rotates toward the positive y axis, and it is negative when the positive z axis rotates toward the negative y axis. The range of values is 180 degrees to -180 degrees.

    Roll (degrees of rotation around the y axis). This value is positive when the positive z axis rotates toward the positive x axis, and it is negative when the positive z axis rotates toward the negative x axis. The range of values is 90 degrees to -90 degrees.

    Using these values you should be able to tell when the phone is "straight" in any position. There are examples on the page under the "Using the orientation sensor" header : http://developer.android.com/guide/topics/sensors/sensors_position.html#sensors-pos-orient

    Also look at http://developer.android.com/reference/android/hardware/SensorEvent.html which shows how to use the sensor events