Search code examples
androidpattern-matchingaugmented-realityandar

Creating, using and matching pattern in Augmented Reality


I'm planning to build an Android App. based on Augmented Reality. I googled it and also searched on SO, but have not found anything very much helpful.

What I want to do is when application is launched, it will start it's camera and initiate the scanning of the pattern. If at any instance the pattern matches with the predefined pattern present in local database, it should perform a specific action.

I referred this tutorial When I use the following code, these files get created on sdcard : patt.hiro , android.patt , barcode.patt.

On opening .patt file, there is a matrix consisting of values between 0-255. How can I use customized pattern through assets instead of these files?

CustomActivity.java

public class CustomActivity extends AndARActivity {

    CustomObject someObject;
    ARToolkit artoolkit;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CustomRenderer renderer = new CustomRenderer();
        super.setNonARRenderer(renderer);
        try {   // register a object for each marker type
            artoolkit = super.getArtoolkit();
            someObject = new CustomObject("test", "patt.hiro", 80.0,
                    new double[] { 0, 0 });
            artoolkit.registerARObject(someObject);
            someObject = new CustomObject("test", "android.patt", 80.0,
                    new double[] { 0, 0 });
            artoolkit.registerARObject(someObject);
            someObject = new CustomObject("test", "barcode.patt", 80.0,
                    new double[] { 0, 0 });
            artoolkit.registerARObject(someObject);
        } catch (AndARException ex) {
        }
        startPreview();
    }
}

CustomObject.java

/** An example of an AR object being drawn on a marker. */
public class CustomObject extends ARObject {

    public CustomObject(String name, String patternName, double markerWidth,
            double[] markerCenter) {

        super(name, patternName, markerWidth, markerCenter);
        float mat_ambientf[] = { 0f, 1.0f, 0f, 1.0f };
        float mat_flashf[] = { 0f, 1.0f, 0f, 1.0f };
        float mat_diffusef[] = { 0f, 1.0f, 0f, 1.0f };
        float mat_flash_shinyf[] = { 50.0f };

        mat_ambient = GraphicsUtil.makeFloatBuffer(mat_ambientf);
        mat_flash = GraphicsUtil.makeFloatBuffer(mat_flashf);
        mat_flash_shiny = GraphicsUtil.makeFloatBuffer(mat_flash_shinyf);
        mat_diffuse = GraphicsUtil.makeFloatBuffer(mat_diffusef);
    }

    public CustomObject(String name, String patternName, double markerWidth,
            double[] markerCenter, float[] customColor) {
        super(name, patternName, markerWidth, markerCenter);
        float mat_flash_shinyf[] = { 50.0f };
        mat_ambient = GraphicsUtil.makeFloatBuffer(customColor);
        mat_flash = GraphicsUtil.makeFloatBuffer(customColor);
        mat_flash_shiny = GraphicsUtil.makeFloatBuffer(mat_flash_shinyf);
        mat_diffuse = GraphicsUtil.makeFloatBuffer(customColor);
    }

    /** Just a box, imported from the AndAR project. */
    private SimpleBox box = new SimpleBox();
    private FloatBuffer mat_flash;
    private FloatBuffer mat_ambient;
    private FloatBuffer mat_flash_shiny;
    private FloatBuffer mat_diffuse;

    /** Everything drawn here will be drawn directly onto the marker, as the
     corresponding translation matrix will already be applied. */
    @Override
    public final void draw(GL10 gl) {

        super.draw(gl);

        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, mat_flash);
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS,
                mat_flash_shiny);
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, mat_diffuse);
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, mat_ambient);

        // draw cube
        gl.glColor4f(0, 1.0f, 0, 1.0f);
        gl.glTranslatef(0.0f, 0.0f, 12.5f);

        // draw the box
        box.draw(gl);
    }
    @Override
    public void init(GL10 gl) {}
}

CustomRenderer.java

/** A custom OpenGL renderer, that gets registered to the {@link AndARRenderer}. It allows you to draw non Augmented Reality stuff, and setup the OpenGL environment.*/
public class CustomRenderer implements OpenGLRenderer {

    /** Light definitions */
    private float[] ambientlight1 = { .3f, .3f, .3f, 1f };
    private float[] diffuselight1 = { .7f, .7f, .7f, 1f };
    private float[] specularlight1 = { 0.6f, 0.6f, 0.6f, 1f };
    private float[] lightposition1 = { 20.0f, -40.0f, 100.0f, 1f };

    private FloatBuffer lightPositionBuffer1 = GraphicsUtil
            .makeFloatBuffer(lightposition1);
    private FloatBuffer specularLightBuffer1 = GraphicsUtil
            .makeFloatBuffer(specularlight1);
    private FloatBuffer diffuseLightBuffer1 = GraphicsUtil
            .makeFloatBuffer(diffuselight1);
    private FloatBuffer ambientLightBuffer1 = GraphicsUtil
            .makeFloatBuffer(ambientlight1);

    /** Do non Augmented Reality stuff here. Will be called once after all AR objects have been drawn. The transformation matrices may have to be reset.*/
    public final void draw(GL10 gl) {
    }

    /**Directly called before each object is drawn. Used to setup lighting and other OpenGL specific things.*/
    public final void setupEnv(GL10 gl) {
        gl.glEnable(GL10.GL_LIGHTING);
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, ambientLightBuffer1);
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, diffuseLightBuffer1);
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_SPECULAR, specularLightBuffer1);
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, lightPositionBuffer1);
        gl.glEnable(GL10.GL_LIGHT1);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glDisable(GL10.GL_TEXTURE_2D);
        initGL(gl);
    }

    /** Called once when the OpenGL Surface was created. */
    public final void initGL(GL10 gl) {
        gl.glDisable(GL10.GL_COLOR_MATERIAL);
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glDisable(GL10.GL_COLOR_MATERIAL);
        gl.glEnable(GL10.GL_LIGHTING);
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glEnable(GL10.GL_NORMALIZE);
    }
}

How do I capture the pattern from camera's focused area and how do I match it with another pattern?

I know I'm bit unclear in this question but as I'm new to android development, any suggestion would be of great help.


Solution

  • you must have to see the CustomObject.java where the actual green cube is creating by using opengl... for creating that 3d objects you must have to know the opengl... Even i m trying to create some other objects by using the same code.. it is bit difficult and we must have to spend some time to learn that...