welcome to linkAR technical documentation

Introduction

There are some cases in which it is necessary to load the camera outside of the lib. We will explain here the main steps that you need to follow to achieve it.

1.- Display Camera Preview

Follow this instructions to add the camera preview into the device screen.

1.1.- Add the callbacks

Implement the following interface into your activity:

  • SurfaceHolder.Callback
public class ARbrowserOutCameraActivity extends Activity 
                                        implements SurfaceHolder.Callback

1.2.- Add the fields

private Camera camera;
private SurfaceView surfaceView;
private SurfaceHolder holder;	

1.3.- Preparing the view

It is necessary to create a and get its holder.

/**Add camera view instance to content view */
setContentView(R.layout.main);
FrameLayout arFrame=(FrameLayout)findViewById(R.id.arFrame);
 
/** Get the Surface View and its holder. Set our activity as SurfaceHolder.Callback*/
surfaceView= new SurfaceView(this);
holder=surfaceView.getHolder();
holder.addCallback(this);
surfaceView.setZOrderMediaOverlay(false);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        

1.4.- Creating Browser

Instantiate the Browser, the constructor last parameter ( isInnerCamera ) must be set to false.

aRbrowserView = new ARbrowserView(this,ARbrowserView.UI_ORIENTATION_ALL,APIKEY,
                              ARbrowserView.SCREEN_ORIENTATION_PORTRAIT,false,false);
 
arFrame.addView(surfaceView);
arFrame.addView(aRbrowserView.getARviewInstance(), 
                   new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

Further initialization can be found at Quick Start.

1.5.- Implement Holder Callback

/** Important to implement this method*/
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int w, int h) {
  if (mPreviewRunning) {
    camera.stopPreview();
  }
  /** Get and modify the camera parameters*/
  Camera.Parameters p = camera.getParameters();
 
  //1
  List<Size> sizes = camera.getParameters().getSupportedPictureSizes();
  int diff = Integer.MAX_VALUE;
  for(int i=0;i<sizes.size(); i++)
  {
    //2
    if(Math.abs(sizes.get(i).width-w)<diff)
    {
      camWidth=sizes.get(i).width;
      camHeight=sizes.get(i).height;
    }
  }
  //3
  p.setPreviewSize(camWidth,camHeight);			
  camera.setParameters(p);
 
  /** changes the camera view*/
  //4
  camera.setDisplayOrientation(90);
  try {
    camera.setPreviewDisplay(holder);
  } catch (IOException e) {
    e.printStackTrace();
  }
 
  //5
  camera.startPreview();
  mPreviewRunning = true;			
}
 
@Override
public void surfaceCreated(SurfaceHolder arg0) {
  /** Gets the camera*/
  //6
  camera=Camera.open();		
}
 
/** Stops the preview and releases camera*/
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
  //7
  camera.stopPreview();
  mPreviewRunning = false;
 
  camera.release();
}

The camera is managed throughout the surface holder callback functions.

  1. Get the supported camera sizes.
  2. Compare the sizes: The idea is to save in (camWidth, camHeight) the closest size to our surfaceView size.
  3. Set the previewSize
  4. When working in portrait, it is necessary to rotate the camera preview. If your activity is in Landscape it is not necessary to rotate it.
  5. Starts the preview.
  6. Initiates the camera when the surface view is created.
  7. Stops the camera when the surface view has been destroyed.