当用户拍照时,我正在尝试获取用户手机的方位。然而,当我将方位角放到屏幕上只是为了调试目的时,它总是回到0.0。我是个使用传感器的新手,所以我正在尝试解决这个问题。我的代码是
private void dispatchTakePictureIntent() {
double latitude, longitude;
if (mGpsLocationTracker.canGetLocation()) {
latitude = mGpsLocationTracker.getLatitude();
longitude = mGpsLocationTracker.getLongitude();
} else {
latitude = 0;
longitude = 0;
}
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
Toast.makeText(this, "" + latitude + ", " + longitude, Toast.LENGTH_SHORT).show();
getDirection ();
Toast.makeText(this, Float.toString(degree), Toast.LENGTH_SHORT).show();
}
public void getDirection() {
SensorManager mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
if(mySensors.size() > 0){
mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_UI);
}
}
/*
DOES NOT WORK. NEED TO FIX
*/
private SensorEventListener mySensorEventListener = new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float radianValue = Math.round(event.values[0]);
degree = (float) Math.toDegrees(radianValue);
if (degree < 0.0f) {
degree += 360.0f;
}
}
};
public void takePicture (View view) {
dispatchTakePictureIntent();
}有人能帮我纠正我做错了什么吗?
发布于 2015-09-16 00:38:16
方向传感器不会以度为单位返回角度吗?假设为弧度并转换为度数。考虑到这一点,它仍然不应该给你0。
发布于 2015-09-16 11:45:52
TYPE_ORIENTATION会折旧,如果设备不是平坦的,则不会给出正确的结果。您需要先使用TYPE_ACCELEROMETER和TYPE_MAGNETIC_FIELD获取rotation matrix,然后调用remapCoordinateSystem,然后再调用getOrientation。
https://stackoverflow.com/questions/32590865
复制相似问题