android 圆形相机预览拍照_Android Camera2 Api 实现预览和拍照

本文介绍了如何使用Android Camera2 API创建一个圆形相机预览,并实现拍照功能。首先,需要新建工程并在Activity中设置TextureView作为预览容器,ImageView显示拍照结果,以及Button触发拍照。接着,配置AndroidManifest.xml以获取相机权限。在预览功能实现中,当TextureView准备好后开启相机。拍照功能则通过ImageReader监听并接收相机图片。此外,还详细讨论了相机的生命周期管理,包括活动暂停时释放相机,恢复时重新开启。
摘要由CSDN通过智能技术生成

camera2的结构如下,主要是通过相机管理器(CameraManager)获得相机设备(CameraDevice),然后再开启一个控制相机的会话,最后发送 拍照、预览、录像等请求。

a5dc2902f9dbd783f69f55f6b9ad29ac.png

0 准备

1 新建工程,在一个Activity上

设置一个TextureView 用于 预览图片;

设置一个ImageView 用于 显示拍照结果;

设置一个 Button 用于触发拍照功能;

2 设置AndroidManifest.xml文件配置相机权限

<uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.camera2.full" />

I 实现预览功能

1.先创建好一个预览的容器textureView

textureView=findViewById(R.id.texture_view_camera2);
  1. 监听这个容器的状态,如果容器准备好了,就开启相机:
// 先准备一个监听器
        surfaceTextureListener=new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                texture_surface=new Surface(textureView.getSurfaceTexture());
                openCamera();
            }
            /.../
        };
        //绑定监听器
        textureView.setSurfaceTextureListener(surfaceTextureListener);
  1. 启动相机时,要创建一系列的回调函数,所以应该从后向前写代码:
private void openCamera() {
      // 1 创建相机管理器,调用系统相机
        cameraManager= (CameraManager) getSystemService(Context.CAMERA_SERVICE); 
      // 2 准备 相机状态回调对象为后面用
        cam_stateCallback=new CameraDevice.StateCallback() {
            @Override
            public void onOpened(@NonNull CameraDevice camera) {
                // 2.1 保存已开启的相机对象
                opened_camera=camera;
                try {
                    // 2.2 构建请求对象(设置预览参数,和输出对象) 
                    requestBuilder = opened_camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); // 设置参数:预览
                    requestBuilder.addTarget(texture_surface); // 设置参数:目标容器
                    request = requestBuilder.build();
                    //2.3 创建会话的回调函数,后面用
                    cam_capture_session_stateCallback=new CameraCaptureSession.StateCallback() {
                        @Override  //2.3.1  会话准备好了,在里面创建 预览或拍照请求
                        public void onConfigured(@NonNull CameraCaptureSession session) {
                            cameraCaptureSession=session;
                            try {
                                // 2.3.2 预览请求
                                session.setRepeatingRequest(request,null,null);
                            } catch (CameraAccessException e) {
                                e.printStackTrace();
                            }
                        }
                    };
                    // 2.3 创建会话
                    opened_camera.createCaptureSession( Arrays.asList(texture_surface), cam_capture_session_stateCallback,null);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
            /..../
        };
      // 4 检查相机权限 
        checkPermission();
      // 5 开启相机(传入:要开启的相机ID,和状态回调对象)
        try {
            cameraManager.openCamera(cameraManager.getCameraIdList()[0],cam_stateCallback,null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

4 申请相机权限

private void checkPermission() {
        // 检查是否申请了权限
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
            if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA)){

            }else{
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},1);
            }
        }
    }

II 实现拍照功能

这段代码都是在 Activity的onCreate里面写的。

  1. 准备ImageReader用于监听并接收相机拍摄的图片
//B1. 准备工作:初始化ImageReader
       imageReader = ImageReader.newInstance(1000  ,1000, ImageFormat.JPEG,2);
  1. 设置ImageReader的监听函数
//B2. 准备工作:设置ImageReader收到图片后的回调函数
        imageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader reader) {
                //B2.1 接收图片:从ImageReader中读取最近的一张,转成Bitmap
                Image image= reader.acquireLatestImage();
                ByteBuffer buffer= image.getPlanes()[0].getBuffer();
                int length= buffer.remaining();
                byte[] bytes= new byte[length];
                buffer.get(bytes);
                image.close();
                bitmap = BitmapFactory.decodeByteArray(bytes,0,length);
                //B2.2 显示图片
                takephoto_imageView.setImageBitmap(bitmap);
            }
        },null);
  1. 保存ImageReader的Surface
//B3 配置:获取ImageReader的Surface
        imageReaderSurface = imageReader.getSurface();
  1. 设置相机的点击事件
//B4. 相机点击事件
        takephoto_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //B4.1 配置request的参数 拍照模式(这行代码要调用已启动的相机 opened_camera,所以不能放在外面
                try {
                    requestBuilder_image_reader = opened_camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
                requestBuilder_image_reader.set(CaptureRequest.JPEG_ORIENTATION,90);
                requestBuilder_image_reader.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
                //B4.2 配置request的参数 的目标对象
                requestBuilder_image_reader.addTarget(imageReaderSurface );
                try {
                    //B4.3 触发拍照
                    cameraCaptureSession.capture(requestBuilder_image_reader.build(),null,null);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        });
  1. 最后要记得在之前配置的session会话里面绑定,当前这个输出对象imageReaderSurface:
private void openCamera() {
        /.../
        cam_stateCallback=new CameraDevice.StateCallback() {
            @Override
            public void onOpened(@NonNull CameraDevice camera) {
                    /.../
                    opened_camera.createCaptureSession( Arrays.asList(texture_surface,imageReaderSurface), cam_capture_session_stateCallback,null);

    }

这段功能的整体代码为:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        /....../
        //B1. 准备工作:初始化ImageReader
        imageReader = ImageReader.newInstance(1000  ,1000, ImageFormat.JPEG,2);
        //B2. 准备工作:设置ImageReader收到图片后的回调函数
        imageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader reader) {
                //B2.1 接收图片:从ImageReader中读取最近的一张,转成Bitmap
                Image image= reader.acquireLatestImage();
                ByteBuffer buffer= image.getPlanes()[0].getBuffer();
                int length= buffer.remaining();
                byte[] bytes= new byte[length];
                buffer.get(bytes);
                image.close();
                bitmap = BitmapFactory.decodeByteArray(bytes,0,length);
                //B2.2 显示图片
                takephoto_imageView.setImageBitmap(bitmap);
            }
        },null);
        //B3 配置:获取ImageReader的Surface
        imageReaderSurface = imageReader.getSurface();

        //B4. 相机点击事件
        takephoto_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //B4.1 配置request的参数 拍照模式(这行代码要调用已启动的相机 opened_camera,所以不能放在外面
                try {
                    requestBuilder_image_reader = opened_camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
                requestBuilder_image_reader.set(CaptureRequest.JPEG_ORIENTATION,90);
                requestBuilder_image_reader.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
                //B4.2 配置request的参数 的目标对象
                requestBuilder_image_reader.addTarget(imageReaderSurface );
                try {
                    //B4.3 触发拍照
                    cameraCaptureSession.capture(requestBuilder_image_reader.build(),null,null);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        });
    }

III 相机的声明周期管理

1. 当activity不处于交互状态时释放相机

@Override
    protected void onPause() {
        // 先把相机的session关掉
        if(cameraCaptureSession!=null){
            cameraCaptureSession.close();
        }
        // 再关闭相机
        if(null!=opened_camera){
            opened_camera.close();
        }
        // 最后关闭ImageReader
        if(null!=imageReader){
            imageReader.close();
        }
        // 最后交给父View去处理
        super.onPause();
    }

2 当活动再次可以交互时开启相机

@Override
    protected void onResume() {
        super.onResume();
        // 如果 textureView可用,就直接打开相机
        if(textureView.isAvailable()){
            openCamera();
        }else{
        // 否则,就开启它的可用时监听。
            textureView.setSurfaceTextureListener(surfaceTextureListener);
        }
    }

IV 预览和功能的完整代码为:

文件名 Camera2ApiActivity.java

package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.camera2.impl.Camera2CaptureRequestBuilder;
import androidx.camera.core.CameraCaptureResult;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.biometrics.BiometricManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureFailure;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CaptureResult;
import android.hardware.camera2.TotalCaptureResult;
import android.media.Image;
import android.media.ImageReader;
import android.os.Bundle;
import android.util.Size;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.ByteArrayInputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;

public class Camera2apiActivity extends AppCompatActivity {
    TextureView textureView;
    TextureView.SurfaceTextureListener surfaceTextureListener;
    CameraManager cameraManager;
    CameraDevice.StateCallback cam_stateCallback;
    CameraDevice opened_camera;
    Surface texture_surface;
    CameraCaptureSession.StateCallback cam_capture_session_stateCallback;
    CameraCaptureSession.CaptureCallback still_capture_callback;
    CameraCaptureSession cameraCaptureSession;
    CaptureRequest.Builder requestBuilder;
    CaptureRequest.Builder requestBuilder_image_reader;
    ImageReader imageReader;
    Surface imageReaderSurface;
    Bitmap bitmap;
    CaptureRequest request;
    CaptureRequest takephoto_request;
    Button takephoto_btn;
    ImageView takephoto_imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera2api);
        textureView=findViewById(R.id.texture_view_camera2);
        takephoto_btn=findViewById(R.id.btn_camera2_takephoto);
        takephoto_imageView= findViewById(R.id.image_view_preview_image);
        surfaceTextureListener=new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                texture_surface=new Surface(textureView.getSurfaceTexture());
                openCamera();
            }
            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
            }
            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                return false;
            }
            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {
            }
        };
        textureView.setSurfaceTextureListener(surfaceTextureListener);
        //B1. 准备工作:初始化ImageReader
        imageReader = ImageReader.newInstance(1000  ,1000, ImageFormat.JPEG,2);
        //B2. 准备工作:设置ImageReader收到图片后的回调函数
        imageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader reader) {
                //B2.1 接收图片:从ImageReader中读取最近的一张,转成Bitmap
                Image image= reader.acquireLatestImage();
                ByteBuffer buffer= image.getPlanes()[0].getBuffer();
                int length= buffer.remaining();
                byte[] bytes= new byte[length];
                buffer.get(bytes);
                image.close();
                bitmap = BitmapFactory.decodeByteArray(bytes,0,length);
                //B2.2 显示图片
                takephoto_imageView.setImageBitmap(bitmap);
            }
        },null);
        //B3 配置:获取ImageReader的Surface
        imageReaderSurface = imageReader.getSurface();

        //B4. 相机点击事件
        takephoto_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //B4.1 配置request的参数 拍照模式(这行代码要调用已启动的相机 opened_camera,所以不能放在外面
                try {
                    requestBuilder_image_reader = opened_camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
                requestBuilder_image_reader.set(CaptureRequest.JPEG_ORIENTATION,90);
                requestBuilder_image_reader.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
                //B4.2 配置request的参数 的目标对象
                requestBuilder_image_reader.addTarget(imageReaderSurface );
                try {
                    //B4.3 触发拍照
                    cameraCaptureSession.capture(requestBuilder_image_reader.build(),null,null);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void openCamera() {
        cameraManager= (CameraManager) getSystemService(Context.CAMERA_SERVICE);  // 初始化
        cam_stateCallback=new CameraDevice.StateCallback() {
            @Override
            public void onOpened(@NonNull CameraDevice camera) {
                opened_camera=camera;
                try {
                    requestBuilder = opened_camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
                    requestBuilder.addTarget(texture_surface);
                    request = requestBuilder.build();
                    cam_capture_session_stateCallback=new CameraCaptureSession.StateCallback() {
                        @Override
                        public void onConfigured(@NonNull CameraCaptureSession session) {
                            cameraCaptureSession=session;
                            try {
                                session.setRepeatingRequest(request,null,null);
                            } catch (CameraAccessException e) {
                                e.printStackTrace();
                            }
                        }
                        @Override
                        public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                        }
                    };
                    opened_camera.createCaptureSession( Arrays.asList(texture_surface,imageReaderSurface), cam_capture_session_stateCallback,null);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onDisconnected(@NonNull CameraDevice camera) {
            }
            @Override
            public void onError(@NonNull CameraDevice camera, int error) {
            }
        };
        checkPermission();
        try {
            cameraManager.openCamera(cameraManager.getCameraIdList()[0],cam_stateCallback,null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    private void checkPermission() {
        // 检查是否申请了权限
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
            if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA)){

            }else{
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},1);
            }
        }
    }
        @Override
    protected void onResume() {
        super.onResume();
        // 如果 textureView可用,就直接打开相机
        if(textureView.isAvailable()){
            openCamera();
        }else{
        // 否则,就开启它的可用时监听。
            textureView.setSurfaceTextureListener(surfaceTextureListener);
        }
    }
        @Override
    protected void onPause() {
        // 先把相机的session关掉
        if(cameraCaptureSession!=null){
            cameraCaptureSession.close();
        }
        // 再关闭相机
        if(null!=opened_camera){
            opened_camera.close();
        }
        // 最后关闭ImageReader
        if(null!=imageReader){
            imageReader.close();
        }
        // 最后交给父View去处理
        super.onPause();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值