一、今天,来介绍如何调用系统自带的相机进行拍照,主要有以下2种实现的方式:
1、Camera应用程序包含了一个意图过滤器,即intent filter,它使得开发人员能够提供与Camera应用程序同等的图像捕获能力,因此我们可以在应用程序的AndroidManifest.xml清单文件中新建一个意图过滤器,这样就会告诉Android系统,这个包含此意图过滤器的活动将根据指令执行指定的任务,创建意图过滤器代码如下:

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

接着我们就通过意图利用Camera应用程序,构造一个由上述过滤器捕获的意图,代码如下:

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

2、第2种方式,不需要像第一种那么麻烦,可以指定MediaStore类中的常量ACTION_IMAGE_CAPTURE来实现调用系统自带的相机,代码如下:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

3、上述两种方式,如果我们不需要返回图像显示在屏幕上的话,实例化Intent对象后,直接使用下面一行代码开启相机即可:

startActivity(intent);

二、接下来将来讲一个小实例,通过调用自带的系统相机进行拍照,拍照完后确认将图像显示出来,但是下面的例子由于是通过Intent意图触发的,所以相机应用程序不会将全尺寸的图像返回给主活动,所以只返回一幅小的缩略图。
1、首先,新建一个Android项目,项目名为Intent_camera,首先,对界面进行布局,打开默认的activity_main.xml文件,有个Button控件和ImageView控件,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button 
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="拍照"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

2、然后,打开默认的MainActivity类文件,主要通过Intent调用系统自带相机,通过startActivityForResult()方法开启相机,然后通过onActivityResult()接收传回的图像,代码如下:

package com.example.intent_camera;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    final static int CAMERA_RESULT = 0;//声明一个常量,代表结果码
    private Button button;//声明一个Button对象
    private ImageView imageView;//声明一个ImageView对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//加载布局文件
        button = (Button) findViewById(R.id.button);//获取到布局管理器的Button控件
        imageView = (ImageView) findViewById(R.id.imageView);//获取到布局管理器的ImageView控件
        //添加按钮点击事件监听器
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//实例化Intent对象,使用MediaStore的ACTION_IMAGE_CAPTURE常量调用系统相机
                startActivityForResult(intent, CAMERA_RESULT);//开启相机,传入上面的Intent对象
            }
        });

    }

    /**
     * 用onActivityResult()接收传回的图像,当用户拍完照片,或者取消后,系统都会调用这个函数
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, intent);
        if(resultCode==RESULT_OK){
            Bundle extras=intent.getExtras();//从Intent中获取附加值
            Bitmap bitmap=(Bitmap) extras.get("data");//从附加值中获取返回的图像
            imageView.setImageBitmap(bitmap);//显示图像
        }
    }
}

3、部署此应用到真机上,点击拍照按钮后,开启系统自带的相机,拍完照后按确认或对勾按钮,即可将图像显示在拍照的那个活动中,但是是缩略图。

三、以上内容,仅供大家学习参考,谢谢!