zoukankan      html  css  js  c++  java
  • 1.1.1 从Camera应用程序返回数据

       当然,在捕获一张照片时,如果Camera程序没有将图片返回给调用活动,那么简单的使用内置的Camera应用程序捕获图像将不具有真正的作用。而为了使用它真正有用,可以将活动中的startActivity方法替换成startActivityForResult方法。使用该方法将允许我们访问从Camera应用程序中返回的数据,它恰好是用户以位图(Bitmap)形式捕获的图像。

       一下是一个基本的示例。

     1 package com.bluemobi.nthm.showversion;
     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 CameraIntent extends Activity {
    10     private  final static int CAMERA_RESULT=0;
    11     private  ImageView imv;
    12     @Override
    13     protected void onCreate(Bundle savedInstanceState) {
    14         super.onCreate(savedInstanceState);
    15         setContentView(R.layout.cameraintentactivity);
    16         
    17         Intent i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    18         startActivityForResult(i, CAMERA_RESULT);
    19     }
    20     @Override
    21     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    22         super.onActivityResult(requestCode, resultCode, data);
    23         if(resultCode==RESULT_OK){
    24             Bundle extras=data.getExtras();
    25             Bitmap bmp=(Bitmap) extras.get("data");
    26             
    27             imv=(ImageView) findViewById(R.id.ReturnedImageView);
    28             imv.setImageBitmap(bmp);
    29         }
    30     }
    31       
    32 }

       它需要在项目的layout/cameraintentactivity.xml 文件中添加如下的内容:

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

        为了完成上述示例,一下是AndroidManifest.xml文件的内容。

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.bluemobi.nthm.showversion"
     4     android:versionCode="1"
     5     android:versionName="1.0" >
     6 
     7     <uses-sdk
     8         android:minSdkVersion="11"   android:targetSdkVersion="20" />
     9 
    10    <application
    11         android:icon="@drawable/ic_launcher"
    12         android:label="@string/app_name">
    13         <activity
    14             android:name=".CameraIntent"
    15             android:label="@string/app_name" >
    16             <intent-filter>
    17                 <action android:name="android.intent.action.MAIN" />
    18                 <category android:name="android.intent.category.LAUNCHER" />
    19             </intent-filter>
    20         </activity>
    21     </application>
    22 </manifest>

     在此示例中,Camera应用程序在一个通过意图传递的附加值(extra)中返回图像,而该意图将在onActivityResult方法中传递给主调活动。附加值的名称“data”,它包含一个Bitmap对象,需要从泛型对象将它强制转换过来。

    1             //从意图中获取附加值
    2             Bundle extras=data.getExtras();
    3             //从附加值中获取返回的对象
    4             Bitmap bmp=(Bitmap) extras.get("data");

       在我们的布局XML(layout/cameraintentactivity.xml)文件中,有一个ImageView对象。该ImageView是泛型视图的扩展,其支持图像的显示。由于我们有一个带有指定ReturnedImageView编号(id)的ImageView对象,因此需要在活动中获得它的引用,并通过setImageBitmap方法将它的Bitmap对象设置为返回的图像。这将使得应用程序用户能够查看这幅捕获的图像。

       为了获得ImageView对象的引用,使用在Activity类中指定的标准方法findViewById。该方法使得我们能够以编程的方式引用在布局XML文件中指定的元素,我们正在通过将元素id传递给setContentView来使用布局XML文件。上述示例在XML中以以下方式指定ImageView对象:

    1       <ImageView 
    2         android:id="@+id/ReturnedImageView"
    3         android:layout_width="wrap_content"
    4         android:layout_height="wrap_content"
    5         android:contentDescription="@string/about_us"/>

    为了引用ImageView并通知它显示来自Camera的Bitmap对象,使用以下代码。

    1         imv=(ImageView) findViewById(R.id.ReturnedImageView);
    2         imv.setImageBitmap(bmp);

    当运行这个示例时,你可能会注意到结果图像很小。这不是一个bug——相反这是一个经过精心设计的。当通过一个意图触发时,Camera应用程序不会将全尺寸的图像返回给主调活动。通常,这样做需要大量的内存,而移动设备一般内存方面受限。相反,Camera应用程序将在返回一幅很小的缩略图。

  • 相关阅读:
    zend guard 4/5 破解版和免过期办法,已补授权Key一枚,成功注册。
    一身冷汗,PHP应该禁用的函数
    CentOS 5.5 安装和卸载桌面
    php加速模块 eAccelerator open_basedir错误解决办法
    配置电信网通双线双IP的解决办法
    php安装igbinary模块
    ubuntu系统VNC服务器安装配置
    python3 之 闭包实例解析 Be
    python3 之 内置函数enumerate Be
    python3 之 匿名函数 Be
  • 原文地址:https://www.cnblogs.com/ZSS-Android/p/3922346.html
Copyright © 2011-2022 走看看