使用相机preVIEW时获取GPS数据问题相机、数据、问题、GPS

2023-09-07 16:58:05 作者:演妓派

我试着写一些AR应用程序。现在我写了一些code,它显示摄像头prewiew和获取数据的传感器设备(acceleromentr,康巴丝,GPS reciver)。

I tried to write some AR app. For now I wrote some code that display camera prewiew and gets data from sensors on the device (acceleromentr, compas, gps reciver).

当我运行code。在单独的应用程序(如摄像头preVIEW为一个应用程序,应用程序,获取GPS数据作为第二)一切正常。但是,当我试图整合这两个模块 - 全球定位系统停止工作;它看起来像听者没有得到任何数据。您是否有一些类似的问题?

When I run the code in separate app (like camera preview as one app and application that gets gps data as a second) everything is OK. But when I try to integrate this two modules - GPS stop working; it looks like the listener doesn't get any data. Did You had some similar problems?

在code是这样的:

public void onResume()
{
    super.onResume();

    mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                                            1000, 0.0f, 
                                            mLocationListener);
}


public void onPause()
{
    super.onPause();
    mLocationManager.removeUpdates(mLocationListener);
}


private LocationListener mLocationListener = new LocationListener()
{

    public void onLocationChanged(Location pLocation) {
        double lLatitude = pLocation.getLatitude();
        double lLongitude = pLocation.getLongitude();
        mGpsTextView.setText ("Longitude" + Double.toString(lLongitude) + " Latitude: " + Double.toString(lLatitude));
    }

    public void onProviderDisabled(String pProvider) {
        mGpsTextView.setText ("Provider disabled");
    }

    public void onProviderEnabled(String pProvider) {
        mGpsTextView.setText ("Provider enabled");
    }

    public void onStatusChanged(String pProvider, int pStatus, Bundle pExtras) {
        switch (pStatus)
        {
         case LocationProvider.OUT_OF_SERVICE:
             mGpsTextView.setText("GPS out of service");
             break;
         case LocationProvider.TEMPORARILY_UNAVAILABLE:
             mGpsTextView.setText("GPS temorarily unawalible");
             break;
         case LocationProvider.AVAILABLE:
             mGpsTextView.setText("GPS avalible");
             break;
         default:
             mGpsTextView.setText("EEE");
        }

   }

};

我试图在注册/注销监听器的onCreate /的onPause但行为是一样的。

I tried to register/unregister listener in onCreate/onPause but the behaviour is the same.

相机preVIEW code是这样的:

The cameraPreview code looks like:

私人SurfaceHolder.Callback mSurfaceHolderCallback =新SurfaceHolder.Callback()     {         私人相机mCamera;

private SurfaceHolder.Callback mSurfaceHolderCallback = new SurfaceHolder.Callback() { private Camera mCamera;

    public void surfaceCreated (SurfaceHolder pSurfaceHolder)
    {
        stopAndReleaseCamera();
        mCamera = Camera.open();
        try {
            mCamera.setPreviewDisplay(pSurfaceHolder);
        }
        catch (Exception imLazy) {
            stopAndReleaseCamera();
            imLazy.printStackTrace();
        }
    }

    public void surfaceChanged (SurfaceHolder pSurfaceHolder, int pFormat, int pWidth, int pHeight)
    {
        Parameters lCameraParams = mCamera.getParameters();
        lCameraParams.setPreviewSize (pWidth, pHeight);
        mCamera.setParameters (lCameraParams);
        mCamera.startPreview();
    }

    public void surfaceDestroyed (SurfaceHolder pSurfaceHolder)
    {
        stopAndReleaseCamera();
    }

    private void stopAndReleaseCamera()
    {
        if (mCamera != null)
        {
            try
            {
                mCamera.stopPreview();
                mCamera.release();
            }
            catch (Exception imLazy)
            {
                //ignore
                imLazy.printStackTrace();
            }
            mCamera = null;
        }
    }
};

这是注册的onCreate:

And this is registered in onCreate:

    SurfaceView lSurfaceView = (SurfaceView)findViewById(R.id.CameraSurface);
    lSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    lSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
    lSurfaceView.getHolder().addCallback (mSurfaceHolderCallback);

R.id.C​​ameraSurface在layout.xml定义为

R.id.CameraSurface is defined in layout.xml as

SurfaceView android:id="@+id/CameraSurface"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"

任何想法有什么不好?

Any ideas what is wrong?

在code的三个不同的手机进行测试

The code was tested on three different phones

推荐答案

如果你需要的,你必须确保该LocationListener的不释放位置更新的稳定和连续流。当听众内死亡,将被检测到(通过远程接口)通过系统的LocationManager,并且如果没有其他LocationListeners侦听,系统LocationManager将停止!其结果是,对GPS具有AQUIRE一个新的修补。正如亨利提到:服务会做的伎俩。

If you need a steady and continous stream of location updates you have to make sure that the LocationListener is NOT deallocated. When the listener dies it will be detected (through remote interface) by system the LocationManager, and if no other LocationListeners is listening, the system LocationManager will stop! The result is that the GPS has to aquire a new fix. As Henry mentioned: a service will do the trick.

如果你有一个物理的手机,您可以通过运行您的GPS code检查行为和旋转手机。

If you have a physical phone you can check the behavior by running your GPS code and rotate the phone.