当前位置:   article > 正文

Android调用系统相机_window.base.launchcamera()

window.base.launchcamera()

简述:

  Android开发中会经常遇到调用系统相机功能的场景,今天总结一下Camera的基本使用。

                                                   

技术点:

    1、首先要考虑权限问题,Android6.0以上的系统需要动态申请权限,实现方式如下:

  1. /**
  2. * 方法描述:针对Android 6.0以上系统的动态权限申请
  3. */
  4. private void requestCameraPermission() {
  5. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  6. requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CODE);
  7. return;
  8. }
  9. launchCamera();
  10. }

    2、其次通过重写onRequestPermissionsResult()方法处理权限申请结果。

       

  1. @Override
  2. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  3. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  4. if (MY_PERMISSIONS_REQUEST_CODE == requestCode) {
  5. //循环遍历已经申请到的权限
  6. for (int index = 0; index < permissions.length; index++) {
  7. switch (permissions[index]) {
  8. case Manifest.permission.CAMERA:
  9. //用户授权
  10.  if (PackageManager.PERMISSION_GRANTED == grantResults[index]) {
  11. launchCamera();
  12. } else if (PackageManager.PERMISSION_DENIED == grantResults[index]) {
  13. Toast.makeText(CameraActivity.this, "应用没有拍照权限,请授权!", Toast.LENGTH_SHORT).show();
  14. } else {
  15. Toast.makeText(CameraActivity.this, "应用没有拍照权限,请授权", Toast.LENGTH_SHORT).show();
  16. }
  17. break;
  18. }
  19. }
  20. }
  21. }

3、其次是系统相机的调用及其拍照结果的回调。

  1. /**
  2. * 方法描述:启动系统相机
  3. */
  4. private void launchCamera() {
  5. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  6. startActivityForResult(intent, REQUEST_CODE);
  7. }
    至于结果回调还是重写onActivityResult()方法。

  1. @Override
  2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  3. super.onActivityResult(requestCode, resultCode, data);
  4. if (resultCode == Activity.RESULT_OK) {
  5. Intent intent = new Intent(CameraActivity.this, PhotoShowActivity.class);
  6. intent.putExtra("picture", data.getExtras());
  7. startActivity(intent);
  8. } else {
  9. launchCamera();
  10. }
  11. }

代码实现:

 CameraActivity代码:

  1. package com.lizhenya.systemcamera;
  2. import android.Manifest;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.content.pm.PackageManager;
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.provider.MediaStore;
  9. import android.view.Window;
  10. import android.widget.Toast;
  11. public class CameraActivity extends Activity {
  12. private static final int REQUEST_CODE = 1;
  13. private static final int MY_PERMISSIONS_REQUEST_CODE = 2;
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. requestWindowFeature(Window.FEATURE_NO_TITLE);
  18. setContentView(R.layout.activity_main);
  19. requestCameraPermission();
  20. }
  21. /**
  22. * 方法描述:启动系统相机
  23. */
  24. private void launchCamera() {
  25. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  26. startActivityForResult(intent, REQUEST_CODE);
  27. }
  28. /**
  29. * 方法描述:针对Android 6.0以上系统的动态权限申请
  30. */
  31. private void requestCameraPermission() {
  32. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  33. requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CODE);
  34. return;
  35. }
  36. launchCamera();
  37. }
  38. @Override
  39. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  40. super.onActivityResult(requestCode, resultCode, data);
  41. if (resultCode == Activity.RESULT_OK) {
  42. Intent intent = new Intent(CameraActivity.this, PhotoShowActivity.class);
  43. intent.putExtra("picture", data.getExtras());
  44. startActivity(intent);
  45. } else {
  46. launchCamera();
  47. }
  48. }
  49. @Override
  50. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  51. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  52. if (MY_PERMISSIONS_REQUEST_CODE == requestCode) {
  53. //循环遍历已经申请到的权限
  54. for (int index = 0; index < permissions.length; index++) {
  55. switch (permissions[index]) {
  56. case Manifest.permission.CAMERA:
  57. if (PackageManager.PERMISSION_GRANTED == grantResults[index]) {
  58. launchCamera();
  59. } else if (PackageManager.PERMISSION_DENIED == grantResults[index]) {
  60. Toast.makeText(CameraActivity.this, "应用没有拍照权限,请授权!", Toast.LENGTH_SHORT).show();
  61. } else {
  62. Toast.makeText(CameraActivity.this, "应用没有拍照权限,请授权", Toast.LENGTH_SHORT).show();
  63. }
  64. break;
  65. }
  66. }
  67. }
  68. }
  69. }

PhotoShowActivity代码:

  1. /**
  2. * 类描述:
  3. * Created by lizhenya on 16/8/23.
  4. */
  5. public class PhotoShowActivity extends Activity {
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_second);
  10. final Intent intent = getIntent();
  11. final Bundle bundle = intent.getBundleExtra("picture");
  12. Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
  13. ((ImageView)findViewById(R.id.iv)).setImageBitmap(bitmap);
  14. }
  15. }

完整源码下载

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

闽ICP备14008679号