当前位置:   article > 正文

Android学习之调用系统相机实现拍照功能_intent intent = new intent(mediastore.action_image

intent intent = new intent(mediastore.action_image_capture);

Android学习之调用系统相机实现拍照功能

标签: android调用系统相机拍照












































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

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

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

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

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
   
   
  • 1
  • 1
  • 1

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

startActivity(intent);
   
   
  • 1
  • 1
  • 1

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

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <Button
  7. android:id="@+id/button"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:text="拍照"/>
  11. <ImageView
  12. android:id="@+id/imageView"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"/>
  15. </LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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

  1. package com.example.intent_camera;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.graphics.Bitmap;
  5. import android.os.Bundle;
  6. import android.provider.MediaStore;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.ImageView;
  11. public class MainActivity extends Activity {
  12. final static int CAMERA_RESULT = 0;//声明一个常量,代表结果码
  13. private Button button;//声明一个Button对象
  14. private ImageView imageView;//声明一个ImageView对象
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);//加载布局文件
  19. button = (Button) findViewById(R.id.button);//获取到布局管理器的Button控件
  20. imageView = (ImageView) findViewById(R.id.imageView);//获取到布局管理器的ImageView控件
  21. //添加按钮点击事件监听器
  22. button.setOnClickListener(new OnClickListener() {
  23. @Override
  24. public void onClick(View arg0) {
  25. // TODO Auto-generated method stub
  26. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//实例化Intent对象,使用MediaStore的ACTION_IMAGE_CAPTURE常量调用系统相机
  27. startActivityForResult(intent, CAMERA_RESULT);//开启相机,传入上面的Intent对象
  28. }
  29. });
  30. }
  31. /**
  32. * 用onActivityResult()接收传回的图像,当用户拍完照片,或者取消后,系统都会调用这个函数
  33. */
  34. @Override
  35. protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  36. // TODO Auto-generated method stub
  37. super.onActivityResult(requestCode, resultCode, intent);
  38. if(resultCode==RESULT_OK){
  39. Bundle extras=intent.getExtras();//从Intent中获取附加值
  40. Bitmap bitmap=(Bitmap) extras.get("data");//从附加值中获取返回的图像
  41. imageView.setImageBitmap(bitmap);//显示图像
  42. }
  43. }
  44. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/175320
推荐阅读
相关标签
  

闽ICP备14008679号