Android图像处理简介の使用内置Camera应用程序进行图像捕获
来源:互联网 发布:mac word偏好设置在哪 编辑:程序博客网 时间:2024/06/06 04:41
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 intentBundle extras = data.getExtras();// Get the returned image from extraBitmap 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 itselfBitmapFactory.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 screenif ((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 realbmpFactoryOptions.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 = "";@Overrideprotected 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);}@Overrideprotected 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 itselfBitmapFactory.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 screenif ((heightRatio > 1) && (widthRatio > 1)) {if (heightRatio > widthRatio) {// Height ratio is larger, scale according to itbmpFactoryOptions.inSampleSize = heightRatio;} else {// Width ratio is larger, scale according to it bmpFactoryOptions.inSampleSize = widthRatio;}}// Decode it for realbmpFactoryOptions.inJustDecodeBounds = false;bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);// Display itiv.setImageBitmap(bmp);}}}
- Android图像处理简介の使用内置Camera应用程序进行图像捕获
- Android使用内置Camera应用程序进行图像捕获
- 使用内置的Camera应用程序捕获图像
- 使用内置的Camera应用程序捕获图像
- Android 使用内置的Camera应用程序捕获图像
- Android 使用内置的Camera应用程序捕获图像
- 使用内置的Camera捕获图像
- Android Camera的图像处理
- Android图像处理简介の图像存储和元数据
- Android图像处理简介の图像存储和元数据
- 使用 Java 进行图像处理 - 图像缩放
- 使用 Java 进行图像处理 - 图像裁剪
- 使用 Java 进行图像处理 - 图像翻转
- 使用GDI+进行图像处理
- 使用CImage进行图像处理
- 使用pillow进行图像处理
- 使用python进行图像处理
- 使用python进行图像处理
- 内核运行于用户上下文时,内核空间与用户空间通信的实现
- Linux网络编程IPv4和IPv6的inet_addr、inet_aton、inet_pton函数
- 卡耐基的人际沟通术之思维导图
- UBUNTU安装PHP
- Apache数组分析
- Android图像处理简介の使用内置Camera应用程序进行图像捕获
- ASP.NET的异步实现方式总结(一)
- VS2010中添加第三方库的路径
- 将Windows的桌面目录、收藏夹等设置到D盘
- netstat
- DB40性能测试
- 关于单CPU,多CPU上的原子操作
- SQL Server 语法
- 网络经典命令行---ping/netstat/tracert