java - Camera2 API 使预览填充整个 View

标签 java android android-layout android-fragments android-camera2

我正在使用 Google Samples (https://github.com/googlesamples/android-Camera2Basic) 中的 Camera2Basic 示例在 fragment 内显示相机预览。 此 fragment 包含一个 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 中显示了总共三个 fragment 。我确实对 Camera2BasicFragment 做了一些小改动,使其成为 android.support.v4.app.Fragment 以与我的 ViewPager 一起使用并删除按钮。

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

这是我目前拥有的: current incorrect display of camera preview

FloatingActionButton 负责捕获图像。这是主要的父 Activity 。

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

最佳答案

我遇到了同样的问题,我使用的是 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);
}

关于java - Camera2 API 使预览填充整个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41790052/

相关文章:

java - 常见做法是什么?类型放置

android - 如何在 Kotlin Android 中为数据类创建空构造函数

android - 如何与 OBD II ELM327 适配器​​进行连续通信?

android - 通过 id 查找列表项 Root View

java - 在默认浏览器中打开本地 URL 时删除参数

java - Spring mvc web.xml错误

java - 当您将并发设置为 false 时,Quartz 是否会触发 "pile up"

位图的 Android 共享 Intent - 是否可以在共享之前不保存它?

java - 自定义键盘的候选 View 会扰乱 WhatsApp 和其他应用布局

android - 无法实例化一个或多个类TextInputEditText内部错误