0

我正在构建我的第一个应用程序,我想在按下按钮并且手机不动时获得手机的方向。用户可以看到后置摄像头的视图,指向然后按下按钮,应该得到角度。

当我在中使用它时,该代码似乎可以正常工作onSensorChanged(我将其用作示例代码:使用 getRotationMatrix() 和 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");
    }
}

任何建议、链接、示例或 tuts 都会很棒。

编辑:

我添加了整个课程代码。

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。

4

2 回答 2

0

您的event字段从未初始化,您必须NullPointerException在 switch 语句中尝试访问它。

您应该在 中实现SensorEventListener并获取事件onSensorChanged(),否则您将永远不会拥有这些值。

但是,不要SensorEvent直接存储 ,因为对象会被SensorManager. 将传感器的值复制到其他字段(如 switch 语句中的变量)后,您必须将它们复制到onSensorChanged(). 然后,您可以在onClick().

于 2013-05-16T18:42:03.620 回答
0

我添加了 onSensorChange 并实时计算值,所以现在一切正常。谢谢您的帮助。

于 2013-05-16T21:18:12.147 回答