0

So I'm writing an app for Google Glass that uses the camera to display a preview. The way I have it set up now, the app loads to a main screen from which the user navigates to the camera portion. The preview shows up then an intent is created that takes a picture and the picture is processed. This works 70% of the time; however, occasionally, the app goes to a blackscreen instead of the preview at which point the glass crashes and a hard restart is required.

This is the message from LogCat:

06-18 09:04:05.717: I/CameraService(18740): CameraService::connect X (id 0, this pid is 18740)
06-18 09:04:05.717: I/CameraService-GoogleCamera(18740): Acquire hardware jpeg encoder lock took: 0 mS 
06-18 09:04:05.717: D/libgcam(18740): [gcam.cc:3305]: Gcam::Pause
06-18 09:04:05.741: D/debug(9063): ONRESUME
06-18 09:04:05.756: I/Choreographer(9063): Skipped 194 frames!  The application may be doing too much work on its main thread.
06-18 09:04:06.413: I/CameraHal(18740): (b79e90f0)   hardware/ti/omap4xxx/camera/CameraHalCommon.cpp:114 PPM - PPM: Standby to first shot: Sensor Change completed -  :161.11 ms :  1403096646415 ms
06-18 09:04:06.459: I/Choreographer(9063): Skipped 41 frames!  The application may be doing too much work on its main thread.
06-18 09:04:06.772: I/ActivityManager(19034): Displayed com.example.d31testing/.CameraActivity: +4s282ms

This is the beginning of the code that I have in OnCreate:

Log.d("debug", "ONCREATE");
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_camera);  
// determining display dimensions to place the reticle
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
Log.d("tag", "x:" + width + "y:" + height);
// Create an instance of Camera
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
// code to get 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);
CropDraw mCrop = new CropDraw(this, width, height);
// adding the reticle to the screen
preview.addView(mCrop);
Toast.makeText(
this,
"Press Camera Button to take a picture! \n Take a picture of a side view",    Toast.LENGTH_SHORT).show();
Vignesh K
  • 13
  • 4
  • You should not sleep() on the main thread, and definitely not 3 sec! You should open the camera (probably wrapped in `getCameraInstance()` method in a background thread. For best results, use a Handler (event) thread, see the [detailed explanation](http://stackoverflow.com/a/19154438/192373). – Alex Cohn Jul 08 '14 at 19:57

1 Answers1

0

Try to capture the camera in a safe way such as this to get the get Camera Instance:

public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open();
    } catch (Exception e){
    }
    return c;
}

Then To get the preview just call a method

public void getPreview()
{
 setContentView(R.layout.activity_quick_scan);
 autoFocusHandler = new Handler();

//For me at least, the camera had some trouble so I retried 3 times
 for(int i=0; i < 3; i++)
    {
        mCamera = getCameraInstance();
        if(mCamera != null) break;
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    if(mCamera == null)
    {
        Toast.makeText(this, "Camera cannot be locked", Toast.LENGTH_SHORT).show();
        finish();
    }


 FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
 mPreview = new CameraPreview(this, mCamera, null , autoFocusCB);
 preview.addView(mPreview);
}

For reference the xml would look a little like this:

<FrameLayout
    android:id="@+id/cameraPreview"
    android:layout_width="480dp"
    android:layout_height="0dp"
    android:layout_weight="0.34" >
</FrameLayout>
  • The problem is whenever I call `Camera.open()` in my activity, I get a runtime exception. I'm aware of the safe method to open the camera - I'm wondering how I could tell any other process currently using the camera to give me access to the service. – Kurt Mueller Jan 11 '15 at 00:38