adlagar
adlagar

Reputation: 935

I can't open the Google Glass Camera

I've got this code (based on http://developer.android.com/training/camera/cameradirect.html), but on Google Glass not works for me.

The code:

public class MainActivity extends Activity {

    private Camera mCamera;
    private CameraPreview mPreview;
    private GestureDetector mGestureDetector;
    private Camera.PictureCallback mPicture;

    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mGestureDetector = createGestureDetector(this);

        mPicture = new Camera.PictureCallback() {

            @Override
            public void onPictureTaken(byte[] data, Camera camera) {

                File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
                if (pictureFile == null){
//                    Log.d("Camera", "Error creating media file, check storage permissions: " + e.getMessage());
                    return;

                }

                try {
                    FileOutputStream fos = new FileOutputStream(pictureFile);
                    fos.write(data);
                    fos.close();
                } catch (FileNotFoundException e) {
                    Log.d("Camera", "File not found: " + e.getMessage());
                } catch (IOException e) {
                    Log.d("Camera", "Error accessing file: " + e.getMessage());
                }
            }
        };

        // Create an instance of Camera
        mCamera = getCameraInstance();

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
    }


    private static File getOutputMediaFile(int type){
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyCameraApp");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".jpg");
        } else if(type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_"+ timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }
}

This line returns nullPointerException:

preview.addView(mPreview);

The getCameraInstance method:

public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            System.out.println(e.getMessage());
            // Camera is not available (in use or does not exist)
        }
        return c; // returns null if camera is unavailable
    }

I pretend to take a photo without a "TAP" gesture... is there any suggestion?

I have read other posts, but I have not found any solution yet :(

Thanks in advance!

UPDATED

The null Pointer have been solved, but now, the application returns me this error: "Fail to connect to camera service"

Upvotes: 2

Views: 662

Answers (1)

Jared Burrows
Jared Burrows

Reputation: 55527

Issue:

Since you are using the Android camera APIs(http://developer.android.com/training/camera/cameradirect.html) versus Google Glasses specific Camera APIs(https://developers.google.com/glass/develop/gdk/camera), you will most likely need to restart your Google Glass.

See other SO question: java.lang.RuntimeException: Fail to Connect to camera service

I have ran into this issue many times. Basically, when you are attempting to connect the Camera and you did not close out the Camera service properly, you will not be able to connect back to it again. I ran into this using the Android APIs and OpenCV's APIs.

Please follow the example below after restarting your device.

Example:

MainActivity:

public class MainActivity extends Activity {
    private CameraSurfaceView cameraView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Initiate CameraView
        cameraView = new CameraSurfaceView(this);

        // Set the view
        this.setContentView(cameraView);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Do not hold the camera during onResume
        if (cameraView != null) {
            cameraView.releaseCamera();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        // Do not hold the camera during onPause
        if (cameraView != null) {
            cameraView.releaseCamera();
        }
    }
}

CameraSurfaceView:

public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    private Camera camera;

    @SuppressWarnings("deprecation")
    public CameraSurfaceView(Context context) {
        super(context);

        final SurfaceHolder surfaceHolder = this.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();

        // Show the Camera display
        try {
            camera.setPreviewDisplay(holder);
        } catch (IOException e) {
            this.releaseCamera();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // Start the preview for surfaceChanged
        if (camera != null) {
            camera.startPreview();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // Do not hold the camera during surfaceDestroyed - view should be gone
        this.releaseCamera();
    }

    /**
     * Release the camera from use
     */
    public void releaseCamera() {
        if (camera != null) {
            camera.release();
            camera = null;
        }
    }
}

References:

I know this because I worked on a big Google Glass repository of examples located here: https://github.com/jaredsburrows/OpenQuartz.

Please see my personal Camera example here: https://github.com/jaredsburrows/OpenQuartz/tree/master/examples/CameraPreview.

Other SO question: java.lang.RuntimeException: Fail to Connect to camera service

Upvotes: 2

Related Questions