生活随笔
收集整理的這篇文章主要介紹了
Android 使用内置的Camera应用程序捕获图像
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本Demo的實現效果是調用手機上已安裝的照相機來實現拍照的功能,拍好的照片以ImageView形式展示。
目的:學習手機調用安裝的相機照相,對大的圖片處理有所認識,這里主要用到BitmapFactory和BitmapFactory.Options兩個類。
載入并顯示一副圖像對內存使用情況有顯著的影響,Android提供了一個名為BitmapFactory 的有用程序類,該程序提供了一系列的靜態方法,同意通過各種來源載入Bitmap圖像。
針對我們的需求,將從文件載入圖像。并在最初的活動中顯示它。幸運的是,BitmapFactory中的可用方法將會調用BitmapFactory.Options類。這使得我們可以定義怎樣將Bitmap讀入內存。詳細而言,當載入圖像時。可以設置BitmapFactory應該使用的採樣大小。在BitmapFactory.Options中指定inSampleSize參數。
比如。將inSampleSize = 8時。產生一幅圖的大小是原始大小的1/8。要注意的是首先應將BitmapFactoryOptions.inJustDecodeBounds變量設置為true,這將通知BitmapFactory類僅僅需返回該圖像的范圍。而無需嘗試解碼圖像本身。
最后將BitmapFactory.Options.inJustDecodeBounds設置為false。最后對其進行真正的解碼。
實現效果圖:
源碼:
activity_main布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><ImageViewandroid:id="@+id/imageView"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>
MainActivity源碼:
package com.multimediademo1;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.view.Display;
import android.widget.ImageView;public class MainActivity extends Activity {private final static int CAMERA_RESULT = 0;private ImageView imageView;private String imageFilePath;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myfavoritepicture.jpg";File imageFile = new File(imageFilePath);Uri imageFileUri = Uri.fromFile(imageFile);Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);startActivityForResult(intent, CAMERA_RESULT);}@Overrideprotected void onActivityResult(int requestCode, int resultCode,Intent intent) {super.onActivityResult(requestCode, resultCode, intent);if (resultCode == RESULT_OK) {imageView = (ImageView) findViewById(R.id.imageView);Display currentDisplay = getWindowManager().getDefaultDisplay();int dw = currentDisplay.getWidth();int dh = currentDisplay.getHeight();// 載入圖像的尺寸,而不是圖像本身BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath, options);int heightRatio = (int) Math.ceil(options.outHeight / (float) dh);int widthRatio = (int) Math.ceil(options.outWidth / (float) dw);// 假設兩個比率都大于1。那么圖像的一條邊將大于屏幕if (heightRatio > 1 && widthRatio > 1) {if (heightRatio > widthRatio) {// 若高度比率更大,則依據它縮放options.inSampleSize = heightRatio;} else {options.inSampleSize = widthRatio;}}options.inJustDecodeBounds = false;bitmap = BitmapFactory.decodeFile(imageFilePath, options);imageView.setImageBitmap(bitmap);}}}
源碼下載:
點擊下載源代碼
總結
以上是生活随笔為你收集整理的Android 使用内置的Camera应用程序捕获图像的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。