Android方形相机拉伸

时间:2019-01-23 05:09:41

标签: android camera surfaceview

我尝试在surfaceview上制作方形摄像头,surfaceview已经方形,但是当显示状态栏和标题栏时,摄像头预览会拉伸。

这是我的camera_view_activity布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<!--Toolbar-->
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/actionbar_height"
    android:background="@color/white"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">

        <ImageView
            android:id="@+id/btnBack"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingTop="@dimen/view_padding"
            android:paddingBottom="@dimen/view_padding"
            android:src="@drawable/ic_arrow_back"
            android:tint="@color/greyishBrown" />

        <com.mycity.qlue.qlueapp.utils.CustomFontTextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="16dp"
            android:layout_toRightOf="@id/btnBack"
            android:gravity="center_vertical"
            android:text="Foto Laporan"
            android:textAlignment="center"
            android:textColor="@color/greyishBrown"
            android:textSize="@dimen/text_title"
            app:fontName="@string/font_roboto" />

    </RelativeLayout>

</android.support.v7.widget.Toolbar>

<!-- Camera Preview -->
<FrameLayout
    android:id="@+id/surfaceContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <SurfaceView
        android:id="@+id/camera_preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar" />

</FrameLayout>
<!-- /End Camera Preview -->

<!-- Overlay -->

<!-- Put your Content Here -->

<RelativeLayout
    android:id="@+id/overlay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/white">

    <LinearLayout
        android:id="@+id/lyCameraControl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/btn_flash"
            android:layout_width="21dp"
            android:layout_height="39dp"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="56dp"
            android:src="@drawable/ic_inactive_flash" />

        <Button
            android:id="@+id/btnCaptureImage"
            android:layout_width="108dp"
            android:layout_height="108dp"
            android:layout_marginBottom="32dp"
            android:background="@drawable/ic_camera_button" />

        <ImageView
            android:id="@+id/btnGallery"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="56dp"
            android:src="@drawable/gallery" />

    </LinearLayout>

</RelativeLayout>
<!-- /End Overlay -->

这就是我设置相机的方式:

private void setupCamera() {
    try {
        camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
        camera.setDisplayOrientation(90);
        Camera.Parameters camParams = camera.getParameters();

        // Find a preview size that is at least the size of our IMAGE_SIZE
        Camera.Size previewSize = camParams.getSupportedPreviewSizes().get(0);
        for (Camera.Size size : camParams.getSupportedPreviewSizes()) {
            if (size.width >= IMAGE_SIZE && size.height >= IMAGE_SIZE) {
                previewSize = size;
                break;
            }
        }

        camParams.setPreviewSize(previewSize.width, previewSize.height);

        // Try to find the closest picture size to match the preview size.
        Camera.Size pictureSize = camParams.getSupportedPictureSizes().get(0);
        for (Camera.Size size : camParams.getSupportedPictureSizes()) {
            if (size.width == previewSize.width && size.height == previewSize.height) {
                pictureSize = size;
                break;
            }
        }
        camParams.setPictureSize(pictureSize.width, pictureSize.height);
        if (camParams.getSupportedFocusModes().contains(
                Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
            camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
        }

        camera.setParameters(camParams);
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

这是我设置Surfaceview大小的方法

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    // Get the preview size
    int previewWidth = cameraPreview.getWidth(),
            previewHeight = cameraPreview.getHeight();

    // Set the height of the overlay so that it makes the preview a square
    RelativeLayout.LayoutParams overlayParams = (RelativeLayout.LayoutParams) overlay.getLayoutParams();
    overlayParams.height = (int) ((previewHeight - previewWidth));
    overlay.setLayoutParams(overlayParams);
}

就像我的屏幕截图一样,当我拍摄照片时,由于相机预览被拉长,所以在底部裁剪了一些像素。 当我显示状态栏和标题栏时,有什么方法可以使摄像头预览不拉伸?

enter image description here

enter image description here

0 个答案:

没有答案
相关问题