简体   繁体   English

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

[英]Camera2 API Make Preview Fill Entire View

I am using the Camera2Basic example from Google Samples ( https://github.com/googlesamples/android-Camera2Basic ) to display a camera preview inside a fragment.我正在使用 Google Samples ( https://github.com/googlesamples/android-Camera2Basic ) 中的 Camera2Basic 示例在片段内显示相机预览。 This fragment contains a RelativeLayout and a custom AutoFitTextureView.该片段包含一个 RelativeLayout 和一个自定义的 AutoFitTextureView。 The latter can be found on the link above.后者可以在上面的链接中找到。 I have adjusted the layout to center the preview and remove control buttons:我已调整布局以将预览居中并删除控制按钮:

<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>

I have three total fragments that are displayed within a ViewPager.我在 ViewPager 中总共显示了三个片段。 I did make some small changes to the Camera2BasicFragment to make it an android.support.v4.app.Fragment to work with my ViewPager and remove the buttons.我确实对 Camera2BasicFragment 进行了一些小的更改,使其成为 android.support.v4.app.Fragment 以与我的 ViewPager 一起使用并删除按钮。

What I would like is to make the camera preview fill the entire screen.我想要的是让相机预览填满整个屏幕。 This is possible by stretching the preview image.这可以通过拉伸预览图像来实现。 That is not what I want.那不是我想要的。 Does anyone know a way of scaling up the preview or TextureView so that the height of the preview matches the container even if the sides are cropped off.有谁知道一种放大预览或 TextureView 的方法,以便预览的高度与容器匹配,即使侧面被裁剪掉。 I would like it to fill the view and maintain aspect ratio.我希望它填充视图并保持纵横比。 This would mean that some of the preview at either side is lost, but that is okay.这意味着两边的一些预览会丢失,但没关系。

This is what I currently have:这是我目前拥有的: 当前相机预览显示不正确

The FloatingActionButton takes care of capturing the image. FloatingActionButton 负责捕获图像。 That is on the main parent activity.那是主要的父活动。

Even if you can point me in the right direction to working this out, that would be a huge help!即使你能指出我解决这个问题的正确方向,那也将是一个巨大的帮助!

I had a same problem, I was using the Google example and I adapted that to my project.我遇到了同样的问题,我使用的是 Google 示例,并将其调整到我的项目中。 To fix it I added an else option in configuredTransform method to scale correctly in vertical position.为了修复它,我在 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 extends TextureView to keep aspect ratio. AutoFitTextureView 扩展 TextureView 以保持纵横比。 If you want to fill the entire scree, just use:如果你想填满整个碎石,只需使用:

<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