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

Android开启相机预览获取Yuv视频流数据

程序员文章站 2022-06-22 10:10:17
自定义SurfaceView 主要步骤: 实现SurfaceHolder.Callback接口,创建SurfaceView的生命周期 实现Camera.PreviewCallback接口,创建预览回调接口 创建Camera打开相机 实例: FrameCallback回调接口 MainActivity ......

自定义surfaceview

主要步骤:

实现surfaceholder.callback接口,创建surfaceview的生命周期

实现camera.previewcallback接口,创建预览回调接口

创建camera打开相机

实例:

public class camerasurfaceviews extends surfaceview implements surfaceholder.callback, camera.previewcallback{

    context mcontext;
    surfaceholder msurfaceholder;//surface的控制器
    camera mcamera;//相机类
    framecallback mcb;//数据回调接口

    public void setmcb(framecallback mcb) {
        this.mcb = mcb;
    }
   //获取surfaceview的surfaceholder对象和接口 public camerasurfaceviews(context context, attributeset attrs) { super(context, attrs); mcontext = context; msurfaceholder = getholder(); msurfaceholder.addcallback(this); }
//寻找相机 private int findcamera(boolean isfront) { int cameracount = 0; camera.camerainfo camerainfo = new camera.camerainfo(); cameracount = camera.getnumberofcameras(); for (int camidx = 0; camidx < cameracount; camidx++) { camera.getcamerainfo(camidx, camerainfo); if (isfront) { // camera_facing_front前置 if (camerainfo.facing == camera.camerainfo.camera_facing_front) { return camidx; } } else { // camera_facing_back后置 if (camerainfo.facing == camera.camerainfo.camera_facing_back) { return camidx; } } } return -1; } //打开相机 private camera getcamera() { camera camera = null; int cameraid = findcamera(false); try { if(cameraid==1){ camera = camera.open(cameraid ); }else if(cameraid==0)( camera = camera.open(0); }
        } catch (exception e) { camera = null; } return camera; } //surface被创建时调用 @override public void surfacecreated(surfaceholder holder) { mcamera = getcamera();
     }
//surface大小被改变时调用 @override public void surfacechanged(surfaceholder holder, int format, int width,int height) {     setstartpreview(mcamera,msurfaceholder);
}
  //surface被销毁时调用
    @override
    public void surfacedestroyed(surfaceholder holder) {
        releasecamera();
    }

    //开启相机预览
    private void setstartpreview(camera camera, surfaceholder holder) {
        try {

            mcamera.setpreviewdisplay(holder);
            mcamera.setpreviewcallback(this);
            camera.startpreview();
        } catch (ioexception e) {
        }
    }
//释放camera public void releasecamera() { if (mcamera != null) { mcamera.setpreviewcallback(null); mcamera.stoppreview();// 停掉摄像头的预览 mcamera.release(); mcamera = null; } }
//预览回调,传递yuv视频流数据 @override public void onpreviewframe(byte[] bytes, camera camera) { // log.i("onpreviewframe",""+ bytes); if(mcb!= null){ mcb.ondecodeframe(bytes); } } }

 framecallback回调接口

public interface framecallback {
    void ondecodeframe(byte[] data) ;
}

mainactivity

public class mainactivity extends appcompatactivity implements framecallback{

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);
  }

    @override
    public void ondecodeframe(byte[] data) {
      //实时处理yuv视频流数据
    }

}

相关阅读

了解surfaceview

yuv数据格式相关转换方法