歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 使用內置的Camera應用程序捕獲圖像【附源碼】

Android 使用內置的Camera應用程序捕獲圖像【附源碼】

日期:2017/3/1 9:44:10   编辑:Linux編程

本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,最後對其進行真正的解碼。

源代碼下載:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2014年資料/4月/19日/Android 使用內置的Camera應用程序捕獲圖像【附源碼】

下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm

實現效果圖:

源代碼

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" >

<ImageView
android: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;

@Override
protected 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);
}

@Override
protected 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相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved