Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
应用程序在尝试加载摄像头时减慢速度并停止响应(Java、Libgdx)_Java_Android_Eclipse_Libgdx_Android Camera - Fatal编程技术网

应用程序在尝试加载摄像头时减慢速度并停止响应(Java、Libgdx)

应用程序在尝试加载摄像头时减慢速度并停止响应(Java、Libgdx),java,android,eclipse,libgdx,android-camera,Java,Android,Eclipse,Libgdx,Android Camera,我正在制作一个带有摄像头集成的libgdx应用程序。在花了大约一周的时间来设置相机后(按照指南),我被一个函数调用卡住了。当我打开包含摄像头的游戏屏幕时,应用程序会减速并停止应答。Logcat上没有错误日志。问题是: 在AndroidDeviceCamera: activity.setFixedSize(1600,1200); 在AndroidLauncher: public void setFixedSize(int width, int height) {

我正在制作一个带有摄像头集成的libgdx应用程序。在花了大约一周的时间来设置相机后(按照指南),我被一个函数调用卡住了。当我打开包含摄像头的游戏屏幕时,应用程序会减速并停止应答。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);
        }
    }
    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);
        }
    }
}
完整代码:

安德鲁伊德拉舍尔:

    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);
        }
    }
    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,我为愚蠢的错误道歉。此外,任何帮助都将不胜感激。

这可能是一个不太可能实现的目标,但我在一个非游戏应用程序中使用了它来从相机获取图片

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

启动摄影机,如下所示:

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");
     }
}

我不确定的是你的游戏是如何布局的,因为这些方法必须在
活动的上下文中运行,所以你可能会将一个摄像头请求传递给更高级别的类,然后将结果传递给需要它的类。

很抱歉我的回复太晚了。我试着在回答之前做这件事。我照你说的做了,效果很好!非常感谢。