我们怎样才能测量物体和android手机相机之间的距离

我想计算相机和识别对象之间的距离。为此,我尝试了很多方法,我尝试使用加速计find对象和相机之间的angular度,然后使用

d = h * tan a

h是从底部算起的高度,一般是1.4

我试图用get方法来计算angular度。 请让我知道我在哪里做错了。 已经有两天多的时间了,我一直在努力满足这个要求。 我们已经研究了Android Store上可用的各种Camera应用程序,并试图了解相同的function,但没有任何结果。

mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); accSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); magnetSensor = mSensorManager .getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) gravity = event.values; if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) geoMagnetic = event.values; if (gravity != null && geoMagnetic != null) { float R[] = new float[9]; float I[] = new float[9]; boolean success = SensorManager.getRotationMatrix(R, I, gravity, geoMagnetic); if (success) { /* Orientation has azimuth, pitch and roll */ float orientation[] = new float[3]; //SensorManager.remapCoordinateSystem(R, 1, 3, orientation); SensorManager.getOrientation(R, orientation); azimut = 57.29578F * orientation[0]; pitch = 57.29578F * orientation[1]; roll = 57.29578F * orientation[2]; } } } captureButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // get an image from the camera double d = (Math.tan(Math.toRadians(Math.abs(pitch))) * sensorHeight); Toast.makeText( getApplicationContext(), "Distance = " + String.valueOf(d) + "m Angle = " + String.valueOf(Math.toRadians(Math.abs(pitch))), Toast.LENGTH_LONG).show(); } }); protected void onResume() { super.onResume(); mSensorManager.registerListener(this, accSensor, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener(this, magnetSensor, SensorManager.SENSOR_DELAY_NORMAL); } 

Solutions Collecting From Web of "我们怎样才能测量物体和android手机相机之间的距离"

你的getRotationMatrix可能返回false! 你应该把值复制到你自己的载体中,这样它们就不会混淆了! 使用clone()方法来做到这一点!

  if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) gravity = event.values.clone(); if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) geoMagnetic = event.values.clone(); 

使用你的代码加上这个改变,我能够得到方位angular/俯仰angular/滚转angular值,没有这个改变,成功标志返回false:

 Log.d("a", "orientation values: " + azimut + " / " + pitch + " / " + roll); 05-21 16:07:55.743: D/a(29429): orientation values: 77.71578 / 43.352722 / -152.39603 05-21 16:07:55.883: D/a(29429): orientation values: 175.26134 / 23.031355 / -148.72844 05-21 16:07:56.793: D/a(29429): orientation values: -146.3089 / 4.1098075 / -14.46417 

如果您将手机保持纵向模式,则应使用PITCH值;如果手机处于横向模式,则应使用ROLL值。

如果您将手机保持在1.4高度,那么您将拥有:

 float dist = Math.abs((float) (1.4f * Math.tan(pitch * Math.PI / 180))); 

请注意,您应该在Math.tan函数上使用RADIANS而不是DEGREES。

我在这里testing和价值似乎是有效的!

最终的代码是

 mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); accSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); magnetSensor = mSensorManager .getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) gravity = event.values; if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) geoMagnetic = event.values; if (gravity != null && geoMagnetic != null) { float R[] = new float[9]; float I[] = new float[9]; boolean success = SensorManager.getRotationMatrix(R, I, gravity, geoMagnetic); if (success) { /* Orientation has azimuth, pitch and roll */ float orientation[] = new float[3]; //SensorManager.remapCoordinateSystem(R, 1, 3, orientation); SensorManager.getOrientation(R, orientation); azimut = 57.29578F * orientation[0]; pitch = 57.29578F * orientation[1]; roll = 57.29578F * orientation[2]; } } } captureButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // get an image from the camera float d = Math.abs((float) (1.4f * Math.tan(pitch * Math.PI / 180))); Toast.makeText( getApplicationContext(), "Distance = " + String.valueOf(d) + "m Angle = " + String.valueOf(Math.toRadians(Math.abs(pitch))), Toast.LENGTH_LONG).show(); } }); protected void onResume() { super.onResume(); mSensorManager.registerListener(this, accSensor, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener(this, magnetSensor, SensorManager.SENSOR_DELAY_NORMAL); }