2

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"

edwardmp
  • 6,339
  • 5
  • 50
  • 77
adlagar
  • 877
  • 10
  • 31

1 Answers1

2

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

Community
  • 1
  • 1
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • I will test it tomorrow, but i think that i have a very similar code. I have read other posts about this problem, and all of them talk about the releaseCamera() method... I have tried several changes, but I have not solved the problem yet =(. Thanks Jared! – adlagar Apr 28 '15 at 20:28
  • Ok let me know. I am here for debugging. – Jared Burrows Apr 28 '15 at 20:45
  • @adri92_DsM Have you tested this? – Jared Burrows Apr 29 '15 at 22:22
  • I couldn't prove it yet.. =( I will test it on the next 3 hours. Sorry for the inconvenience – adlagar Apr 30 '15 at 08:22
  • Hi again @Jared-burrows. I've tried the code on my Android smartphone and, at this moment, I have not gotten to run the camera succesfully. The example app of Google Glass camera not works (error 404) :( https://github.com/jaredsburrows/OpenQuartz/blob/master/examples/CameraApp Is there any other way to view the code? Thanks so much – adlagar May 01 '15 at 15:02
  • @adri92_DsM The example was for Google Glass. I am still updating the projects to Android Studio from Eclipse. Please see the link from my answer: https://github.com/jaredsburrows/OpenQuartz/tree/master/examples/CameraPreview. – Jared Burrows May 01 '15 at 15:08
  • Yes, I know that it was for Google Glass, but I don't have the glasses in my home =( I will test the code on Google Glass next Monday. This weekend I only have a smartphone =( Thank you very much for your attention, really :) – adlagar May 01 '15 at 15:52
  • Ok, you can still use the code from the repo, I need to update the README. – Jared Burrows May 01 '15 at 15:55
  • I couldn't to test it on Glass yet... I still testing it on my Android smartphone, but it not works. I hope to test it today or tomorrow, but I have a lot of work these days :( – adlagar May 05 '15 at 08:01
  • 1
    Yeah Jared! The example works fine on Glass! =D Thanks so much... How I can make a photo with that code? – adlagar May 13 '15 at 10:02
  • You can use the camera intent to take a photo. I think I have an example in the same repo, see here: https://github.com/jaredsburrows/OpenQuartz/blob/master/examples-old/CameraApp/src/com/openquartz/camera/MainActivity.java. – Jared Burrows May 13 '15 at 15:58