Android擴充 - 拍照篇(Camera)

來源:互聯網
上載者:User

標籤:android   blog   http   java   使用   os   strong   檔案   

1.調用系統網路攝影機

  1.聲明常量和變數

  2.按鈕點擊事件,開啟系統網路攝影機

  3.重寫onActivityResult事件接收拍照返回

  4.組建檔案名返迴路徑

  5.儲存圖片

 

[java] view plaincopy 
  1. private static final int TAKE_PICTURE = 1;  
  2.   
  3. class btnGotoActivity02Listener implements OnClickListener {  
  4.   public void onClick(View v) {  
  5.     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //調用網路攝影機拍照  
  6.     startActivityForResult(intent, TAKE_PICTURE);  
  7.   }  
  8. }  
  9.   
  10. @Override  
  11. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  12.   String filename = "";  
  13.   try {  
  14.     if (resultCode == Activity.RESULT_OK && requestCode == TAKE_PICTURE) {  
  15.       Bundle bundle = data.getExtras();  
  16.       Bitmap bmp = (Bitmap)bundle.get("data");  
  17.       filename = saveImage(bmp);  
  18.     }  
  19.   } catch (Exception err) {  
  20.     Log.e("Exception", err.getLocalizedMessage());  
  21.   }  
  22. }  
  23.   
  24. public String buildFileName(){  
  25.   Date now = new Date();  
  26.   SimpleDateFormat formater = new SimpleDateFormat("yyMMdd_HHmmss");  
  27.   
  28.   String SDCARD = Environment.getExternalStorageDirectory() + "/";  
  29.   String basePath = SDCARD + "dier/photo/";  
  30.   
  31.   File dir = new File(basePath);  
  32.   dir.mkdirs();  
  33.     
  34.   return basePath + formater.format(now) + ".jpg";  
  35. }  
  36.   
  37. public String saveImage(Bitmap bmp) throws Exception {  
  38.   File file = new File(buildFileName());  
  39.   file.createNewFile();  
  40.   FileOutputStream oStream = new FileOutputStream(file);  
  41.   bmp.compress(Bitmap.CompressFormat.JPEG, 100, oStream); //100是照片品質,0-100,越大越好  
  42.   oStream.flush();  
  43.   oStream.close();  
  44.   return file.getPath();  
  45. }  


 
2.儲存完整解析度的照片

  使用上面的方法,儲存下來的圖片僅為原圖的1/4大小,如果需要儲存大照片則需要使用下面的方式

  缺點:此方法除了在指定的目錄下儲存圖片外,在系統拍照路徑下也會儲存下一張相同的圖片。

[java] view plaincopy 
    1. private static final int TAKE_PICTURE = 1;  
    2. private String TEMP_FILE = "";  
    3.   
    4. class btnGotoActivity02Listener implements OnClickListener {  
    5.   public void onClick(View v) {  
    6.     TEMP_FILE = buildFileName();  
    7.     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //調用網路攝影機拍照  
    8.     intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(TEMP_FILE))); //另存圖片地址  
    9.     startActivityForResult(intent, TAKE_PICTURE);  
    10.   }  
    11. }  
    12.   
    13. public String buildFileName(){  
    14.   Date now = new Date();  
    15.   SimpleDateFormat formater = new SimpleDateFormat("yyMMdd_HHmmss");  
    16.   
    17.   String SDCARD = Environment.getExternalStorageDirectory() + "/";  
    18.   String basePath = SDCARD + "dier/photo/";  
    19.   
    20.   File dir = new File(basePath);  
    21.   dir.mkdirs();  
    22.     
    23.   return basePath + formater.format(now) + ".jpg";  
    24. }  
    25.   
    26. @Override  
    27. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    28.   String filename = "";  
    29.   try {  
    30.     if (resultCode == Activity.RESULT_OK && requestCode == TAKE_PICTURE) {  
    31.       File file = new File(TEMP_FILE);  
    32.       if(file.exists()){  
    33.         filename = file.getPath();  
    34.         Toast.makeText(MainActivity.this, "圖片已儲存在:" + filename, 3000).show();  
    35.       }  
    36.       else {  
    37.         Toast.makeText(MainActivity.this, "親,我實在無能為力啊,你是用啥程式照的相片?", 5000).show();  
    38.       }  
    39.     }  
    40.   } catch (Exception err) {  
    41.     Log.e("Exception", err.getLocalizedMessage());  
    42.   }  
    43. }  

http://blog.csdn.net/dier4836/article/details/7364721

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.