我正在构建我的第一个应用程序,我希望在按下按钮并且手机不移动时获取手机的方向。用户可以看到后置摄像头的视图,指向然后按下按钮并获得角度。
当我在onSensorChanged
中使用它时,代码似乎正常工作(我使用的是来自finding orientation using getRotationMatrix() and getOrientation()的示例代码),但是当我在'onClick'中使用它时崩溃。
我想获得其他计算的音高值(我做了音高全局变量)。
这是我的onClick
代码:
public void onClick(View arg0)
{
camera.takePicture(myShutterCallback,
myPictureCallback_RAW, myPictureCallback_JPG);
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
gravity = event.values.clone();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
magnet = event.values.clone();
break;
}
if(gravity!=null && magnet!=null) {
rotMatrix = SensorManager.getRotationMatrix(inR, I, gravity, magnet);
SensorManager.remapCoordinateSystem(inR,
SensorManager.AXIS_X, SensorManager.AXIS_Z, outR);
SensorManager.getOrientation(outR, orientVals);
float azimuth = orientVals[0]*rad2deg;
pitch = orientVals[1]*rad2deg;
float roll = orientVals[2]*rad2deg;
TextView rezDisplay = (TextView) findViewById(R.id.finalRezult);
rezDisplay.setText("Atstumas iki objekto yra:" + pitch + "m");
}
}
任何建议,链接,示例或内容都会很棒。
编辑:
我添加了整个班级代码。
import java.io.IOException;
import com.mindjack.measuremydistance.SensorEvnt.mSensorEventListener;
import android.app.Activity;
//import android.content.Intent;
import android.content.Context;
import android.content.pm.ActivityInfo;
//import android.graphics.Bitmap;
//import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;
public class AndroidCamera extends Activity implements SurfaceHolder.Callback
{
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;
TextView display, display1;
float[] magnet = new float[3];
float[] gravity = new float[3];
SensorEvent event;
boolean rotMatrix;
float[] outR = new float[16];
float[] inR = new float[16];
float[] I = new float[16];
float[] orientVals = new float[3];
final float pi = (float) Math.PI;
final float rad2deg = 180/pi;
float pitch;
SensorManager mSensorManager;
/** Called when the activity is first created. */
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.andcam);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.control, null);
LayoutParams layoutParamsControl
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
buttonTakePicture.setOnClickListener(new Button.OnClickListener() {
// TODO Auto-generated method stub
@Override
public void onClick(View arg0) {
camera.takePicture(myShutterCallback,
myPictureCallback_RAW, myPictureCallback_JPG);
switch (event.sensor.getType()){
case Sensor.TYPE_ACCELEROMETER:
gravity = event.values.clone();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
magnet = event.values.clone();
break;
}
//if(gravity!=null && magnet!=null){
rotMatrix = SensorManager.getRotationMatrix(inR,
I, gravity, magnet);
SensorManager.remapCoordinateSystem(inR,
SensorManager.AXIS_X, SensorManager.AXIS_Z, outR);
SensorManager.getOrientation(outR, orientVals);
float azimuth = orientVals[0]*rad2deg;
pitch = orientVals[1]*rad2deg;
float roll = orientVals[2]*rad2deg;
// }
TextView rezDisplay = (TextView) findViewById(R.id.finalRezult);
rezDisplay.setText("Atstumas iki objekto yra:" + pitch + "m");
}
});
}
ShutterCallback myShutterCallback = new ShutterCallback() {
@Override
public void onShutter() {
// TODO Auto-generated method stub
}
};
PictureCallback myPictureCallback_RAW = new PictureCallback() {
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
}
};
PictureCallback myPictureCallback_JPG = new PictureCallback() {
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
//Bitmap bitmapPicture
//= BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
}
};
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
// TODO Auto-generated method stub
if(previewing) {
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
因为我是新手,可能会有一些愚蠢的错误,但我无法弄清楚这种情况。 LogCat说它在第84行崩溃。我试图将第84行中的部分取出到91然后它不会崩溃,但也会在音高值上给出0。
答案 0 :(得分:0)
您的event
字段从未初始化,在尝试在switch语句中访问时,您必须获得NullPointerException
。
您应该实施SensorEventListener
并在onSensorChanged()
中获取活动,否则您将永远不会拥有这些值。
但是,请勿直接存储SensorEvent
,因为SensorManager
会重复使用该对象。只要在onSensorChanged()
中获取传感器的值,就必须将传感器的值复制到其他字段(例如switch语句中的变量)。然后,您可以在onClick()
。
答案 1 :(得分:0)
我添加了onSensorChanged并实时计算值,所以现在一切似乎都没问题。谢谢您的帮助。