Androidのアプリからカメラを使うには・・・実はもっと簡単な方法があった。。。orz
Intentを使えばよかったらしい。
- Intentでアプリを呼び出す
- カメラを呼び出す機能の定数(Intent Action)
本当に簡単だった。
サンプルプログラムにはボタンとImageViewがあり、ボタンが押されたらIntentを作成してstartActivityForResultを呼び出す。2個目の引数の0(ゼロ)は、Actionの成功時にrequestCodeとして返される。1つのプログラムで複数のIntentActionを使う場合は、これで判別できる。
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 0);
カメラでシャッターが押されたら(画像がキャプチャされたら)onActivityResult が呼び出される。
IntentからBundleを使ってExtra dataを取り出す。
(”data”というKeyだったが、1つしかないので、ここではあまり気にしない)
取り出したObjectをImageにキャストしてImageViewで表示する/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
//If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field.
Bundle b = data.getExtras();
ImageView img = (ImageView)this.findViewById(stndstn.intenttest.R.id.imageView1);
Set keys = b.keySet();
Iterator k = keys.iterator();
while(k.hasNext()) {
Object o = b.get(k.next());
if(o.getClass() == Bitmap.class) {
Bitmap bmp = (Bitmap)o;
img.setImageBitmap(bmp);
break;
}
}
}
}
}
カメラのシャッターは自分で押さないといけないが、アプリからカメラ機能を使う状況を考えると、普通はこれで十分だろう。ただし、セルフタイマーなどのようにプログラムからシャッターを切るような動作はできないようにみえる。
コメントを残す