简体   繁体   中英

App slows down and stops responding trying to load camera(Java, Libgdx)

I am making a libgdx app with camera integration. 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. Here is the problem:

At AndroidDeviceCamera:

    activity.setFixedSize(1600,1200);

At 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:

    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).

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. 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:

    public enum Mode {
       normal,
       prepare,
       preview,
       takePicture, 
       waitForPictureReady, 
}

DeviceCamera Interface at core project:

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM