Tuesday, September 3, 2013

[Tut] How to perform AndEngine Examples of Nicolas Gramlich - AugmentedRealityExample (with camera)


andengineaugmentedrealityextension.jar has some problems when using it to implement this example on emulator, so you can use some files to replace the .jar file

Create the package org.anddev.andengine.extension.augmentedreality and create 2 files like this


Here is the code of BaseAugmentedRealityGameActivity class
package org.anddev.andengine.extension.augmentedreality;

import org.anddev.andengine.opengl.view.ComponentSizeChooser;
import org.anddev.andengine.opengl.view.RenderSurfaceView;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;


/**
 * @author Nicolas Gramlich
 * @since 21:38:32 - 24.05.2010
 */
public abstract class BaseAugmentedRealityGameActivity extends BaseGameActivity {
// ===========================================================
// Constants
// ===========================================================

// ===========================================================
// Fields
// ===========================================================

private CameraPreviewSurfaceView mCameraPreviewSurfaceView;

// ===========================================================
// Constructors
// ===========================================================
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.mCameraPreviewSurfaceView = new CameraPreviewSurfaceView(this);
this.addContentView(this.mCameraPreviewSurfaceView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// this.mRenderSurfaceView.setZOrderMediaOverlay(true);
//this.mRenderSurfaceView.bringToFront();
}

// ===========================================================
// Getter & Setter
// ===========================================================

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
protected void onSetContentView() {
//this.mRenderSurfaceView = new RenderSurfaceView(this, this.mEngine);
this.mRenderSurfaceView = new RenderSurfaceView(this);

this.mRenderSurfaceView.setEGLConfigChooser(new ComponentSizeChooser(4, 4, 4, 4, 16, 0)); 
this.mRenderSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);

//this.mRenderSurfaceView.applyRenderer();
this.mRenderSurfaceView.setRenderer(this.mEngine);

this.setContentView(this.mRenderSurfaceView, createSurfaceViewLayoutParams());
}
@Override
protected void onPause() {
super.onPause();
// finish();
}

// ===========================================================
// Methods
// ===========================================================

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
Here is the code of CameraPreviewSurfaceView class
/**
 * 
 */
package org.anddev.andengine.extension.augmentedreality;

import java.io.IOException;

import org.anddev.andengine.util.Debug;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * @author Nicolas Gramlich
 * @since 21:38:21 - 24.05.2010
 */
class CameraPreviewSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
// ===========================================================
// Constants
// ===========================================================

// ===========================================================
// Fields
// ===========================================================

private final SurfaceHolder mSurfaceHolder;
private Camera mCamera;

// ===========================================================
// Constructors
// ===========================================================

public CameraPreviewSurfaceView(final Context pContext) {
super(pContext);

this.mSurfaceHolder = this.getHolder();
this.mSurfaceHolder.addCallback(this);
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
// ===========================================================
// Getter & Setter
// ===========================================================

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

public void surfaceCreated(final SurfaceHolder pSurfaceHolder) {
this.mCamera = Camera.open();
try {
this.mCamera.setPreviewDisplay(pSurfaceHolder);
} catch (IOException e) {
Debug.e("Error in Camera.setPreviewDisplay", e);
}
}

public void surfaceDestroyed(final SurfaceHolder pSurfaceHolder) {
this.mCamera.stopPreview();
this.mCamera.release();
this.mCamera = null;
}

public void surfaceChanged(final SurfaceHolder pSurfaceHolder, final int pPixelFormat, final int pWidth, final int pHeight) {
final Camera.Parameters parameters = this.mCamera.getParameters();
parameters.setPreviewSize(pWidth, pHeight);
this.mCamera.setParameters(parameters);
this.mCamera.startPreview();
}
// ===========================================================
// Methods
// ===========================================================

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
Create the AugmentedRealityExample class in org.anddev.andengine.examples package. Here is the code:
package org.anddev.andengine.examples;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.extension.augmentedreality.BaseAugmentedRealityGameActivity;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.widget.Toast;

/**
 * (c) 2010 Nicolas Gramlich
 * (c) 2011 Zynga
 *
 * @author Nicolas Gramlich
 * @since 11:54:51 - 03.04.2010
 */
public class AugmentedRealityExample extends BaseAugmentedRealityGameActivity {
// ===========================================================
// Constants
// ===========================================================

private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;

// ===========================================================
// Fields
// ===========================================================

private Camera mCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mFaceTextureRegion;

// ===========================================================
// Constructors
// ===========================================================

// ===========================================================
// Getter & Setter
// ===========================================================

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

@Override
public Engine onLoadEngine() {
Toast.makeText(this, "If you don't see a sprite moving over the screen, try starting this while already being in Landscape orientation!!", Toast.LENGTH_LONG).show();
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
}

@Override
public void onLoadResources() {
this.mBitmapTextureAtlas = new BitmapTextureAtlas(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);

this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
}

@Override
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());

final Scene scene = new Scene();
// scene.setBackgroundEnabled(false);
scene.setBackground(new ColorBackground(0.0f, 0.0f, 0.0f, 0.0f));

final int centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
final int centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion);
face.registerEntityModifier(new MoveModifier(30, 0, CAMERA_WIDTH - face.getWidth(), 0, CAMERA_HEIGHT - face.getHeight()));
scene.attachChild(face);

return scene;
}

@Override
public void onLoadComplete() {

}

// ===========================================================
// Methods
// ===========================================================

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}

Open the AndroidManifest.xml and append the activity element below the previous activity element like this:
<activity
            android:name=".AnimatedSpritesExample"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="org.anddev.andengine.examples.ANIMATEDSPRITESEXAMPLE" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".AugmentedRealityExample"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="org.anddev.andengine.examples.AUGMENTEDREALITYEXAMPLE" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
 Append the uses-feature and uses-permission element after uses-sdk element like this
<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
<uses-feature 
   android:name="android.hardware.camera"
   android:required="false"
   />
<uses-feature 
   android:name="android.hardware.camera.autofocus"
   android:required="false"
   />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>


Run the project (in debug mode) to re-generate apk file in the bin folder
Open and Edit the batch command file named r.bat in the bin folder with some commands like this:
adb install -r AndEngineDemo.apk
adb shell am start -n org.anddev.andengine.examples/.AugmentedRealityExample
Run android emulator with virtualbox
Then run the batch file r.bat

The result's like this with animated pictures

We only see camera like this on the emulator :)

[ttaiit.blogspot.com] - please write clearly the source on copying this tutorial :)

0 comments:

Post a Comment

Choose an Android item