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

來源:互聯網
上載者:User

Android中可以簡單直接地使用intent來擷取已安裝應用軟體提供的功能,它是Android的關鍵組件之一,主要作用有兩個:一是觸發其他應用程式提供的功能;二是在單個應用程式中實現Activity之間的切換。

軟體開發人員使用intent filter來聲明應用程式提供某種特定功能,這個聲明是在AndroidManifest.xml中進行的,例如,內建的Camera應用在它的manifest檔案中的"Camera"標籤下進行了如下聲明:

<intent-filter></p><p><action android:name="android.media.action.IMAGE_CAPTURE" /></p><p> <action android:name="android.intent.category.DEFAULT"/></p><p></intent-filter>

要通過intent來使用Camera應用,我們只需建立一個Intent來捕獲上面聲明的filter就行,代碼如下:

Intent it = new Intent("android.media.action.IMAGE_CAPTURE");

但上面代碼顯然屬於寫入程式碼,字串"android.media.action.IMAGE_CAPTURE"將來如果改變了,我們的代碼也得跟著修改,不利於維護,好在MediaStore類提供常量ACTION_IMAGE_CAPTURE供開發人員使用,這樣字串名稱變動就在Android內部自己解決,對外的介面ACTION_IMAGE_CAPTURE不變,改進後的代碼如下:

Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);</p><p>startActivity(it);

1)從Camera應用返回資料

只捕獲映像而不進行儲存或其他處理是沒有任何意義的,為了獲得Camera應用捕獲到的映像,我們只需使用startActivityForResult函數代替startActivity,同時重載Activity的函數onActivityResult即可,從Camera返回的資料我們當作Bitmap來處理,代碼如下:

package hust.iprai.asce1885.promedia;</p><p>import android.app.Activity;<br />import android.content.Intent;<br />import android.graphics.Bitmap;<br />import android.os.Bundle;<br />import android.widget.ImageView;</p><p>public class ImageCaptureActivity extends Activity {</p><p> final static int CAMERA_RESULT = 0;<br /> ImageView iv = null;</p><p> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);<br /> startActivityForResult(it, CAMERA_RESULT);<br /> }</p><p> @Override<br /> protected void onActivityResult(int requestCode, int resultCode, Intent data) {<br /> super.onActivityResult(requestCode, resultCode, data);</p><p> if (RESULT_OK == resultCode) {<br />// Get Extra from the intent<br />Bundle extras = data.getExtras();<br />// Get the returned image from extra<br />Bitmap bmp = (Bitmap) extras.get("data");</p><p>iv = (ImageView) findViewById(R.id.ReturnedImageView);<br />iv.setImageBitmap(bmp);<br /> }<br /> }<br />}

對應的layout/main.xml檔案如下:

<?xml version="1.0" encoding="utf-8"?></p><p><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</p><p> android:orientation="vertical"</p><p> android:layout_width="fill_parent"</p><p> android:layout_height="fill_parent"</p><p> ></p><p><ImageView android:id="@+id/ReturnedImageView"</p><p>android:layout_width="wrap_content"</p><p>android:layout_height="wrap_content" /></p><p></LinearLayout>

編譯運行上面的代碼,我們發現捕獲的映像很小,這是因為Camera應用當被intent觸發時,它並不會給調用它的Activity返回完整大小的映像,這樣做是考慮到行動裝置記憶體有限,而完整的映像佔用的記憶體空間不小。

2)儲存捕獲的映像

如果我們想直接將網路攝影機捕獲的映像儲存為圖片,可以在調用Camera應用時傳遞一個額外參數,並指定儲存映像的URI即可。額外參數的名稱是定義在MediaStore中的常量EXTRA_OUTPUT。下面的程式碼片段就是將Camera應用捕獲的映像儲存到SD卡中,命名為myfavoritepicture.jpg:

String imageFilePath =</p><p> Environment.getExternalStorageDirectory().getAbsolutePath() + </p><p> "/myfavoritepicture.jpg";</p><p>File imageFile = new File(imageFilePath);</p><p>Uri imageFileUri = Uri.fromFile(imageFile);</p><p>Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);</p><p>it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);</p><p>startActivityForResult(it, CAMERA_RESULT);

3)顯示大映像

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

BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();</p><p>bmpFactoryOptions.inSampleSize = 8;</p><p>Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);

這是載入大映像的快速方法,但它沒有考慮映像的實際大小以及手機螢幕的大小,實際中,縮放映像後通常需要適合螢幕的大小。

我們一般會根據螢幕的尺寸來計算inSampleSize的值,擷取顯示螢幕寬高的代碼如下:

Display currentDisplay = getWindowManager().getDefaultDisplay();</p><p>int dw = currentDisplay.getWidth();</p><p>int dh = currentDisplay.getHeight();

而要擷取映像的實際大小,我們還是使用BitmapFactory.Options類,並將BitmapFactory.Options.inJustDecodeBounds變數設為true,這將告訴BitmapFactory類在只計算出映像的大小,而不實際進行映像的解碼:

// Load up the image's dimensions not the image itself</p><p>BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();</p><p>bmpFactoryOptions.inJustDecodeBounds = true;</p><p>Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);</p><p>int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight/(float)dh);</p><p>int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth/(float)dw);

上面計算出高和寬的比率如果都大於1時,我們縮小映像時取較大的比率作為inSampleSize的值:

// If both of the ratios are greater than 1,</p><p>// one of the sides of the image is greater than the screen</p><p>if ((heightRatio > 1) && (widthRatio > 1)) {</p><p> if (heightRatio > widthRatio) {</p><p> // Height ratio is larger, scale according to it</p><p> bmpFactoryOptions.inSampleSize = heightRatio;</p><p> } else {</p><p> // Width ratio is larger, scale according to it </p><p> bmpFactoryOptions.inSampleSize = widthRatio;</p><p> }</p><p>}</p><p>// Decode it for real</p><p>bmpFactoryOptions.inJustDecodeBounds = false;</p><p>bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);

完整的顯示大映像的代碼如下所示,先看layout/main.xml檔案:

<?xml version="1.0" encoding="utf-8"?></p><p><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</p><p> android:orientation="vertical"</p><p> android:layout_width="fill_parent"</p><p> android:layout_height="fill_parent"</p><p> ></p><p><ImageView android:id="@+id/ReturnedImageView"</p><p>android:layout_width="wrap_content"</p><p>android:layout_height="wrap_content" /></p><p></LinearLayout>

接著就是Java代碼部分了:

package hust.iprai.asce1885.promedia;</p><p>import java.io.File;</p><p>import android.app.Activity;<br />import android.content.Intent;<br />import android.graphics.Bitmap;<br />import android.graphics.BitmapFactory;<br />import android.net.Uri;<br />import android.os.Bundle;<br />import android.os.Environment;<br />import android.util.Log;<br />import android.view.Display;<br />import android.widget.ImageView;</p><p>public class SizedCameraActivity extends Activity {<br />final static int CAMERA_RESULT = 0;<br />ImageView iv = null;<br />String imageFilePath = "";</p><p>@Override<br />protected void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.main);</p><p>String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() +<br />"/myfavoritepicture.jpg";<br />File imageFile = new File(imageFilePath);<br />Uri imageFileUri = Uri.fromFile(imageFile);</p><p>Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);<br />it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);<br />startActivityForResult(it, CAMERA_RESULT);<br />}</p><p>@Override<br />protected void onActivityResult(int requestCode, int resultCode, Intent data) {<br />super.onActivityResult(requestCode, resultCode, data);</p><p>if (RESULT_OK == resultCode) {<br />iv = (ImageView) findViewById(R.id.ReturnedImageView);</p><p>Display currentDisplay = getWindowManager().getDefaultDisplay();<br />int dw = currentDisplay.getWidth();<br />int dh = currentDisplay.getHeight();</p><p>// Load up the image's dimensions not the image itself<br />BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();<br />bmpFactoryOptions.inJustDecodeBounds = true;<br />Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);</p><p>int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight/(float)dh);<br />int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth/(float)dw);</p><p>Log.v("HEIGHTRATIO", "" + heightRatio);<br />Log.v("WIDTHRATIO", "" + widthRatio);</p><p>// If both of the ratios are greater than 1,<br />// one of the sides of the image is greater than the screen<br />if ((heightRatio > 1) && (widthRatio > 1)) {<br />if (heightRatio > widthRatio) {<br />// Height ratio is larger, scale according to it<br />bmpFactoryOptions.inSampleSize = heightRatio;<br />} else {<br />// Width ratio is larger, scale according to it<br />bmpFactoryOptions.inSampleSize = widthRatio;<br />}<br />}</p><p>// Decode it for real<br />bmpFactoryOptions.inJustDecodeBounds = false;<br />bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);</p><p>// Display it<br />iv.setImageBitmap(bmp);<br />}<br />}<br />}

 

相關文章

聯繫我們

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