簡體   English   中英

需要幫助來了解Google的Camera2Basic示例

[英]Need help in understanding Camera2Basic Sample from google

我正在追蹤來自Google的camera2basic示例,以了解有關camera2 API的信息。 我在努力理解以下方法,尤其是該方法的要求及其作用,因為它沒有正確記錄。

   /**
 * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`.
 * This method should be called after the camera preview size is determined in
 * setUpCameraOutputs and also the size of `mTextureView` is fixed.
 *
 * @param viewWidth  The width of `mTextureView`
 * @param viewHeight The height of `mTextureView`
 */
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);
}

任何線索將不勝感激。

這是倉庫的鏈接: -Camera2BasicFragment.java

我加了0º和180º

@Override public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {

            configCamara(width, height);
            openCamera();
            previewRotation(width, height);
        }
private void previewRotation(int width, int height) {
        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        Matrix matrix = new Matrix();
        RectF textureRectF = new RectF(0, 0, width, height);
        RectF previewRectF = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth());
        float centerX = textureRectF.centerX();
        float centery = textureRectF.centerY();

        if (rotation == Surface.ROTATION_270 || rotation == Surface.ROTATION_90) {
            previewRectF.offset(centerX - previewRectF.centerX(), centery - previewRectF.centerY());
            matrix.setRectToRect(textureRectF, previewRectF, Matrix.ScaleToFit.FILL);
            float scale = Math.max((float) width / mTextureView.getWidth(), (float) height / mTextureView.getHeight());

            matrix.postScale(scale, scale, centery, centerX);
            matrix.postRotate(90 * (rotation - 2), centerX, centery);
        } else if (rotation == Surface.ROTATION_0) {
            matrix.postRotate(rotation, centerX, centery);
        }else if (rotation == Surface.ROTATION_180) {
            matrix.postRotate(180, centerX, centery);
        }
        mTextureView.setTransform(matrix);
    }

當設備方向為Surface.ROTATION_90或Surface.ROTATION_270時,有兩個步驟來構造TextureView的變換:縮放和旋轉。 在Android演示的代碼中,第一步是縮放,第二步是旋轉。 這很難理解。

我更改了兩個步驟的順序。 在我的代碼中,第一步是旋轉,第二步是縮放。

private Matrix mTempMatrix = new Matrix();
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);
    float centerX = viewRect.centerX();
    float centerY = viewRect.centerY();
    if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
        //1. scale
        matrix.postRotate(90 * (rotation - 2), centerX, centerY);
        //2. rotate
        RectF rotatedRect = new RectF(0, 0, viewHeight, viewWidth);
        rotatedRect.offset(centerX - rotatedRect.centerX(), centerY - rotatedRect.centerY());
        RectF bufferRect = new RectF(0, 0, mPreviewSize.getWidth(), mPreviewSize.getHeight());
        bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
        mTempMatrix.setRectToRect(rotatedRect, bufferRect, Matrix.ScaleToFit.FILL);
        matrix.postConcat(mTempMatrix);
        float scale = Math.max(
                (float) viewHeight / mPreviewSize.getHeight(),
                (float) viewWidth / mPreviewSize.getWidth());
        matrix.postScale(scale, scale, centerX, centerY);
    } else if (Surface.ROTATION_180 == rotation) {
        matrix.postRotate(180, centerX, centerY);
    }
    mTextureView.setTransform(matrix);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM