繁体   English   中英

Camera2 API 制作预览填充整个视图

[英]Camera2 API Make Preview Fill Entire View

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

<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 负责捕获图像。 那是主要的父活动。

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

我遇到了同样的问题,我使用的是 Google 示例,并将其调整到我的项目中。 为了修复它,我在 configureTransform 方法中添加了一个 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);
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM