Android图像处理简介の使用内置Camera应用程序进行图像捕获
2011-12-18 15:03
585 查看
Android中可以简单直接地使用intent来获取已安装应用软件提供的功能,它是Android的关键组件之一,主要作用有两个:一是触发其他应用程序提供的功能;二是在单个应用程序中实现Activity之间的切换。
软件开发人员使用intent filter来声明应用程序提供某种特定功能,这个声明是在AndroidManifest.xml中进行的,例如,内置的Camera应用在它的manifest文件中的"Camera"标签下进行了如下声明:
要通过intent来使用Camera应用,我们只需创建一个Intent来捕获上面声明的filter就行,代码如下:
但上面代码显然属于硬编码,字符串"android.media.action.IMAGE_CAPTURE"将来如果改变了,我们的代码也得跟着修改,不利于维护,好在MediaStore类提供常量ACTION_IMAGE_CAPTURE供开发人员使用,这样字符串名称变动就在Android内部自己解决,对外的接口ACTION_IMAGE_CAPTURE不变,改进后的代码如下:
1)从Camera应用返回数据
只捕获图像而不进行存储或其他处理是没有任何意义的,为了获得Camera应用捕获到的图像,我们只需使用startActivityForResult函数代替startActivity,同时重载Activity的函数onActivityResult即可,从Camera返回的数据我们当作Bitmap来处理,代码如下:
对应的layout/main.xml文件如下:
编译运行上面的代码,我们发现捕获的图像很小,这是因为Camera应用当被intent触发时,它并不会给调用它的Activity返回完整大小的图像,这样做是考虑到移动设备内存有限,而完整的图像占用的内存空间不小。
2)保存捕获的图像
如果我们想直接将摄像头捕获的图像保存为图片,可以在调用Camera应用时传递一个额外参数,并指定存储图像的URI即可。额外参数的名称是定义在MediaStore中的常量EXTRA_OUTPUT。下面的代码片段就是将Camera应用捕获的图像保存到SD卡中,命名为myfavoritepicture.jpg:
3)显示大图像
加载和显示图像通常会占用较多的内存空间,为了减少内存用尽的可能性,Android提供了称为BitmapFactory的工具类,它提供了一系列的静态函数从不同的来源加载Bitmap图像。先来关注BitmapFactory.Options类,它允许我们定义将位图读入内存的方式。例如,我们可以设定BitmapFactory加载图像时使用的样本大小,这只需设置BitmapFactory.Options.inSampleSize的值就行,下面的代码片段指示将inSampleSize设为8,这将使加载的图像是原图像大小的1/8:
这是加载大图像的快速方法,但它没有考虑图像的实际大小以及手机屏幕的大小,实际中,缩放图像后通常需要适合屏幕的大小。
我们一般会根据屏幕的尺寸来计算inSampleSize的值,获取显示屏幕宽高的代码如下:
而要获取图像的实际大小,我们还是使用BitmapFactory.Options类,并将BitmapFactory.Options.inJustDecodeBounds变量设为true,这将告诉BitmapFactory类在只计算出图像的大小,而不实际进行图像的解码:
上面计算出高和宽的比率如果都大于1时,我们缩小图像时取较大的比率作为inSampleSize的值:
完整的显示大图像的代码如下所示,先看layout/main.xml文件:
接着就是Java代码部分了:
软件开发人员使用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); } } }
相关文章推荐
- Android图像处理简介の使用内置Camera应用程序进行图像捕获
- Android使用内置Camera应用程序进行图像捕获
- Android 使用内置的Camera应用程序捕获图像
- Android 使用内置的Camera应用程序捕获图像
- Android 使用内置的Camera应用程序捕获图像
- [Android] 使用Matrix矩阵类对图像进行缩放、旋转、对照度、亮度处理
- [Android] 使用Matrix矩阵类对图像进行缩放、旋转、对比度、亮度处理
- Android应用程序中,使用Glide下载和处理图像的教程
- **ANDROID**# 第七章图形与图像处理(静态处理) > Bitmap是有像素点构成的点阵图。 ------ ## 使用简单的图片 ---- * 通过Drawable对象进行访问。
- 使用内置的Camera应用程序捕获图像
- 使用内置的Camera应用程序捕获图像
- android 使用NDK进行图像处理(openCV)时的图像转换问题(灰度图显示)
- 使用内置的Camera应用程序捕获图像
- [置顶] Android 之Camera1 使用 以及camera 图像的处理
- [Android] 使用Matrix矩阵类对图像进行缩放、旋转、对比度、亮度处理
- android开发在jni中使用Opencv进行图像处理,环境搭建篇
- 使用ABBYY FineReader进行自动图像预处理
- opencv图像边缘检测使用,先做平滑处理在进行边缘检测
- 【转】详解使用tcpdump、wireshark对Android应用程序进行抓包并分析
- 使用BP神经网络和图像处理 进行的 识别纺织机纱管