Android学習の呼び出しシステムカメラで撮影機能を実現
7734 ワード
一、今日は、システムが持つカメラを呼び出して写真を撮る方法を紹介します.主に以下の2つの実現方法があります.1、Cameraアプリケーションには、開発者がCameraアプリケーションと同等の画像キャプチャ能力を提供できるようにする意図フィルタ、intent filterが含まれています.そのため、アプリケーションのAndroid Manifestで使用することができます.xmlリストファイルに意図フィルタを新規作成すると、この意図フィルタを含むアクティビティが命令に従って指定されたタスクを実行し、意図フィルタコードを作成するAndroidシステムに通知されます.
次に、Cameraアプリケーションを利用して、上記のフィルタによってキャプチャされる意図を構築します.コードは次のとおりです.
2、第2の方式は、第1のように面倒をかける必要がなく、MediaStoreクラスの定数ACTIONを指定することができるIMAGE_CAPTUREは呼び出しシステムに付属するカメラを実現し、コードは以下の通りである.
3、上記の2つの方法で、画面に画像を表示する必要がなければ、Intentオブジェクトをインスタンス化した後、次のコードを使用してカメラを開くことができます.
二、次の例では、持参したシステムカメラを呼び出して写真を撮り、写真を撮った後に画像を表示することを確認しますが、次の例はIntentの意図によってトリガーされるため、カメラアプリはフルサイズの画像をメインアクティビティに戻すことはできませんので、小さなサムネイルを1枚だけ返します.1、まず、Androidプロジェクトを新設し、プロジェクト名はIntent_camera、まず、インタフェースをレイアウトし、デフォルトのactivity_を開きます.main.xmlファイルには、ButtonコントロールとImageViewコントロールがあります.コードは次のとおりです.
2、その後、デフォルトのMainActivityクラスファイルを開き、主にIntent呼び出しシステムによってカメラを持参し、startActivity ForResult()メソッドによってカメラを開き、onActivity Result()によって転送された画像を受信します.コードは以下の通りです.
3、これを本体に配置し、撮影ボタンをクリックした後、システムが持っているカメラを開き、撮影が終わったら確認またはチェックボタンを押すと、写真を撮る活動に画像を表示できますが、サムネイルです.
三、以上の内容は、皆さんの勉強の参考になります.ありがとうございます.
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE"/>
<category android:name="android.intent.category.DEFAULT"/>
intent-filter>
次に、Cameraアプリケーションを利用して、上記のフィルタによってキャプチャされる意図を構築します.コードは次のとおりです.
Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
2、第2の方式は、第1のように面倒をかける必要がなく、MediaStoreクラスの定数ACTIONを指定することができるIMAGE_CAPTUREは呼び出しシステムに付属するカメラを実現し、コードは以下の通りである.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
3、上記の2つの方法で、画面に画像を表示する必要がなければ、Intentオブジェクトをインスタンス化した後、次のコードを使用してカメラを開くことができます.
startActivity(intent);
二、次の例では、持参したシステムカメラを呼び出して写真を撮り、写真を撮った後に画像を表示することを確認しますが、次の例はIntentの意図によってトリガーされるため、カメラアプリはフルサイズの画像をメインアクティビティに戻すことはできませんので、小さなサムネイルを1枚だけ返します.1、まず、Androidプロジェクトを新設し、プロジェクト名はIntent_camera、まず、インタフェースをレイアウトし、デフォルトのactivity_を開きます.main.xmlファイルには、ButtonコントロールとImageViewコントロールがあります.コードは次のとおりです.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" "/>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
LinearLayout>
2、その後、デフォルトのMainActivityクラスファイルを開き、主にIntent呼び出しシステムによってカメラを持参し、startActivity ForResult()メソッドによってカメラを開き、onActivity Result()によって転送された画像を受信します.コードは以下の通りです.
package com.example.intent_camera;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
final static int CAMERA_RESULT = 0;// ,
private Button button;// Button
private ImageView imageView;// ImageView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//
button = (Button) findViewById(R.id.button);// Button
imageView = (ImageView) findViewById(R.id.imageView);// ImageView
//
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// Intent , MediaStore ACTION_IMAGE_CAPTURE
startActivityForResult(intent, CAMERA_RESULT);// , Intent
}
});
}
/**
* onActivityResult() , , ,
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, intent);
if(resultCode==RESULT_OK){
Bundle extras=intent.getExtras();// Intent
Bitmap bitmap=(Bitmap) extras.get("data");//
imageView.setImageBitmap(bitmap);//
}
}
}
3、これを本体に配置し、撮影ボタンをクリックした後、システムが持っているカメラを開き、撮影が終わったら確認またはチェックボタンを押すと、写真を撮る活動に画像を表示できますが、サムネイルです.
三、以上の内容は、皆さんの勉強の参考になります.ありがとうございます.