应用程序减速并停止响应尝试加载相机(Java,Libgdx)

时间:2017-03-18 05:20:02

标签: java android eclipse libgdx android-camera

我正在制作一个带摄像头集成的libgdx应用程序。在挣扎了大约一周后设置相机(按照this指南)后,我陷入了一个功能调用。当我打开应该包含相机的游戏屏幕时,应用程序会变慢并停止应答。 Logcat没有错误日志。这是问题所在:

在AndroidDeviceCamera:

    activity.setFixedSize(1600,1200);

在AndroidLauncher:

    public void setFixedSize(int width, int height) {
        if (graphics.getView() instanceof SurfaceView) {
            SurfaceView glView = (SurfaceView) graphics.getView();
            glView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
            glView.getHolder().setFixedSize(width, height);
        }
    }

完整代码:

AndroidLauncher:

    package com.temp.name;

    import com.badlogic.gdx.backends.android.AndroidApplication;
    import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
    import com.temp.name.tools.DeviceCamera;

    import android.graphics.PixelFormat;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.SurfaceView;

    public class AndroidLauncher extends AndroidApplication{

        @Override
        protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();

        config.r = 8;
        config.g = 8;
        config.b = 8;
        config.a = 8;

        DeviceCamera deviceCamera = new AndroidDeviceCamera(this);
        initialize(new TempName(deviceCamera), config);
    }

    public void post(Runnable r) {
    handler.post(r);
    }

    public void setFixedSize(int width, int height) {
        if (graphics.getView() instanceof SurfaceView) {
            SurfaceView glView = (SurfaceView) graphics.getView();
            glView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
            glView.getHolder().setFixedSize(width, height);
        }
    }
}

对于其他课程,我正在遵循指南,虽然纠正了LibGdx的更新(因为指南是在2013/12/30编写的,现在有些命令如cfg.useGL20已被弃用)。

此外,我将deviceCamera实例从AndroidLauncher传递到主游戏类(TempName),然后从一个屏幕传递到另一个屏幕,直到调用相机的屏幕。顺便说一句,相机以这种方式调用:

    //In the screen that should have the camera, inside the render() method
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    if (deviceCamera != null) {
        deviceCamera.prepareCameraAsync(); //deviceCamera here and below is from the interface DeviceCamera
        deviceCamera.startPreview();
    }
    if (Gdx.input.isTouched()) {
        if (TempName.mode == Mode.normal) {
            TempName.mode = Mode.prepare;
            if (deviceCamera != null) {
                deviceCamera.prepareCameraAsync();
            }
        }
    } else { // touch removed
        if (TempName.mode == Mode.preview) {
            TempName.mode = Mode.takePicture;
        }
    }

    if (TempName.mode == Mode.takePicture) {
        Gdx.gl20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        if (deviceCamera != null) {
            deviceCamera.takePicture();
        }
        TempName.mode = Mode.waitForPictureReady;
    } else if (TempName.mode == Mode.waitForPictureReady) {
        Gdx.gl20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);          
    } else if (TempName.mode == Mode.prepare) {
        Gdx.gl20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        if (deviceCamera != null) {
            if (deviceCamera.isReady()) {
                deviceCamera.startPreviewAsync();
                TempName.mode = Mode.preview;
            }
        }
    } else if (TempName.mode == Mode.preview) {
        Gdx.gl20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    } else { // TempName.mode = normal
        Gdx.gl20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    }

TempName类中的枚举:

    public enum Mode {
       normal,
       prepare,
       preview,
       takePicture, 
       waitForPictureReady, 
}

核心项目的DeviceCamera接口:

package com.temp.name.tools;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;

public interface DeviceCamera {
    void prepareCamera();

    void startPreview();

    void stopPreview();

    void takePicture();

    byte[] getPictureData();

    void startPreviewAsync();

    void stopPreviewAsync();

    byte[] takePictureAsync(long timeout);

    void saveAsJpeg(FileHandle jpgfile, Pixmap cameraPixmap);

    boolean isReady();

    void prepareCameraAsync();
}

那么,是什么原因造成减速以及如何解决? 当我刚开始使用Libgdx时,我为愚蠢的错误道歉。此外,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

这可能是一个很长的镜头,但我在非游戏应用程序中使用它来从相机获取图片。

首先,确保您有权使用相机。注意:您不能只在清单文件中指定权限,您必须使用对话框专门请求权限。 (如果您需要此代码,请告诉我。)

像这样启动相机意图:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);   
startActivityForResult(cameraIntent, Constants.CAMERA_REQUEST);

然后,您要覆盖onActivityResult()方法以获取请求。当用户完成拍照时,将调用此方法。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (requestCode == Constants.CAMERA_REQUEST && resultCode == RESULT_OK) {
           Bitmap photo = (Bitmap) data.getExtras().get("data");
     }
}

我不确定你的游戏是如何布局的,因为这些方法必须从Activity的上下文运行,所以你可能已经将相机请求传递给了更高级别的课程,然后将结果传回给任何一个班级需要它。

相关问题