Android圖像處理簡介の使用內置Camera應用程序進行圖像捕獲

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的專欄

發佈留言