Camera2官方樣例解讀

樣例地址:android/camera-samples/Camera2BasicJava/ - Github
大家可以把項目下載到本地並用AndroidStudio打開了再看

簡介

在Android5.0的時候,谷歌推出了Camera2API,較上一代Camera1,Camera2支持了很多Camera1所不支持的特性:

  1. 更先進的API架構
  2. 可以獲取更多的幀信息、以及手動控制每一幀的參數
  3. 對Camera的控制更加完全
  4. 支持更多的格式以及高速連拍
    ……

API大致的使用流程如下:CameraAPI

  1. 通過context.getSystemService(Context.CAMERA_SERVICE)獲取CameraManager.
  2. 調用CameraManager .open()方法在回調中得到CameraDevice.
  3. 通過CameraDevice.createCaptureSession()在回調中獲取CameraCaptureSession.
  4. 構建CaptureRequest, 有三種模式可選 預覽/拍照/錄像.
  5. 通過CameraCaptureSession發送CaptureRequest, capture表示只發一次請求, setRepeatingRequest表示不斷髮送請求.
  6. 拍照數據可以在ImageReader.OnImageAvailableListener回調中獲取, CaptureCallback中則可獲取拍照實際的參數和Camera當前狀態.

CameraActivity

這個類是主要的Activity類,也是唯一的一個Activity。他的代碼也很簡單:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);
    if (null == savedInstanceState) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, Camera2BasicFragment.newInstance())
                .commit();
    }
}

其實就是調用Camera2BasicFragment的newInstance()方法將Camera2BasicFragment加載進來。

接下來我們看下這個方法

Camera2BasicFragment

Camera2BasicFragment # newInstance()

public static Camera2BasicFragment newInstance() {
    return new Camera2BasicFragment();
}

這就是一個靜態方法,返回了一個Camera2BasicFragment的對象。

而Camera2BasicFragment是什麼,他是一個Fragment,所以我們就從一個Fragment的生命週期開始看起。

Camera2BasicFragment # onCreateView()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_camera2_basic, container, false);
}

綁定Layout

Camera2BasicFragment # onViewCreated()

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    view.findViewById(R.id.picture).setOnClickListener(this);
    view.findViewById(R.id.info).setOnClickListener(this);
    mTextureView = view.findViewById(R.id.texture);
}

設置點擊事件和綁定view

Camera2BasicFragment # onResume()

@Override
public void onResume() {
    super.onResume();
    startBackgroundThread();

    if (mTextureView.isAvailable()) {
        openCamera(mTextureView.getWidth(), mTextureView.getHeight());
    } else {
        mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
    }
}

那我們來看看startBackgroundThread()方法:

Camera2BasicFragment # startBackgroundThread()

private void startBackgroundThread() {
    mBackgroundThread = new HandlerThread("CameraBackground");
    mBackgroundThread.start();
    mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
}

這就是開啓了一個線程而已。

再來看看openCamera方法

Camera2BasicFragment # openCamera()

private void openCamera(int width, int height) {
    // 獲取權限
    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {
        requestCameraPermission();
        return;
    }
    setUpCameraOutputs(width, height);
    configureTransform(width, height);
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException("Time out waiting to lock camera opening.");
        }
        manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
    }
}

此處將mTextureView的寬和高傳進來了。其中mTextureView就是預覽的窗口View。

首先判斷是否獲得Camera權限,如果沒有那就去獲取。獲取權限這塊我就不細說,大家可以去了解下Android6.0權限。

接着進入了setUpCameraOutputs方法:

Camera2BasicFragment # setUpCameraOutputs()

private void setUpCameraOutputs(int width, int height) {
Activity activity = getActivity();
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
    for (String cameraId : manager.getCameraIdList()) {
        CameraCharacteristics characteristics
                = manager.getCameraCharacteristics(cameraId);
    
        // We don't use a front facing camera in this sample.
        // CameraCharacteristics.LENS_FACING 相機設備相對於屏幕的方向
        Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
        // 如果該攝像頭是前置攝像頭,就不進行處理
        if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
            continue;
        }
    
    
        // 得到該攝像頭設備支持的可用流配置;還包括每種格式/尺寸組合的最小幀時長和停頓時長。
        StreamConfigurationMap map = characteristics.get(
                CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        if (map == null) {
            continue;
        }
    
        // For still image captures, we use the largest available size.
        // 對於靜態圖像捕獲,我們使用最大的可用大小。
        Size largest = Collections.max(
                Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                new CompareSizesByArea());
        // ImageReader類允許應用程序直接訪問渲染到Surface中的圖像數據
        mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
                ImageFormat.JPEG, /*maxImages*/2);
        mImageReader.setOnImageAvailableListener(
                mOnImageAvailableListener, mBackgroundHandler);
    
        // Find out if we need to swap dimension to get the preview size relative to sensor
        // coordinate.
        // 找出是否需要交換尺寸以獲得相對於傳感器座標的預覽尺寸。
        int displayRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        //noinspection ConstantConditions
        mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
        boolean swappedDimensions = false;
        switch (displayRotation) {
            case Surface.ROTATION_0:
            case Surface.ROTATION_180:
                if (mSensorOrientation == 90 || mSensorOrientation == 270) {
                    swappedDimensions = true;
                }
                break;
            case Surface.ROTATION_90:
            case Surface.ROTATION_270:
                if (mSensorOrientation == 0 || mSensorOrientation == 180) {
                    swappedDimensions = true;
                }
                break;
            default:
                Log.e(TAG, "Display rotation is invalid: " + displayRotation);
        }
    
        Point displaySize = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
        int rotatedPreviewWidth = width;
        int rotatedPreviewHeight = height;
        int maxPreviewWidth = displaySize.x;
        int maxPreviewHeight = displaySize.y;
    
        if (swappedDimensions) {
            rotatedPreviewWidth = height;
            rotatedPreviewHeight = width;
            maxPreviewWidth = displaySize.y;
            maxPreviewHeight = displaySize.x;
        }
    
        if (maxPreviewWidth > MAX_PREVIEW_WIDTH) {
            maxPreviewWidth = MAX_PREVIEW_WIDTH;
        }
    
        if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) {
            maxPreviewHeight = MAX_PREVIEW_HEIGHT;
        }
    
        // Danger, W.R.! Attempting to use too large a preview size could exceed the camera
        // bus' bandwidth limitation, resulting in gorgeous previews but the storage of
        // garbage capture data.
    
        // 危險!嘗試使用太大的預覽大小可能會超出相機總線的帶寬限制,導致華麗的預覽,但會存儲垃圾捕獲數據。
        mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
                rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
                maxPreviewHeight, largest);
    
        // We fit the aspect ratio of TextureView to the size of preview we picked.
        // 我們將TextureView的寬高比與我們選擇的預覽大小相匹配。
        int orientation = getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            mTextureView.setAspectRatio(
                    mPreviewSize.getWidth(), mPreviewSize.getHeight());
        } else {
            mTextureView.setAspectRatio(
                    mPreviewSize.getHeight(), mPreviewSize.getWidth());
        }
    
        // Check if the flash is supported.
        Boolean available = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
        mFlashSupported = available == null ? false : available;
    
        mCameraId = cameraId;
        return;
    }
} catch (CameraAccessException e) {
    e.printStackTrace();
} catch (NullPointerException e) {
    // Currently an NPE is thrown when the Camera2API is used but not supported on the
    // device this code runs.
    ErrorDialog.newInstance(getString(R.string.camera_error))
            .show(getChildFragmentManager(), FRAGMENT_DIALOG);
    }
}

首先他得到了CameraManager對象。然後就能得到所有攝像頭的集合,接着遍歷這個集合,先判斷是否爲前攝像頭,如果是的話就跳出,循環下一個攝像頭;然後再得到該攝像頭支持的流配置,如果沒有,就跳出循環下一個;接着在得到該攝像頭在JPEG格式下所支持的最大的大小配置,同時配置好ImageReader。

接下來根據攝像頭的方向和屏幕方向設置預覽的方向以及尺寸。核心方法是chooseOptimalSize()。我們來看下次方法:

Camera2BasicFragment # chooseOptimalSize()
private static Size chooseOptimalSize(Size[] choices, int textureViewWidth,
                                      int textureViewHeight, int maxWidth, int maxHeight, Size aspectRatio) {

    // Collect the supported resolutions that are at least as big as the preview Surface
    List<Size> bigEnough = new ArrayList<>();
    // Collect the supported resolutions that are smaller than the preview Surface
    List<Size> notBigEnough = new ArrayList<>();
    int w = aspectRatio.getWidth();
    int h = aspectRatio.getHeight();
    for (Size option : choices) {
        if (option.getWidth() <= maxWidth && option.getHeight() <= maxHeight &&
                option.getHeight() == option.getWidth() * h / w) {
            if (option.getWidth() >= textureViewWidth &&
                    option.getHeight() >= textureViewHeight) {
                bigEnough.add(option);
            } else {
                notBigEnough.add(option);
            }
        }
    }

    // Pick the smallest of those big enough. If there is no one big enough, pick the
    // largest of those not big enough.
    if (bigEnough.size() > 0) {
        return Collections.min(bigEnough, new CompareSizesByArea());
    } else if (notBigEnough.size() > 0) {
        return Collections.max(notBigEnough, new CompareSizesByArea());
    } else {
        Log.e(TAG, "Couldn't find any suitable preview size");
        return choices[0];
    }
}

首先說一下傳進來的參數:

  • choices:根據預覽SurfaceView的得到的攝像頭所支持的流配置
  • textureViewWidth:TextureView的寬
  • textureViewHeight:TextureView的高
  • maxWidth:可顯示區域的寬
  • maxHeight:可顯示區域的寬
  • aspectRatio:可使用的最大尺寸

先創建兩個集合:bigEnough和notBigEnough。然後再遍歷choices,也就是遍歷所有SurfaceView所支持的流配置的Size,然後判斷寬和高是不是小於maxWidth和maxHeight,以及比例是否相同。然後判端如果尺寸大於TextureView尺寸就放入bigEnough,小於就放入notBigEnough。最後從bigEnough中找到最小的尺寸,如果bigEnough沒有就從notBigEnough中找到最大的尺寸。然後返回。

接下來我們回到setUpCameraOutputs()方法
接下來判端了屏幕的方向,是橫屏還是豎屏,接着根據即如果配置預覽的寬高。最後配置好mFlashSupported和mCameraId變量。方法結束。

Camera2BasicFragment # configureTransform()

然後我們返回openCamera()方法,下面又是configureTransform()方法:

private void configureTransform(int viewWidth, int viewHeight) {
    Activity activity = getActivity();
    if (null == mTextureView || null == mPreviewSize || null == activity) {
        return;
    }
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    Matrix matrix = new Matrix();
    RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
    RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
    float centerX = viewRect.centerX();
    float centerY = viewRect.centerY();
    if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
        bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
        matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
        float scale = Math.max(
                (float) viewHeight / mPreviewSize.getHeight(),
                (float) viewWidth / mPreviewSize.getWidth());
        matrix.postScale(scale, scale, centerX, centerY);
        matrix.postRotate(90 * (rotation - 2), centerX, centerY);
    } else if (Surface.ROTATION_180 == rotation) {
        matrix.postRotate(180, centerX, centerY);
    }
    mTextureView.setTransform(matrix);
}

配置預覽圖的大小、方向/角度。在此就不多說了。

然後再返回openCamera()方法
下面就是得到CameraManager實例,並通過manager打開Camera,這樣相機打開流程就結束了。

Camera2BasicFragment # onPause()

@Override
public void onPause() {
    closeCamera();
    stopBackgroundThread();
    super.onPause();
}
```
具體也不細說了,就是把Camera關閉,並關掉後臺線程。

## TextureView.SurfaceTextureListener
我們來看下TextureView的配置。
```java
private final TextureView.SurfaceTextureListener mSurfaceTextureListener
        = new TextureView.SurfaceTextureListener() {

    // 在view或view的祖先的可見性更改時調用。
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) {
        openCamera(width, height);
    }

    // 當此view的大小更改時調用此方法。
    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture texture, int width, int height) {
        configureTransform(width, height);
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture texture) {
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture texture) {
    }

};

兩個方法都在上面介紹過,也不再細說了。

CameraDevice.StateCallback

我們再來看下這個方法

private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {

    @Override
    public void onOpened(@NonNull CameraDevice cameraDevice) {
        // camera開啓時
        mCameraOpenCloseLock.release();
        mCameraDevice = cameraDevice;
        createCameraPreviewSession();
    }

    @Override
    public void onDisconnected(@NonNull CameraDevice cameraDevice) {
        // camera摧毀時
        mCameraOpenCloseLock.release();
        cameraDevice.close();
        mCameraDevice = null;
    }

    @Override
    public void onError(@NonNull CameraDevice cameraDevice, int error) {
        // camera報錯時
        mCameraOpenCloseLock.release();
        cameraDevice.close();
        mCameraDevice = null;
        Activity activity = getActivity();
        if (null != activity) {
            activity.finish();
        }
    }

};

首先先來看opOpened方法中的createCameraPreviewSession方法:

Camera2BasicFragment # createCameraPreviewSession()

private void createCameraPreviewSession() {
    try {
        SurfaceTexture texture = mTextureView.getSurfaceTexture();
        assert texture != null;

        // 我們將默認緩衝區的大小配置爲所需的相機預覽大小。
        texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());

        // 這是我們需要開始預覽的輸出Surface。
        Surface surface = new Surface(texture);

        // 我們用輸出Surface設置CaptureRequest.Builder。
        mPreviewRequestBuilder
                = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);

        // 在這裏,我們爲相機預覽創建一個CameraCaptureSession
        mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
                new CameraCaptureSession.StateCallback() {

                    @Override
                    public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                        // 相機已經關閉
                        if (null == mCameraDevice) {
                            return;
                        }

                        // 當會話準備好後,我們開始顯示預覽。
                        mCaptureSession = cameraCaptureSession;
                        try {
                            // 相機預覽時自動對焦應連續。
                            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                            // 必要時自動啓用flash。
                            setAutoFlash(mPreviewRequestBuilder);

                            // 最後,我們開始顯示相機預覽。
                            mPreviewRequest = mPreviewRequestBuilder.build();
                            mCaptureSession.setRepeatingRequest(mPreviewRequest,
                                    mCaptureCallback, mBackgroundHandler);
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onConfigureFailed(
                            @NonNull CameraCaptureSession cameraCaptureSession) {
                        showToast("Failed");
                    }
                }, null
        );
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

然後回到StateCallback。onDisconnected和onError的內容相似,區別就是onError退出的同時把Activity也強制關閉了。

CameraCaptureSession.CaptureCallback

private CameraCaptureSession.CaptureCallback mCaptureCallback
        = new CameraCaptureSession.CaptureCallback() {

    private void process(CaptureResult result) {
        switch (mState) {
            case STATE_PREVIEW: {
                // 當相機預覽正常工作時,我們什麼也不做。
                break;
            }
            case STATE_WAITING_LOCK: {
                Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
                if (afState == null) {
                    captureStillPicture();
                } else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
                        CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
                    // CONTROL_AE_STATE在某些設備上可以爲空
                    Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
                    if (aeState == null ||
                            aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
                        mState = STATE_PICTURE_TAKEN;
                        captureStillPicture();
                    } else {
                        runPrecaptureSequence();
                    }
                }
                break;
            }
            case STATE_WAITING_PRECAPTURE: {
                // CONTROL_AE_STATE在某些設備上可以爲空
                Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
                if (aeState == null ||
                        aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE ||
                        aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED) {
                    mState = STATE_WAITING_NON_PRECAPTURE;
                }
                break;
            }
            case STATE_WAITING_NON_PRECAPTURE: {
                // CONTROL_AE_STATE在某些設備上可以爲空
                Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
                if (aeState == null || aeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE) {
                    mState = STATE_PICTURE_TAKEN;
                    captureStillPicture();
                }
                break;
            }
        }
    }

這塊也沒啥多講的,看下注釋。

然後再看看captureStillPicture()方法:

Camera2BasicFragment # captureStillPicture()

private void captureStillPicture() {
    try {
        final Activity activity = getActivity();
        if (null == activity || null == mCameraDevice) {
            return;
        }
        // 這是我們用來拍照的capturerequest.builder。
        final CaptureRequest.Builder captureBuilder =
                mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());

        // 使用與預覽相同的ae和af模式。
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        setAutoFlash(captureBuilder);

        // Orientation
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));

        CameraCaptureSession.CaptureCallback captureCallback
                = new CameraCaptureSession.CaptureCallback() {

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                                           @NonNull CaptureRequest request,
                                           @NonNull TotalCaptureResult result) {
                showToast("Saved: " + mFile);
                Log.d(TAG, mFile.toString());
                unlockFocus();
            }
        };

        mCaptureSession.stopRepeating();
        mCaptureSession.abortCaptures();
        mCaptureSession.capture(captureBuilder.build(), captureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

看到這裏代碼已經不復雜了,組裝好我們的請求然後用CameraCaptureSession發送這個請求就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章