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

android Camera相机类

程序员文章站 2022-03-18 15:39:40
Camera相机类相关的几个流程方法 Camera.open(cameraId) 打开相机 camera.setDisplayOrientation(0) 设置相机水平方向 mCamera.setPreviewDisplay(holder) 在SurfaceHolder上显示画面 mCamera.s ......

camera相机类相关的几个流程方法

camera.open(cameraid) 打开相机

camera.setdisplayorientation(0) 设置相机水平方向

mcamera.setpreviewdisplay(holder) 在surfaceholder上显示画面

mcamera.setpreviewcallback(callback) 设置回调,将yuv数据传递到onpreviewframe方法

previewrate mparams = mcamera.getparameters() 获取相机相关参数

mcamera.stoppreview() 停止预览

mcamera.release() 释放相机

实现camera.previewcallback接口获取yuv视频流

@override
public void onpreviewframe(byte[] bytes, camera camera) {
       //获取实时的yuv数据流
}

实现camera.picturecallback接口实现拍照

@override
public void onpicturetaken(byte[] bytes, camera camera) {
    try {
        fileoutputstream fileoutputstream=new fileoutputstream("/mnt/sdcard/dcim/cameras/"+system.currenttimemillis()+".png");
        fos.write(bytes);
        fos.close();
        //重新打开预览    
        camera.stoppreview();
        camera.startpreview();
    } catch (filenotfoundexception e) {
        e.printstacktrace();
    }
}

寻找相机

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;
}

camera.parameters相机参数类

camera.parameters parameters = camera.getparameters(); //获取相机参数

list<size> supportedpreviewsizes = parameters.getsupportedpreviewsizes(); //获取摄像头支持的各种分辨率

parameters.setpictureformat(pixelformat.jpeg);//设置照片格式

parameters.setpreviewsize(witdh,height); //设置预览尺寸

parameters.setpicturesize(witdh,height);

camera.setparameters(parameters);//提交相机参数

设置相机分辨率

private void initcamera(camera mcamera){
    if(mcamera != null){
        try{
            mparams = mcamera.getparameters();
            size previewsize = camparautil.getinstance().getproppreviewsize(mparams.getsupportedpreviewsizes(),1.333f, 640);
            log.d("log", string.valueof(previewsize.width) + "-" + previewsize.height);
            pwidth = previewsize.width;
            pheight = previewsize.height;
            mparams.setpreviewsize(pwidth, pheight);

            list<string> focusmodes = mparams.getsupportedfocusmodes();
            if(focusmodes.contains("continuous-video")){
                mparams.setfocusmode(camera.parameters.focus_mode_continuous_video);
            }
            mcamera.setparameters(mparams);
            mcamera.startpreview();

            ispreviewing = true;
            mpreviwrate = previewrate;
        }catch (exception ex){
            ex.printstacktrace();
        }

    }
}

public size getproppreviewsize(list<size> list, float th, int minwidth){
    collections.sort(list, sizecomparator);

    int i = 0;
    for(size s:list){
        if((s.width >= minwidth) && equalrate(s, th)){
            break;
        }
        i++;
    }
    if(i == list.size()){
    }
    return list.get(i);
}

 相关阅读

surfaceview开启相机预览