---
title: "Android图像处理简介の使用内置Camera应用程序进行图像捕获"
author: "mmdev"
date: "2011-12-18T15:03:00Z"
tags: []
slug: "android-camera"
draft: false
lastmod: "2012-07-23T23:39:46Z"
id: 1604757
url: "https://mmdev.iteye.com/blog/1604757"
---

Android中可以简单直接地使用intent来获取已安装应用软件提供的功能，它是Android的关键组件之一，主要作用有两个：一是触发其他应用程序提供的功能；二是在单个应用程序中实现Activity之间的切换。

软件开发人员使用intent filter来声明应用程序提供某种特定功能，这个声明是在AndroidManifest.xml中进行的，例如，内置的Camera应用在它的manifest文件中的"Camera"标签下进行了如下声明：

```
<intent-filter>

<action android:name="android.media.action.IMAGE_CAPTURE" />

    <action android:name="android.intent.category.DEFAULT"/>

</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);

startActivity(it);
```

1）从Camera应用返回数据

只捕获图像而不进行存储或其他处理是没有任何意义的，为了获得Camera应用捕获到的图像，我们只需使用startActivityForResult函数代替startActivity，同时重载Activity的函数onActivityResult即可，从Camera返回的数据我们当作Bitmap来处理，代码如下：

```
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);
	  }
    }
}
```

对应的layout/main.xml文件如下：

```
<?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：

```
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：

```
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();

bmpFactoryOptions.inSampleSize = 8;

Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
```

这是加载大图像的快速方法，但它没有考虑图像的实际大小以及手机屏幕的大小，实际中，缩放图像后通常需要适合屏幕的大小。

我们一般会根据屏幕的尺寸来计算inSampleSize的值，获取显示屏幕宽高的代码如下：

```
Display currentDisplay = getWindowManager().getDefaultDisplay();

int dw = currentDisplay.getWidth();

int dh = currentDisplay.getHeight();
```

而要获取图像的实际大小，我们还是使用BitmapFactory.Options类，并将BitmapFactory.Options.inJustDecodeBounds变量设为true，这将告诉BitmapFactory类在只计算出图像的大小，而不实际进行图像的解码：

```
// 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的值：

```
// 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文件：

```
<?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代码部分了：

```
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);
		}	
	}
}
```