1

I've followed the Glass guide for taking a picture, found here. However, this creates a new Intent that automatically captures a picture without creating a camera preview in the Glass viewport.

In an attempt to allow the user to first preview what the camera sees on the glass' viewport, I followed the android developer guide, found here, and eventually ran into a runtime exception being thrown when attempting to call Camera.open():

java.lang.RuntimeException: Fail to connect to camera service

The developer guide wraps Camera.open() in a try-catch clause but doesn't explain what to do in the event an exception is raised.

Here are my questions:

  1. Is there any way to see if any other Android process / service has a lock on the Glass' camera?
  2. If so, how can I tell that process / service to give me the camera?
  3. Perhaps in the Glass developer guide I linked, is there a way to let the user preview the image before the picture is taken?

Thanks for any help!

Kurt Mueller
  • 3,173
  • 2
  • 29
  • 50
  • `Thanks for any help!` Isn't the `try-catch` block your way to detect if you can open the camera? If you can't open it, it has to be locked by another process, then the catch is "called"... – Mike Jan 11 '15 at 01:13
  • I understand that. I don't understand what to actually do when the Exception is caught. The developer guide simply prints a stack trace. How can I tell an other processes to give me control of the camera? How can I tell what other process has control of the camera? – Kurt Mueller Jan 11 '15 at 01:22
  • I think(!)- i'm not sure - , but you will not have the power to force other applications to release the camera. But for example you can tell the user what's the problem and what he should do. For start up, look [this](http://stackoverflow.com/questions/24155236/android-error-cant-connect-to-the-camara) it shows a pattern of how to check if another app has locked the camera. Hope it helps ;) – Mike Jan 11 '15 at 01:32
  • and it sounds just logical to me that it automatically takes a picture, i'm now familar with google glass, but doesn't the camera "looks" in exactly the same direction your eyes, more your head do? So why providing a preview. It takes a picture from what you see. Just a Suggestion. – Mike Jan 11 '15 at 01:38

3 Answers3

1

Place permissions above the Application tag, in your manifest file

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"/>

<application>
      <activity></activity>
</application> 

I kept the permissions in wrong place which is why I kept on getting the same error! Hope this will help you too.

Acoop
  • 2,586
  • 2
  • 23
  • 39
Fazal
  • 3,374
  • 1
  • 15
  • 20
0

Please have a look at following code snippet which shows the acquisition of camera in camera.open() and then release when a photo is captured.

public class CameraView extends SurfaceView implements SurfaceHolder.Callback
{
            private SurfaceHolder surfaceHolder = null;
            private Camera camera = null;

            @SuppressWarnings("deprecation")
            public CameraView(Context context)
            {
                super(context);
                surfaceHolder = this.getHolder();
                surfaceHolder.addCallback(this);
                surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            }

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

                // Set the Hotfix for Google Glass
                this.setCameraParameters(camera);

                // Show the Camera display
                try
                {
                                camera.setPreviewDisplay(holder);
                }
                catch (Exception 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();
            }

            public void setCameraParameters(Camera camera)
            {
                if (camera != null)
                {
                                Parameters parameters = camera.getParameters();
                                parameters.setPreviewFpsRange(30000, 30000);
                                camera.setParameters(parameters);   
                }
            }

            public void releaseCamera()
            {
                if (camera != null)
                {
                                camera.release();
                                camera = null;
                }
            }
}

This is a working code. Fore details take a look at the following tutorial.

Hope this would help!!

AniV
  • 3,997
  • 1
  • 12
  • 17
  • according to the OP, the exception is thrown by the call to Camera.open. your code would crash. – njzk2 Jan 14 '15 at 23:42
0

Figured it out. In my AndroidManifest.xml, I was placing my permissions and feature requests under application instead of directly under manifest:

Before:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my.organization">

        <application
            android:name=".MyApp"
            android:allowBackup="true"
            android:label="@string/app_name">

            <uses-permission android:name="android.permission.CAMERA" />
            <uses-feature android:name="android.hardware.Camera" />

After:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my.organization">

        <uses-permission android:name="android.permission.CAMERA" />
        <uses-feature android:name="android.hardware.Camera" />

        <application
            android:name=".MyApp"
            android:allowBackup="true"
            android:label="@string/app_name">
Kurt Mueller
  • 3,173
  • 2
  • 29
  • 50