[英]App slows down and stops responding trying to load camera(Java, Libgdx)
I am making a libgdx app with camera integration. 我正在制作具有摄像头集成功能的libgdx应用。 After struggling for about a week to set the camera(following this guide), I got stuck at a function call.
在努力设置相机约一个星期后(按照本指南操作),我陷入了函数调用的困境。 When I open the game screen that should contain the camera, the app slows down and stop answering.
当我打开应该包含相机的游戏屏幕时,该应用程序速度变慢并停止应答。 There is no error log at Logcat.
Logcat上没有错误日志。 Here is the problem:
这是问题所在:
At AndroidDeviceCamera: 在AndroidDeviceCamera:
activity.setFixedSize(1600,1200);
At AndroidLauncher: 在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);
}
}
The full code: 完整代码:
AndroidLauncher: 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);
}
}
}
For other classes, I am following the guide, albeit correcting for the updates of LibGdx(as the guide was written in 2013/12/30, some commands like cfg.useGL20 are deprecated nowadays). 对于其他课程,尽管正在纠正LibGdx的更新,但我仍在遵循该指南(因为该指南于2013/12/30编写,如今已弃用了cfg.useGL20之类的某些命令)。
Furthermore, I passed the deviceCamera instance from AndroidLauncher to the main game class(TempName) and then from one screen to another screen until the screen that the camera is called. 此外,我将deviceCamera实例从AndroidLauncher传递到了主游戏类(TempName),然后从一个屏幕传递到另一个屏幕,直到调用相机的屏幕为止。 By the way, the camera is called in this way:
顺便说一下,相机是这样调用的:
//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);
}
enum in TempName class: TempName类中的枚举:
public enum Mode {
normal,
prepare,
preview,
takePicture,
waitForPictureReady,
}
DeviceCamera Interface at core project: 核心项目的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();
}
So, what is causing the slow down and how to solve it? 那么, 是什么原因导致速度下降以及如何解决呢? As I just started with Libgdx, I apologize for stupid errors.
当我刚开始使用Libgdx时,我为愚蠢的错误表示歉意。 Also, any help would be greatly appreciated.
此外,任何帮助将不胜感激。
This might be a long shot, but I used this in a non-game app for getting pictures from the camera. 这可能是一个长镜头,但是我在非游戏应用程序中使用了它来从相机获取图片。
First things first, make sure you have permission to use the camera. 首先,请确保您有权使用相机。 Note: You can't just specify the permission in the manifest file, you have to specifically request permission to it with a dialog.
注意:您不仅可以在清单文件中指定权限,还必须通过对话框专门请求权限。 (Let me know if you need code for this).
(让我知道您是否需要代码)。
Start a camera intent like so: 像这样启动相机意图:
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);
Then, you'll want to override the onActivityResult()
method to get a request. 然后,您将要重写
onActivityResult()
方法以获取请求。 This method will be called when the user finishes taking a picture. 用户完成拍照后将调用此方法。
@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");
}
}
The thing I'm not sure about is how your game is layed out, since these methods have to be run from the context of an Activity
, so you might have pass a camera request up to a higher level class and then have the result passed back down to whichever class needs it. 我不确定的是您的游戏布局如何,因为这些方法必须从
Activity
的上下文中运行,所以您可能已经将相机请求传递到了更高级别的类,然后才传递了结果回到任何需要的班级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.