Camera2 API 使预览填充整个视图

Camera2 API Make Preview Fill Entire View

我正在使用 Google 示例 (https://github.com/googlesamples/android-Camera2Basic) 中的 Camera2Basic 示例在片段内显示相机预览。 此片段包含一个 RelativeLayout 和一个自定义 AutoFitTextureView。后者可以在上面的 link 中找到。我已调整布局以居中预览并删除控制按钮:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/camera_rel">

    <uk.co.deluxe_digital.messngr.AutoFitTextureView
    android:id="@+id/texture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

</RelativeLayout>

我在 ViewPager 中显示了总共三个片段。我确实对 Camera2BasicFragment 做了一些小改动,使其成为 android.support.v4.app.Fragment 以与我的 ViewPager 一起使用并删除按钮。

我想要的是让相机预览充满整个屏幕。这可以通过拉伸预览图像来实现。那不是我想要的。有谁知道一种放大预览或 TextureView 的方法,以便预览的高度与容器相匹配,即使侧面被裁剪掉也是如此。我希望它能填充视图并保持纵横比。这意味着两边的一些预览会丢失,但这没关系。

这是我目前拥有的:

FloatingActionButton 负责捕获图像。这是主要的 parent activity.

即使您能为我指出正确的方向来解决这个问题,那也是一个巨大的帮助!

AutoFitTextureView 扩展 TextureView 以保持纵横比。 如果你想填满整个屏幕,只需使用:

<TextureView
    android:id="@+id/texture"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 />

我遇到了同样的问题,我使用的是 Google 示例,并将其改编到我的项目中。为了修复它,我在 configuredTransform 方法中添加了一个 else 选项,以在垂直位置正确缩放。

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);
    } /*I added this else*/ else {
        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(0, centerX, centerY);

    }
    mTextureView.setTransform(matrix);
}