2017-03-18 76 views
0

我提出帶攝像頭集成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時,我爲愚蠢的錯誤道歉。此外,任何幫助將不勝感激。

回答

0

這可能是一個長鏡頭,但我用這個在非遊戲應用從相機獲取的圖片。第一

第一件事,請確保您有使用相機的權限。注意:您不能只在清單文件中指定權限,您必須通過對話框專門請求權限。 (讓我知道你是否需要這樣的代碼)。

啓動相機的意圖,像這樣:

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的上下文中運行,所以你可能有過一個相機請求直到上級的事然後將結果傳回給任何需要它的類。

+0

我很抱歉我遲到的回覆。我在回答之前試圖做到這一點。我照你說的做了,它工作正常!謝謝! – user140561

+0

沒問題。很高興工作! – wdavies973