1

I want a make camera device using libgdx android and i am using this code. But i am geting NullPointerException at this line Camera.Parameters p = camera.getParameters(); in CameraSurface.java class.

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;
}

}
Enigo
  • 3,685
  • 5
  • 29
  • 54
A.K
  • 41
  • 4
  • so, I guess that `surfaceChanged` method is called before `surfaceCreated`. anyway take a look [here](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Enigo Jul 23 '16 at 21:10
  • Have you added camera permissions & flash/hardware features in manifest??? – VVB Jul 24 '16 at 09:42
  • yes, i added camera permission in AndroidManifest.xml file,and surfaceCreated method is first time run after run surfaceChanged.So i am geting NullPointerException at this line Camera.Parameters p = camera.getParameters() in CameraSurface class. – A.K Jul 24 '16 at 13:56

1 Answers1

1

I see your code is copyed from https://github.com/libgdx/libgdx/wiki/Integrating-libgdx-and-the-device-camera, I use the same one,and then i create a project with the help of it,the codes in CameraSurface.java are the same,so maybe there is sth else wrong with. You can see my simple project https://github.com/54wall/LibgdxAndroidCamera, to see the difference between yours and mine.I think some key codes in AndroidDeviceCameraController.class. I hope that would help you!

54wall
  • 116
  • 1
  • 6