应用程序在尝试加载相机时变慢并停止响应(Java、Libgdx)
App slows down and stops responding trying to load camera(Java, Libgdx)
我正在制作一个 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);
}
}
完整代码:
安卓启动器:
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);
}
}
}
对于其他 classes,我正在遵循指南,尽管更正了 LibGdx 的更新(因为指南是在 2013/12/30 编写的,一些命令如 cfg.useGL20现在已弃用)。
此外,我将 deviceCamera 实例从 AndroidLauncher 传递到主游戏 class(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 中的枚举 class:
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");
}
}
我不确定你的游戏是如何布局的,因为这些方法必须来自 Activity
的上下文 运行,所以你可能已经通过了相机请求更高级别 class,然后将结果传回给 class 需要它的任何人。
我正在制作一个 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);
}
}
完整代码:
安卓启动器:
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);
}
}
}
对于其他 classes,我正在遵循指南,尽管更正了 LibGdx 的更新(因为指南是在 2013/12/30 编写的,一些命令如 cfg.useGL20现在已弃用)。
此外,我将 deviceCamera 实例从 AndroidLauncher 传递到主游戏 class(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 中的枚举 class:
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");
}
}
我不确定你的游戏是如何布局的,因为这些方法必须来自 Activity
的上下文 运行,所以你可能已经通过了相机请求更高级别 class,然后将结果传回给 class 需要它的任何人。