![](/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.