Requirement:
I need to detect if the camera is parallel to surface before I allow user to click picture using camera
I have already read Android developers Sensor manager notes and tried the sample code
I already have a custom camera for my app
edit1: below code never fires
public void onSensorChanged(SensorEvent event) {
}
edit2 my code
public class OrientationDemo extends Activity implements
SensorEventListener {
TextView mOrientationData;
private SensorManager mSensMan;
private float mAzimuth;
private float[] mGravs = new float[3];
private float[] mGeoMags = new float[3];
private float[] mOrientation = new float[3];
private float[] mRotationM = new float[9]; // Use [16]
to co-operate with android.opengl.Matrix
private float[] mRemapedRotationM = new float[9];
private boolean mFailed;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// I'd like to actually see something so let's have a view:
mOrientationData = new TextView(this);
setContentView(mOrientationData, new
LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
// Initiate the Sensor Manager and register this as Listener for
the required sensor types:
// TODO: Find how to get a SensorManager outside an Activity, to
implement as a utility class.
mSensMan = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensMan.registerListener(this,
mSensMan.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), //
Anonymous Sensors- no further use for them.
SensorManager.SENSOR_DELAY_UI);
mSensMan.registerListener(this,
mSensMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do nothing
}
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
/*
* NOTE: The data must be copied off the event.values
* as the system is reusing that array in all SensorEvents.
* Simply assigning:
* mGravs = event.values won't work.
*
* I use a member array in an attempt to reduce garbage production.
*/
System.arraycopy(event.values, 0, mGravs, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
// Here let's try another way:
for (int i=0;i<3;i++) mGeoMags[i] = event.values[i];
break;
default:
return;
}
if (SensorManager.getRotationMatrix(mRotationM, null, mGravs,
mGeoMags)){
// Rotate to the camera's line of view (Y axis along the camera's
axis)
// TODO: Find how to use android.opengl.Matrix to rotate to an
arbitrary coordinate system.
SensorManager.remapCoordinateSystem(mRotationM,
SensorManager.AXIS_X,
SensorManager.AXIS_Z, mRemapedRotationM);
SensorManager.getOrientation(mRemapedRotationM, mOrientation);
onSuccess();
}
else onFailure();
}
void onSuccess(){
if (mFailed) mFailed = false;
// Convert the azimuth to degrees in 0.5 degree resolution.
mAzimuth = (float) Math.round((Math.toDegrees(mOrientation[0])) *2)/
2;
// Adjust the range: 0 < range <= 360 (from: -180 < range <= 180).
mAzimuth = (mAzimuth+360)%360; // alternative: mAzimuth =
mAzimuth>=0 ? mAzimuth : mAzimuth+360;
mOrientationData.setText("Azimuth= " + mAzimuth);
}
void onFailure() {
if (!mFailed) {
mFailed = true;
mOrientationData.setText("Failed to retrive rotation Matrix");
}
}
}
I need sample app that can give me rotation matrix for my device
Thanks in advance!!!
.
stackoverflow.comm
No comments:
Post a Comment