码迷,mamicode.com
首页 > 移动开发 > 详细

Android -- camera(1)

时间:2014-05-30 00:12:48      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:android   c   style   class   blog   code   

使用内置的Camera应用程序捕获图像                                              

所有带有合适硬件(摄像头)的原版Android设备都会附带Camera应用程序。Camera应用程序包含一个意图过滤器(intent filter),它使得开发人员能够提供与Camera应用程序同等的图像捕获能力,而不必构建他们自己的定制捕获例程。

Camera应用程序在其清单文件中指定了以下意图过滤器。这里显示的意图过滤器包含在"Camera"活动标记内。

<intent-filter> 
<action android:name="android.media.action.IMAGE_CAPTURE"/> 
<category android:name="android.intent.category.DEFAULT"/> 
</intent-filter>

为了通过一个意图利用Camera应用程序,我们所要做的仅仅是必须构造一个将由上述过滤器捕获的意图。

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

在实践中,我们可能不希望直接使用动作字符串创建意图。在这种情况下,可以指定MediaStore类中的常量ACTION_IMAGE_CAPTURE。应该使用常量而非字符串本身的原因在于,如果该字符串发生了改变(当然常量也可能会不断地改变),那么使用常量将使得我们的调用比之前使用字符串更有利于未来的变化。

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
startActivity(i);

从Camera应用程序返回数据                                                            

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

bubuko.com,布布扣
import android.app.Activity;  
import android.content.Intent;  
import android.graphics.Bitmap;  
import android.os.Bundle;  
import android.widget.ImageView;  

public class CameraIntent extends Activity {  

    final static int CAMERA_RESULT = 0;  

    ImageView imv;  

    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        Intent i = new Intent(android.provider  
                .MediaStore.ACTION_IMAGE_CAPTURE);  
        startActivityForResult(i, CAMERA_RESULT);  
    }  

    protected void onActivityResult(int requestCode, int resultCode,  
            Intent intent) {  
        super.onActivityResult(requestCode, resultCode, intent);  

        if (resultCode == RESULT_OK)  
        {  
            Get Bundle extras = intent.getExtras();  
            Bitmap bmp = (Bitmap) extras.get("data");  


            imv = (ImageView) findViewById(R.id.ReturnedImageView);  
            imv.setImageBitmap(bmp);  
        }  
    }  
}
bubuko.com,布布扣
bubuko.com,布布扣
<?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 
androidandroid:id="@+id/ReturnedImageView"
android:layout_width=  "wrap_content"
android:layout_height="wrap_content">
</ImageView> 
</LinearLayout>
bubuko.com,布布扣

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

//从意图中获取附加值  
Bundle extras = intent.getExtras();  
 
//从附加值中获取返回的图像  
Bitmap bmp = (Bitmap) extras.get("data");

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

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

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

 

 

 

转载请注明出处:http://www.cnblogs.com/yydcdut/p/3744874.html

Android -- camera(1),布布扣,bubuko.com

Android -- camera(1)

标签:android   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/yydcdut/p/3744874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!