Android調用系統相機功能

來源:互聯網
上載者:User

標籤:

  在常規應用開發過程中,我們經常會使用到手機的相機功能,通過調用系統相機方便快捷的協助我們實現拍照功能,本篇我將帶領大家實現一下,如何通過調用系統相機實現拍照。

  第一種:調用系統相機拍照,通過返回的照片縮圖展示我們的拍照圖片

  開啟系統相機:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent, TAKE_PICTURE);// 如果用Activity.RESULT_OK,onActivityResult()不會被回調

  處理拍照返回參數:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {    if (resultCode == RESULT_OK) {        // 擷取系統預設返回圖片(縮圖)        if (requestCode == TAKE_PICTURE) {            Bundle bundle = data.getExtras();            bitmap = (Bitmap) bundle.get("data");        }        // 顯示圖片        iv.setImageBitmap(bitmap);    }}

  是不是很簡單,不過有一點很不好,那就是返回的圖片是縮圖,那麼如何獲得拍攝的高清圖片呢?

  第二種:儲存拍照的照片到指定檔案,通過檔案路徑顯示拍照照片

private static final int ADD_IMAGE_ONE = 1;//拍攝照片傳回值private String path;//拍照後圖片儲存的路徑private String fileName;//儲存檔案的檔案名稱path = Environment.getExternalStorageDirectory() + "/CeShi/Image/";File file = new File(path);//建立路徑if (!file.exists()) {  file.mkdirs();}fileName = "copyImage.jpg";File f = new File(path+fileName);if(!f.exists()){  try {    f.createNewFile();//建立檔案  } catch (IOException e1) {    e1.printStackTrace();  }}Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));startActivityForResult(intent, ADD_IMAGE_ONE);

  拍攝完成後資料處理:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);if (resultCode == Activity.RESULT_OK){  switch (requestCode) {    case ADD_IMAGE_ONE://拍照照片//       Toast.makeText(mContext, "拍照完成", Toast.LENGTH_SHORT).show();          iv.setImageBitmap(BitmapFactory.decodeFile(path + fileName));      break;    default:      break;    }              }}        

  好了,關於調用系統相機完成照片拍攝的知識,就為總結完畢,希望對大家有所協助。源碼下載:http://pan.baidu.com/s/1sjZMUQx

Android調用系統相機功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.