从无到有做一个身份证识别app——调用系统摄像头或者创建摄像头

使用摄像头

调用摄像头有两种方式,第一种是调用系统的摄像头,只不过安卓7.0之后调用系统摄像头麻烦一些,这里下面再说;第二种方式就是之定义一个摄像头,满足自己拍照的要求。

一、调用系统摄像头

这里就直接贴代码了

//在写下面这段代码之前,默认权限已经赋予
import android.Manifest;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //权限相关变量
    private static final String[] FILE_PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
    private static final int FILE_PERMISSIONS_CODE = 1;
    //控件变量
    private ImageView imageView;
    private Button openCamera;
    private Button button1;
    private Button button2;
    private EditText edittext;
    //文件处理变量
    private static File file;
    private Uri imageUri;
    private String filepath;
    //固定值(请求码/返回码)
    public static final int CROP_PHOTO = 2222;
    private static final int TAKE_PHOTO=1111;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取权限
        requestPermission();
        //实例控件
        imageView = findViewById(R.id.imageview);
        edittext=findViewById(R.id.edittext);
        openCamera = findViewById(R.id.open_camera);
        button1=findViewById(R.id.button1);

        //监听控件
        openCamera.setOnClickListener(this);
        openAlbum.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.open_camera:
                openCamera(this);
                //打开系统相册方法
                //openGallery();
                break;
            case R.id.open_album:
                openAlbum();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case TAKE_PHOTO:
                if (resultCode == RESULT_OK) {
                    Intent intent = new Intent("com.android.camera.action.CROP");
                    intent.setDataAndType(imageUri, "image/*");
                    intent.putExtra("scale", true);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    startActivityForResult(intent, CROP_PHOTO); // 启动裁剪程序
                }
                break;
            case CROP_PHOTO:
                if (resultCode == RESULT_OK) {
                    try {
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
                                .openInputStream(imageUri));
                        imageView.setImageBitmap(bitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
           case CHOOSE_PHOTO:
                if (resultCode == RESULT_OK) {
                    try {
                        if(data != null) {
                            Uri uri = data.getData();
                            imageUri = uri;
                        }
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
                                .openInputStream(imageUri));
                        imageView.setImageBitmap(bitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;

        }
    }

    //拍照过程
    private void openCamera(Activity activity){
        //获取系统的版本
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        //通过隐式意图,调用照相的界面
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // 判存储卡是否可以用,可用进行存储断
        if( Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)){
            //格式化时间,用法可以参考下列网址:https://www.runoob.com/java/date-time-am-pm.html
            SimpleDateFormat timeStampFormat = new SimpleDateFormat(
                    "yyyy_MM_dd_HH_mm_ss");
            String filename = timeStampFormat.format(new Date());
            //生成文件,名字是系统时间
            file = new File(Environment.getExternalStorageDirectory(),
                    filename + ".jpg");
            //Android7.0之后,也就是SDK版本24之后,不允许将文件的uri直接暴露出来,所以先判断是否符合
            if(currentapiVersion<24){
                //获取文件的uri
                imageUri = Uri.fromFile(file);
                //将最后拍照生成的输出文件,指定到刚刚获取的uri,
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            } else{
                //插入数据,传输数据
                ContentValues contentValues=new ContentValues(1);
                //从文件的绝对路径获得文件对象
                contentValues.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
                imageUri = activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            }
            startActivityForResult(intent,TAKE_PHOTO);
        }
    private void openAlbum() {
        Intent intent = new Intent("android.intent.action.GET_CONTENT");
        intent.setType("image/*");
        startActivityForResult(intent, CHOOSE_PHOTO);
    }
    }

二、自定义摄像头

关于如何调用摄像头,因为我之前写过一个类似的,我就直接拿上来用了,链接如下:https://blog.csdn.net/weixin_43308622/article/details/90234569

你可能感兴趣的:(从无到有做一个身份证识别app——调用系统摄像头或者创建摄像头)