Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> 使用android內置相機拍照

使用android內置相機拍照

編輯:高級開發

 主程序

  public class AndroCamera extends Activity {

  private static final int IMAGE_CAPTURE = 0;

  private Button startBtn;

  private Uri imageUri;

  private ImageView imageVIEw;

  /** Called when the activity is first created.

  * sets the content and gets the references to

  * the basic widgets on the screen like

  * {@code Button} or {@link ImageVIEw}

  */

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentVIEw(R.layout.main);

  imageView = (ImageView)findVIEwById(R.id.img);

  startBtn = (Button) findVIEwById(R.id.startBtn);

  startBtn.setOnClickListener(new VIEw.OnClickListener() {

  public void onClick(VIEw v) {

  startCamera();

  }

  });

  }

  public void startCamera() {

  Log.d("ANDRO_CAMERA", "Starting camera on the phone...");

  String fileName = "testphoto.jpg";

  ContentValues values = new ContentValues();

  values.put(MediaStore.Images.Media.TITLE, fileName);

  values.put(MediaStore.Images.Media.DESCRIPTION,

  "Image capture by camera");

  values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");

  imageUri = getContentResolver().insert(

  MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

  intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

  intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

  startActivityForResult(intent, IMAGE_CAPTURE);

  }

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (requestCode == IMAGE_CAPTURE) {

  if (resultCode == RESULT_OK){

  Log.d("ANDRO_CAMERA","Picture taken!!!");

  接上頁

  imageVIEw.setImageURI(imageUri);

  }

  }

  }

  頁面文件

  < ?XML version="1.0" encoding="utf-8"?>

  < LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"

  android:orIEntation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  >

  < Button android:text="Start Camera"

  android:id="@+id/startBtn"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content">

  < /Button>

  < ImageVIEw android:id="@+id/img"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent">

  < /ImageVIEw>

  < /LinearLayout >

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved