我提出帶攝像頭集成libgdx應用。經過大約一週的時間來設置相機(下面的this指南)後,我陷入了一個函數調用。當我打開應該包含相機的遊戲屏幕時,該應用程序變慢並停止應答。 Logcat沒有錯誤日誌。這裏的問題是:應用程序變慢,停止響應試圖加載攝像頭(Java中,Libgdx)
在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時,我爲愚蠢的錯誤道歉。此外,任何幫助將不勝感激。
我很抱歉我遲到的回覆。我在回答之前試圖做到這一點。我照你說的做了,它工作正常!謝謝! – user140561
沒問題。很高興工作! – wdavies973