摄像头的应用(数值传递)

package com.androidstudy.uicomponenttest;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

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

public class CameraActivity extends AppCompatActivity {
    private Button btnCamera;
    private ImageView ivImage;
    private final int CAMERA_REQUEST = 10; //双方认可,这个是标志
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        initView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case CAMERA_REQUEST:
                if(resultCode == RESULT_OK){
                    Bitmap bitmap = (Bitmap) data.getExtras().get("data");
                    ivImage.setImageBitmap(bitmap);
                }
                break;
        }
    }

    public void initView(){
        ivImage = findViewById(R.id.iv_capture_photo);
        btnCamera = findViewById(R.id.btn_camera_invoke);
        btnCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //启动本机的摄像头
                //如何调用本机的摄像头 ACTION是关键
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                //启动系统拍照程序,并将拍摄的照片返回显示在ImageView组件中
                startActivityForResult(intent,CAMERA_REQUEST);
            }
        });
    }
}

 

posted @ 2021-01-10 20:57  Master_Sun  阅读(80)  评论(0编辑  收藏  举报