android Camera camera class

Several process methods related to Camera class

Camera.open(cameraId)

camera.setDisplayOrientation(0) set camera horizontal direction

Mgamera.setpreviewdisplay (holder) displays the picture on the SurfaceHolder

mCamera.setPreviewCallback(callback) sets the callback to pass Yuv data to the onPreviewFrame method

Previewrate mparames = mmamera. Getparameters() get camera related parameters

mCamera.stopPreview() stop Preview

mCamera.release() release camera

Implement Camera.PreviewCallback interface to obtain Yuv video stream

@Override
public void onPreviewFrame(byte[] bytes, Camera camera) {
       //Get real-time Yuv data stream
}

Implement Camera.PictureCallback interface to take photos

@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();
        //Reopen Preview    
        camera.stopPreview();
        camera.startPreview();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

Looking for camera

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 Preposition
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                return camIdx;
            }
        } else {
            // CAMERA_FACING_BACK Postposition
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                return camIdx;
            }
        }

    }
    return -1;
}

Camera.Parameters camera parameter class

Camera.Parameters parameters = camera.getParameters(); / / get camera parameters

List < size > supportedpreviewsizes = parameters. Getsupportedpreviewsizes(); / / obtain various resolutions supported by the camera

parameters.setPictureFormat(PixelFormat.JPEG); / / format photos

Parameters. Setpreviewsize (withh, height); / / set preview size

parameters.setPictureSize(witdh,height);

camera.setParameters(parameters); / / submit camera parameters

Set camera resolution

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

Related reading

SurfaceView turns on camera preview

Tags: Android SurfaceView

Posted on Thu, 02 Jan 2020 04:31:30 -0500 by ayzee01