頭をタッチしたら写真をとるアプリ
写真表示用のImageViewを追加
1 2 3 4 5 |
<ImageView android:id="@+id/img_picture" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop"/> |
「Holderの取得」〜「頭タッチ時の処理」
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
//手振れ防止のholderの取得 //(ここに処理を記述1) //頭がタッチされた時の処理 //(ここに処理を記述2) touchSensor.addOnStateChangedListener(state -> { Log.d("SampleApp", "Head/Touch"); if(!state.getTouched() || (futureTakePicture != null && !futureTakePicture.isDone())) return; futureTakePicture = SayBuilder.with(qiContext) //写真を撮るときに話す内容(お好きに書き換えてください!) .withText("\\vct=130\\ \\rspd=110\\ それじゃぁー \\pau=500\\ 撮りますよーッッ") .buildAsync() .andThenCompose(say -> say.async().run()) //Autonomousを止める(ここに処理を記述3) .andThenCompose() //アニメーションのビルドと実行 .andThenCompose(aVoid -> AnimationBuilder.with(qiContext) //好きなアニメーションに変える .withResources(R.raw.animation_00).buildAsync()) .andThenCompose(animation -> AnimateBuilder.with(qiContext) .withAnimation(animation).buildAsync()) .andThenCompose(animate -> animate.async().run()) //写真を撮る(ここに処理を記述4) .andThenCompose() .andThenApply() .andThenConsume( ) .thenConsume(voidFuture -> { if(voidFuture.hasError()) Log.d("SampleApp", voidFuture.getErrorMessage()); runOnUiThread(() -> Toast.makeText(getApplicationContext(), "完了", Toast.LENGTH_SHORT).show()); //Holderをリリースし、Autonomousを再開 //(ここに処理を記述5) }); }); |
写真の表示処理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//画像表示用メソッド private void showPicture(EncodedImage encodedImage){ //引数のEncodedImageオブジェクトはTakePicture actionにて取得できる ByteBuffer data = encodedImage.getData(); //getDataでByteBufferのオブジェクトを取得 data.rewind(); //バッファの位置の値を0に戻して初めから読み込みできるようにする int pictureBufferSize = data.remaining(); // バッファの現在位置からリミットまでに存在する要素の数を返す //Byte配列に変換して、BitmapFactoryクラスでBitmapオブジェクトに変換 byte[] facePictureArray = new byte[pictureBufferSize]; data.get(facePictureArray); if (pictureBufferSize != 0) { final Bitmap bmp = BitmapFactory.decodeByteArray( facePictureArray, 0, pictureBufferSize); //UIスレッドで、BitmapをImageViewに設定 runOnUiThread(() -> ((ImageView)findViewById(R.id.img_picture)) .setImageBitmap(bmp)); } } |