korechi’s diary

とあるVR/ARエンジニアのブログ

SmartEyeGlassに画像を表示する方法

初期設定

まずARイベント用ハンドラーであるSmartEyeglassEventListenerの登録を行う

private final SmartEyeglassEventListener listener = new SmartEyeglassEventListener() {
  @Override
  public void onARRegistrationResult(
     final int result, final int objectId) {
     if (result != SmartEyeglassControl.Intents.AR_RESULT_OK) {
       return;
     }
  }
  // Find bitmap to render when requested by AR engine
  @Override
  public void onARObjectRequest(final int objectId) {
     // send bitmap
       utils.sendARObjectResponse(renderObj, 0);
   }
  };

そしてSmartEyeglassControlUtilsのオブジェクトを生成し、初期設定

private final SmartEyeglassControlUtils utils;

utils = new SmartEyeglassControlUtils(hostAppPackageName, listener);
utils.setRequiredApiVersion(SMARTEYEGLASS_API_VERSION);
utils.activate(context);

imageMapにbitmapを登録

画像はbitmapにしてSmartEyeGlassに表示される
まずは、bitmapを複数格納する配列を用意する

private SparseArray<Bitmap> imageMap = new SparseArray<Bitmap>();

このimageMapにbitmapを入れる

// 配列の番号(キー)
int key;
// SAMPLE_PICTURE.pngが/res/drawable下にあるものとする
imageMap.put(key, R.drawable.SAMPLE_PICTUREのbitmap));

この処理を初めのタイミング(コンストラクタ内など)で使いたい画像全てに対して行うことでimageMapにbitmapが登録される。
けれど、肝心のbitmapはどう作ればいいのか??

bitmapはBitmapFactoryを使えば作れる

Bitmap b = BitmapFactory.decodeResource(context.getResources(), R.drawable.SAMPLE_PICTURE);
b.setDensity(DisplayMetrics.DENSITY_DEFAULT);

なので、このbを上にあるimageMap.putの第2引数にすればよい

こうしてimageMapに使いたいbitmapを無事登録できた。

bitmapをSmartEyeGlassに表示

これは実はそんなに難しくはない
まず、utilにrenderObjectを設定する

RenderObject renderObj = new RenderObject(OBJECT_ID, getBitmapResource(R.drawable.SAMPLE_PICTURE), 0, 0) {
  @Override
  public void toMoveExtras(Intent intent) {
  }
};
this.utils.registerARObject(renderObj);

そして、bitmapを表示する

// 欲しいbitmapのキー
int num = 0;
private static final int OBJECT_ID = 1;

Bitmap bitmap = imageMap.get(num);
utils.showBitmap(bitmap);

これだけで画像は表示できる。
もし、アニメーションを表示したい場合は、上の2つを変更する

CylindricalRenderObject  renderObj = new CylindricalRenderObject(OBJECT_ID,
                getBitmapResource(R.drawable.sample_00), 0,
                SmartEyeglassControl.Intents.AR_OBJECT_TYPE_ANIMATED_IMAGE,
                h, v);
this.utils.registerARObject(renderObj);

そして、アニメーションはtimerを使ってブレなく表示し続けるのが良さそう

// 欲しいbitmapのキー
int num = 0;
private static final int OBJECT_ID = 1;

// 追加
utils.enableARAnimationRequest();

timer = new Timer();
timer.schedule(new TimerTask() {
  private Handler mHandler = new Handler();
  @Override
  public void run() {
    mHandler.post(new Runnable() {
      public void run() {
        Bitmap bitmap = imageMap.get(num);
        utils.sendARAnimationObject(OBJECT_ID, bitmap);
      }
    });
  }
}, 0, ANIMATION_INTERVAL_TIME);

当然終了する箇所にはdisableをいれる

utils.disableARAnimationRequest();