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

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

	}

}

源码下载:

点击下载源代码


原文地址:https://www.cnblogs.com/zhchoutai/p/6803990.html