protected void initCamera(SurfaceHolder surfaceHolder) {
   try {
     cameraManager.openDriver(surfaceHolder);
     // Creating the handler starts the preview, which can also throw a
     // RuntimeException.
     if (handler == null)
       handler = new DecoderActivityHandler(this, decodeFormats, characterSet, cameraManager);
   } catch (IOException ioe) {
     Log.w(TAG, ioe);
   } catch (RuntimeException e) {
     // Barcode Scanner has seen crashes in the wild of this variety:
     // java.?lang.?RuntimeException: Fail to connect to camera service
     Log.w(TAG, "Unexpected error initializing camera", e);
   }
 }
  @Override
  protected void onPause() {
    super.onPause();

    if (handler != null) {
      handler.quitSynchronously();
      handler = null;
    }

    cameraManager.closeDriver();

    if (!hasSurface) {
      SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
      SurfaceHolder surfaceHolder = surfaceView.getHolder();
      surfaceHolder.removeCallback(this);
    }
  }