libgdx android:camera.getParameters()崩溃app NullPointerException

时间:2016-07-23 06:00:33

标签: java android libgdx

我想要一个使用libgdx android的制作相机设备,我正在使用this code。 但是我在NullPointerException课程的Camera.Parameters p = camera.getParameters();CameraSurface.java。{/ p>

package com.mygdx.cameradevice;

import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraSurface extends SurfaceView implements SurfaceHolder.Callback {

private Camera camera;

public CameraSurface( Context context ) {
    super( context );

    // We're implementing the Callback interface and want to get notified
    // about certain surface events.
    getHolder().addCallback( this );
    // We're changing the surface to a PUSH surface, meaning we're receiving
    // all buffer data from another component - the camera, in this case.
    getHolder().setType( SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS );

}

public void surfaceCreated( SurfaceHolder holder ) {
    // Once the surface is created, simply open a handle to the camera hardware.
     camera = Camera.open();

}
public void surfaceChanged( SurfaceHolder holder, int format, int width, int height ) {
    // This method is called when the surface changes, e.g. when it's size is set.
    // We use the opportunity to initialize the camera preview display dimensions.
    //System.out.println("ajay");
    //camera=Camera.open();
    Camera.Parameters p = camera.getParameters();
    p.setPreviewSize(width,height);
    camera.setParameters(p);

    // We also assign the preview display to this surface...
    try {
        camera.setPreviewDisplay(holder);
    } catch( IOException e ) {
        e.printStackTrace();
    }
}

public void surfaceDestroyed( SurfaceHolder holder ) {
    // Once the surface gets destroyed, we stop the preview mode and release
    // the whole camera since we no longer need it.
    camera.stopPreview();
    camera.release();
    camera = null;
}

public Camera getCamera() {
    return camera;
}

}

1 个答案:

答案 0 :(得分:1)

我看到您的代码是从https://github.com/libgdx/libgdx/wiki/Integrating-libgdx-and-the-device-camera复制的, 我使用相同的,然后我在它的帮助下创建一个项目,CameraSurface.java中的代码是相同的,所以也许有其他错误。 你可以看到我的简单项目https://github.com/54wall/LibgdxAndroidCamera, 看看你和我之间的区别。我想AndroidDeviceCameraController.class中的一些关键代码。 我希望这会对你有所帮助!