welcome to linkAR technical documentation

next previous

Process Preview

Follow this instructions to process every frame and have a continuous matching.

Add the callbacks

The following interfaces are necessary:

  • Camera.PreviewCallback
  • SurfaceHolder.Callback

Add the fields

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

Creating Matcher

Instantiate the Matcher with the 3 params constructor

/**Create an instance of the ARmatcher object. */
        aRmatcher = new ARmatcher(this,APIKEY);

Preparing the view

It is necessary to get the SurfaceView we added into the layout and its holder.

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

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().getSupportedPreviewSizes();
			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();
		camera.setPreviewCallback(this);
 
	}
 
	/** Stops the preview and releases camera*/
	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		//7
		camera.stopPreview();
		camera.setPreviewCallback(null);
		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.

Implement Camera Preview Callback

In the callback method just pass the previewed frame to the lib processFrame function.

@Override
	public void onPreviewFrame(byte[] data, Camera camera) {
		aRmatcher.processFrame(data, camWidth, camHeight);
 
	}

next previous