欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用Camera类调用摄像头获取每一帧数据

程序员文章站 2022-05-08 22:34:39
...

在这里先记录一下,有时间再整理

package com.yfmao.fras;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.yfmao.mylibrary.faceid.FaceID;
import com.yfmao.mylibrary.jni.OneDetection;

import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

import static org.opencv.android.Utils.bitmapToMat;

public class camera_frame extends Activity implements SurfaceHolder.Callback,
        Camera.PreviewCallback {

    private SurfaceView mSurfaceview = null; // SurfaceView对象:(视图组件)视频显示
    private SurfaceHolder mSurfaceHolder = null; // SurfaceHolder对象:(抽象接口)SurfaceView支持类
    private Camera mCamera = null; // Camera对象,相机预览
    private Point mCameraSize;


    private int total_detect = 0;
    private float total_time = 0;
    private float ave_time = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_frame);
        mSurfaceview = (SurfaceView) findViewById(R.id.surfaceView);
        mSurfaceHolder = mSurfaceview.getHolder(); // 绑定SurfaceView,取得SurfaceHolder对象
        mSurfaceHolder.addCallback(this); // SurfaceHolder加入回调接口
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// 設置顯示器類型,setType必须设置
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (mCamera == null) {
            return;
        }
        mCamera.stopPreview();
        mCamera.setPreviewCallback(this);
        mCamera.startPreview();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mCamera = Camera.open();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }

    @Override
    protected void onStop(){
        super.onStop();
        mCamera.release();
        mCamera = null;
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        mCamera.release();
        mCamera = null;
    }

    //释放Camera
    public void releaseCamera() {
        if (mCamera != null) {
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();// 停掉摄像头的预览
            mCamera.release();
            mCamera = null;
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            if (mCamera != null) {
                mCamera.setPreviewDisplay(mSurfaceHolder);
                mCamera.startPreview();
                mCamera.autoFocus(null);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        releaseCamera();
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        Camera.Parameters parameters = camera.getParameters();
        Size size = parameters.getPreviewSize();
        YuvImage image = new YuvImage(data, parameters.getPreviewFormat(), size.width, size.height,null);

        ByteArrayOutputStream out_image = new ByteArrayOutputStream();
        image.compressToJpeg(new Rect(0, 0, size.width, size.height), 100, out_image);
        byte[] bytes = out_image.toByteArray();
        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);


        Mat rgba= new Mat();
        bitmapToMat(bitmap,rgba);

        Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGBA2BGR);

        Date a1 = new Date();
        OneDetection[] face_dets = FaceID.DetectFace(rgba.getNativeObjAddr());
        int maxidx = FaceID.GetMaxId();
        Date b1 = new Date();
        long interval1 = (b1.getTime() - a1.getTime());
        System.out.println("检测耗时:" + "    "+interval1+"ms");//会打印出相差3秒




        if(maxidx == 0) {
            String name[]=new String[1];
            float score[]=new float[1];
            int face_id[]= new int[1];


            Date a2 = new Date();
            int recog_result = FaceID.thenRecognize(face_dets[maxidx].getFaceBoxIdx(), face_id, name, score);
            Date b2 = new Date();
            long interval2 = (b2.getTime() - a2.getTime());
            System.out.println("识别时间  :"+interval2+"ms");//会打印出相差3秒

            long total = interval1 + interval2;
            System.out.println("检测+识别时间:"+total+"ms");//会打印出相差3秒

            total_detect += 1;
            total_time += total;
            ave_time = total_time / total_detect;
            System.out.println("平均检测+识别时间:  " + ave_time + " ms");
        }

    }
}

其中, onPreviewFrame是对每一帧图像的具体处理.

相关标签: Android Camera