Android使用内置Camera应用程序进行图像捕获

来源:互联网 发布:零点网络 编辑:程序博客网 时间:2024/05/17 01:35

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

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

  1. <intent-filter>  
  2.   
  3. <action android:name="android.media.action.IMAGE_CAPTURE" />  
  4.   
  5.     <action android:name="android.intent.category.DEFAULT"/>  
  6.   
  7. </intent-filter>  


要通过intent来使用Camera应用,我们只需创建一个Intent来捕获上面声明的filter就行,代码如下:

  1. Intent it = new Intent("android.media.action.IMAGE_CAPTURE");  

但上面代码显然属于硬编码,字符串"android.media.action.IMAGE_CAPTURE"将来如果改变了,我们的代码也得跟着修改,不利于维护,好在MediaStore类提供常量ACTION_IMAGE_CAPTURE供开发人员使用,这样字符串名称变动就在Android内部自己解决,对外的接口ACTION_IMAGE_CAPTURE不变,改进后的代码如下:

  1. Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
  2.   
  3. startActivity(it);  

1)从Camera应用返回数据

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

  1. <span style="font-size:18px;">package hust.iprai.asce1885.promedia;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.graphics.Bitmap;  
  6. import android.os.Bundle;  
  7. import android.widget.ImageView;  
  8.   
  9. public class ImageCaptureActivity extends Activity {  
  10.       
  11.     final static int CAMERA_RESULT = 0;  
  12.     ImageView iv = null;  
  13.       
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.           
  19.         Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
  20.         startActivityForResult(it, CAMERA_RESULT);  
  21.     }  
  22.   
  23.     @Override  
  24.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  25.         super.onActivityResult(requestCode, resultCode, data);  
  26.           
  27.       if (RESULT_OK == resultCode) {  
  28.         // Get Extra from the intent  
  29.         Bundle extras = data.getExtras();  
  30.         // Get the returned image from extra  
  31.         Bitmap bmp = (Bitmap) extras.get("data");  
  32.               
  33.         iv = (ImageView) findViewById(R.id.ReturnedImageView);  
  34.         iv.setImageBitmap(bmp);  
  35.       }  
  36.     }  
  37. }</span>  

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

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.     android:orientation="vertical"  
  6.   
  7.     android:layout_width="fill_parent"  
  8.   
  9.     android:layout_height="fill_parent"  
  10.   
  11.     >  
  12.   
  13. <ImageView android:id="@+id/ReturnedImageView"  
  14.   
  15. android:layout_width="wrap_content"  
  16.   
  17. android:layout_height="wrap_content" />  
  18.   
  19. </LinearLayout>  

编译运行上面的代码,我们发现捕获的图像很小,这是因为Camera应用当被intent触发时,它并不会给调用它的Activity返回完整大小的图像,这样做是考虑到移动设备内存有限,而完整的图像占用的内存空间不小。

2)保存捕获的图像

如果我们想直接将摄像头捕获的图像保存为图片,可以在调用Camera应用时传递一个额外参数,并指定存储图像的URI即可。额外参数的名称是定义在MediaStore中的常量EXTRA_OUTPUT。下面的代码片段就是将Camera应用捕获的图像保存到SD卡中,命名为myfavoritepicture.jpg:

  1. String imageFilePath =  
  2.   
  3.   Environment.getExternalStorageDirectory().getAbsolutePath() +   
  4.   
  5.     "/myfavoritepicture.jpg";  
  6.   
  7. File imageFile = new File(imageFilePath);  
  8.   
  9. Uri imageFileUri = Uri.fromFile(imageFile);  
  10.   
  11. Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
  12.   
  13. it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);  
  14.   
  15. startActivityForResult(it, CAMERA_RESULT);  


3)显示大图像

加载和显示图像通常会占用较多的内存空间,为了减少内存用尽的可能性,Android提供了称为BitmapFactory的工具类,它提供了一系列的静态函数从不同的来源加载Bitmap图像。先来关注BitmapFactory.Options类,它允许我们定义将位图读入内存的方式。例如,我们可以设定BitmapFactory加载图像时使用的样本大小,这只需设置BitmapFactory.Options.inSampleSize的值就行,下面的代码片段指示将inSampleSize设为8,这将使加载的图像是原图像大小的1/8:

  1. BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();  
  2.   
  3. bmpFactoryOptions.inSampleSize = 8;  
  4.   
  5. Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);  


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

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

  1. Display currentDisplay = getWindowManager().getDefaultDisplay();  
  2.   
  3. int dw = currentDisplay.getWidth();  
  4.   
  5. int dh = currentDisplay.getHeight();  

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

  1. // Load up the image's dimensions not the image itself  
  2.   
  3. BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();  
  4.   
  5. bmpFactoryOptions.inJustDecodeBounds = true;  
  6.   
  7. Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);  
  8.   
  9. int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight/(float)dh);  
  10.   
  11. int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth/(float)dw);  

上面计算出高和宽的比率如果都大于1时,我们缩小图像时取较大的比率作为inSampleSize的值:

  1. // If both of the ratios are greater than 1,  
  2.   
  3. // one of the sides of the image is greater than the screen  
  4.   
  5. if ((heightRatio > 1) && (widthRatio > 1)) {  
  6.   
  7.     if (heightRatio > widthRatio) {  
  8.   
  9.     // Height ratio is larger, scale according to it  
  10.   
  11.     bmpFactoryOptions.inSampleSize = heightRatio;  
  12.   
  13.     } else {  
  14.   
  15.     // Width ratio is larger, scale according to it   
  16.   
  17.     bmpFactoryOptions.inSampleSize = widthRatio;  
  18.   
  19.     }  
  20.   
  21. }  
  22.   
  23. // Decode it for real  
  24.   
  25. bmpFactoryOptions.inJustDecodeBounds = false;  
  26.   
  27. bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);  

完整的显示大图像的代码如下所示,先看layout/main.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.     android:orientation="vertical"  
  6.   
  7.     android:layout_width="fill_parent"  
  8.   
  9.     android:layout_height="fill_parent"  
  10.   
  11.     >  
  12.   
  13. <ImageView android:id="@+id/ReturnedImageView"  
  14.   
  15. android:layout_width="wrap_content"  
  16.   
  17. android:layout_height="wrap_content" />  
  18.   
  19. </LinearLayout>  

接着就是Java代码部分了:

  1. package hust.iprai.asce1885.promedia;  
  2.   
  3. import java.io.File;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.BitmapFactory;  
  9. import android.net.Uri;  
  10. import android.os.Bundle;  
  11. import android.os.Environment;  
  12. import android.util.Log;  
  13. import android.view.Display;  
  14. import android.widget.ImageView;  
  15.   
  16. public class SizedCameraActivity extends Activity {  
  17.     final static int CAMERA_RESULT = 0;  
  18.     ImageView iv = null;  
  19.     String imageFilePath = "";  
  20.       
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.           
  26.         String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() +   
  27.                 "/myfavoritepicture.jpg";  
  28.         File imageFile = new File(imageFilePath);  
  29.         Uri imageFileUri = Uri.fromFile(imageFile);  
  30.           
  31.         Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
  32.         it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);  
  33.         startActivityForResult(it, CAMERA_RESULT);  
  34.     }  
  35.       
  36.     @Override  
  37.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  38.         super.onActivityResult(requestCode, resultCode, data);  
  39.           
  40.         if (RESULT_OK == resultCode) {  
  41.             iv = (ImageView) findViewById(R.id.ReturnedImageView);  
  42.               
  43.             Display currentDisplay = getWindowManager().getDefaultDisplay();  
  44.             int dw = currentDisplay.getWidth();  
  45.             int dh = currentDisplay.getHeight();  
  46.               
  47.             // Load up the image's dimensions not the image itself  
  48.             BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();  
  49.             bmpFactoryOptions.inJustDecodeBounds = true;  
  50.             Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);  
  51.               
  52.             int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight/(float)dh);  
  53.             int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth/(float)dw);  
  54.               
  55.             Log.v("HEIGHTRATIO""" + heightRatio);  
  56.             Log.v("WIDTHRATIO""" + widthRatio);  
  57.               
  58.             // If both of the ratios are greater than 1,  
  59.             // one of the sides of the image is greater than the screen  
  60.             if ((heightRatio > 1) && (widthRatio > 1)) {  
  61.                 if (heightRatio > widthRatio) {  
  62.                     // Height ratio is larger, scale according to it  
  63.                     bmpFactoryOptions.inSampleSize = heightRatio;  
  64.                 } else {  
  65.                     // Width ratio is larger, scale according to it   
  66.                     bmpFactoryOptions.inSampleSize = widthRatio;  
  67.                 }  
  68.             }  
  69.               
  70.             // Decode it for real  
  71.             bmpFactoryOptions.inJustDecodeBounds = false;  
  72.             bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);  
  73.               
  74.             // Display it  
  75.             iv.setImageBitmap(bmp);  
  76.         }     
  77.     }  
  78. }  


原创粉丝点击