需要帮助从google了解Camera2Basic Sample

时间:2016-12-10 21:45:32

标签: android image-processing graphics camera android-camera

我正在关注来自谷歌的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);
}

任何线索都会受到赞赏。

这里是repo的链接: - Camera2BasicFragment.java

2 个答案:

答案 0 :(得分:1)

我加了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);
    }

答案 1 :(得分:0)

当设备方向为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);
}