![](/img/trans.png)
[英]Java: Writing to socket with DataOutputStream slows down, then stops
[英]App slows down and stops responding trying to load camera(Java, Libgdx)
我正在制作具有摄像头集成功能的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);
}
}
完整代码:
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时,我为愚蠢的错误表示歉意。 此外,任何帮助将不胜感激。
这可能是一个长镜头,但是我在非游戏应用程序中使用了它来从相机获取图片。
首先,请确保您有权使用相机。 注意:您不仅可以在清单文件中指定权限,还必须通过对话框专门请求权限。 (让我知道您是否需要代码)。
像这样启动相机意图:
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
的上下文中运行,所以您可能已经将相机请求传递到了更高级别的类,然后才传递了结果回到任何需要的班级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.