在增强现实中创建、使用和匹配模式
我正计划构建一个 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();
}
}
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);
}
}
如何从相机的聚焦区域捕获图案以及如何将其与其他图案进行匹配?
我知道我对这个问题有点不清楚,但由于我是 Android 开发的新手,任何建议都会有很大的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须查看 CustomObject.java,其中使用 opengl 创建实际的绿色立方体...为了创建 3d 对象,您必须了解 opengl...即使我尝试使用相同的代码创建一些其他对象..这有点困难,我们必须花一些时间来学习......
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...