1

我打算构建一个Android应用程序。基于增强现实。我用谷歌搜索并搜索了 SO,但没有发现任何非常有用的东西。

我想要做的是当应用程序启动时,它会启动它的相机并开始扫描图案。如果在任何情况下模式与本地数据库中存在的预定义模式匹配,它应该执行特定操作。

我参考了本教程 当我使用以下代码时,这些文件会在 sdcard 上创建:patt.hiro, android.patt, barcode.patt.

在打开.patt文件时,有一个由 0-255 之间的值组成的矩阵。如何通过资产而不是这些文件使用自定义模式?

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();
    }
}

自定义对象.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) {}
}

自定义渲染器.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);
    }
}

如何从相机的聚焦区域捕捉图案以及如何将其与另一个图案匹配?

我知道我在这个问题上有点不清楚,但由于我是 android 开发的新手,任何建议都会有很大帮助。

4

1 回答 1

2

你必须看到使用opengl创建实际绿色立方体的CustomObject.java......创建3d对象你必须知道opengl......即使我试图通过使用相同的代码创建一些其他对象..这有点难,我们必须花一些时间来学习...

于 2012-02-22T12:27:53.757 回答