Android影像處理簡介の使用內建Camera應用程式進行映像捕獲

來源:互聯網
上載者:User

Android中可以簡單直接地使用intent來擷取已安裝應用軟體提供的功能,它是Android的關鍵組件之一,主要作用有兩個:一是觸發其他應用程式提供的功能;二是在單個應用程式中實現Activity之間的切換。
軟體開發人員使用intent filter來聲明應用程式提供某種特定功能,這個聲明是在AndroidManifest.xml中進行的,例如,內建的Camera應用在它的manifest檔案中的"Camera"標籤下進行了如下聲明:
[html]
<intent-filter> 
 
<action android:name="android.media.action.IMAGE_CAPTURE" /> 
 
    <action android:name="android.intent.category.DEFAULT"/> 
 
</intent-filter> 

要通過intent來使用Camera應用,我們只需建立一個Intent來捕獲上面聲明的filter就行,代碼如下:
[java]
Intent it = new Intent("android.media.action.IMAGE_CAPTURE"); 
但上面代碼顯然屬於寫入程式碼,字串"android.media.action.IMAGE_CAPTURE"將來如果改變了,我們的代碼也得跟著修改,不利於維護,好在MediaStore類提供常量ACTION_IMAGE_CAPTURE供開發人員使用,這樣字串名稱變動就在Android內部自己解決,對外的介面ACTION_IMAGE_CAPTURE不變,改進後的代碼如下:
[java]
Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
 
startActivity(it); 
1)從Camera應用返回資料
只捕獲映像而不進行儲存或其他處理是沒有任何意義的,為了獲得Camera應用捕獲到的映像,我們只需使用startActivityForResult函數代替startActivity,同時重載Activity的函數onActivityResult即可,從Camera返回的資料我們當作Bitmap來處理,代碼如下:
[java]
<span style="font-size:18px;">package hust.iprai.asce1885.promedia; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.widget.ImageView; 
 
public class ImageCaptureActivity extends Activity { 
     
    final static int CAMERA_RESULT = 0; 
    ImageView iv = null; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(it, CAMERA_RESULT); 
    } 
 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        super.onActivityResult(requestCode, resultCode, data); 
         
      if (RESULT_OK == resultCode) { 
        // Get Extra from the intent 
        Bundle extras = data.getExtras(); 
        // Get the returned image from extra 
        Bitmap bmp = (Bitmap) extras.get("data"); 
             
        iv = (ImageView) findViewById(R.id.ReturnedImageView); 
        iv.setImageBitmap(bmp); 
      } 
    } 
}</span> 
對應的layout/main.xml檔案如下:
[html]
<?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" 
 
    > 
 
<ImageView android:id="@+id/ReturnedImageView" 
 
android:layout_width="wrap_content" 
 
android:layout_height="wrap_content" /> 
 
</LinearLayout> 
編譯運行上面的代碼,我們發現捕獲的映像很小,這是因為Camera應用當被intent觸發時,它並不會給調用它的Activity返回完整大小的映像,這樣做是考慮到行動裝置記憶體有限,而完整的映像佔用的記憶體空間不小。
2)儲存捕獲的映像
如果我們想直接將網路攝影機捕獲的映像儲存為圖片,可以在調用Camera應用時傳遞一個額外參數,並指定儲存映像的URI即可。額外參數的名稱是定義在MediaStore中的常量EXTRA_OUTPUT。下面的程式碼片段就是將Camera應用捕獲的映像儲存到SD卡中,命名為myfavoritepicture.jpg:
[java]
String imageFilePath = 
 
  Environment.getExternalStorageDirectory().getAbsolutePath() +  
 
    "/myfavoritepicture.jpg"; 
 
File imageFile = new File(imageFilePath); 
 
Uri imageFileUri = Uri.fromFile(imageFile); 
 
Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
 
it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); 
 
startActivityForResult(it, CAMERA_RESULT); 

3)顯示大映像
載入和顯示映像通常會佔用較多的記憶體空間,為了減少記憶體用盡的可能性,Android提供了稱為BitmapFactory的工具類,它提供了一系列的靜態函數從不同的來源載入Bitmap映像。先來關注BitmapFactory.Options類,它允許我們定義將位元影像讀入記憶體的方式。例如,我們可以設定BitmapFactory載入映像時使用的樣本大小,這隻需設定BitmapFactory.Options.inSampleSize的值就行,下面的程式碼片段指示將inSampleSize設為8,這將使載入的映像是原映像大小的1/8:
[java]
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
 
bmpFactoryOptions.inSampleSize = 8; 
 
Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions); 

這是載入大映像的快速方法,但它沒有考慮映像的實際大小以及手機螢幕的大小,實際中,縮放映像後通常需要適合螢幕的大小。
我們一般會根據螢幕的尺寸來計算inSampleSize的值,擷取顯示螢幕寬高的代碼如下:
[java]
Display currentDisplay = getWindowManager().getDefaultDisplay(); 
 
int dw = currentDisplay.getWidth(); 
 
int dh = currentDisplay.getHeight(); 
而要擷取映像的實際大小,我們還是使用BitmapFactory.Options類,並將BitmapFactory.Options.inJustDecodeBounds變數設為true,這將告訴BitmapFactory類在只計算出映像的大小,而不實際進行映像的解碼:
[java]
// Load up the image's dimensions not the image itself 
 
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
 
bmpFactoryOptions.inJustDecodeBounds = true; 
 
Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions); 
 
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight/(float)dh); 
 
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth/(float)dw); 
上面計算出高和寬的比率如果都大於1時,我們縮小映像時取較大的比率作為inSampleSize的值:
[java]
// If both of the ratios are greater than 1, 
 
// one of the sides of the image is greater than the screen 
 
if ((heightRatio > 1) && (widthRatio > 1)) { 
 
    if (heightRatio > widthRatio) { 
 
    // Height ratio is larger, scale according to it 
 
    bmpFactoryOptions.inSampleSize = heightRatio; 
 
    } else { 
 
    // Width ratio is larger, scale according to it  
 
    bmpFactoryOptions.inSampleSize = widthRatio; 
 
    } 
 

 
// Decode it for real 
 
bmpFactoryOptions.inJustDecodeBounds = false; 
 
bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions); 
完整的顯示大映像的代碼如下所示,先看layout/main.xml檔案:
[java]
<?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" 
 
    > 
 
<ImageView android:id="@+id/ReturnedImageView" 
 
android:layout_width="wrap_content" 
 
android:layout_height="wrap_content" /> 
 
</LinearLayout> 
接著就是Java代碼部分了:
[java]
package hust.iprai.asce1885.promedia; 
 
import java.io.File; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.view.Display; 
import android.widget.ImageView; 
 
public class SizedCameraActivity extends Activity { 
    final static int CAMERA_RESULT = 0; 
    ImageView iv = null; 
    String imageFilePath = ""; 
     
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() +  
                "/myfavoritepicture.jpg"; 
        File imageFile = new File(imageFilePath); 
        Uri imageFileUri = Uri.fromFile(imageFile); 
         
        Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
        it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); 
        startActivityForResult(it, CAMERA_RESULT); 
    } 
     
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        super.onActivityResult(requestCode, resultCode, data); 
         
        if (RESULT_OK == resultCode) { 
            iv = (ImageView) findViewById(R.id.ReturnedImageView); 
             
            Display currentDisplay = getWindowManager().getDefaultDisplay(); 
            int dw = currentDisplay.getWidth(); 
            int dh = currentDisplay.getHeight(); 
             
            // Load up the image's dimensions not the image itself 
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
            bmpFactoryOptions.inJustDecodeBounds = true; 
            Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions); 
             
            int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight/(float)dh); 
            int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth/(float)dw); 
             
            Log.v("HEIGHTRATIO", "" + heightRatio); 
            Log.v("WIDTHRATIO", "" + widthRatio); 
             
            // If both of the ratios are greater than 1, 
            // one of the sides of the image is greater than the screen 
            if ((heightRatio > 1) && (widthRatio > 1)) { 
                if (heightRatio > widthRatio) { 
                    // Height ratio is larger, scale according to it 
                    bmpFactoryOptions.inSampleSize = heightRatio; 
                } else { 
                    // Width ratio is larger, scale according to it  
                    bmpFactoryOptions.inSampleSize = widthRatio; 
                } 
            } 
             
            // Decode it for real 
            bmpFactoryOptions.inJustDecodeBounds = false; 
            bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions); 
             
            // Display it 
            iv.setImageBitmap(bmp); 
        }    
    } 


摘自 ASCE1885的專欄

聯繫我們

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