【问题标题】:Custom camera aspect ratio is incorrect自定义相机纵横比不正确
【发布时间】:2014-04-04 21:29:29
【问题描述】:

我在我的 Android 应用中构建了一个自定义相机活动,并在 FrameLayout 中呈现相机预览。不过,我遇到了运行 KitKat 的设备的问题 - 相机预览的纵横比似乎不正确(您可以看到左边的图像看起来垂直挤压)。但是,当我拍照时,保存的图像的纵横比是正确的(您可以从右侧的图像中看到)。

如何解决此纵横比问题?

这是我如何检查设备是否正在运行 KitKat,然后应用全屏布局:

// put the views behind the navigation bar
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN){
        Log.d(TAG, "adjusting to account for navigation bar");
          getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN );
}

这是我用来设置相机预览大小的:

private void setHolderParameters() {
    Log.d(TAG, "setting camera layout parameters");
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int height = metrics.heightPixels;
    int width = metrics.widthPixels;

    Size mPreviewSize = CameraPreview.getOptimalPreviewSize(camera
            .getParameters().getSupportedPreviewSizes(), width, height);

    Camera.Parameters parameters = camera.getParameters();
    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
    parameters.set("orientation", "portrait"); // default orientation to
                                               // portrait
    camera.setParameters(parameters);
    camera.setDisplayOrientation(90);
}

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:id="@+id/gallery_scroll_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:scaleType="fitXY"
         >

        <com.example.helperClass.PictureHorizontalLayout
            android:id="@+id/mygallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:scaleType="fitXY" >
        </com.example.helperClass.PictureHorizontalLayout>
    </HorizontalScrollView>

    <FrameLayout
        android:id="@+id/camerapreview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/black" 
        />   

    <LinearLayout
        android:id="@+id/button_holder_customized_cam"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_alignParentBottom="true"
        android:background="#838B8B"
        android:gravity="bottom"
        android:orientation="horizontal"
        android:weightSum="0.9"
        >

        <ImageButton
            android:id="@+id/gallery_customized_camera"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="1dp"
            android:layout_weight="0.3"
            android:adjustViewBounds="true"
            android:background="@null"
            android:contentDescription="@string/add"
            android:maxWidth="75dp"
            android:scaleType="center"
            android:src="@drawable/ic_action_picture" >

            <!-- android:background="@drawable/custom_button_blue" -->

        </ImageButton>

        <ImageButton
            android:id="@+id/shutter_customized_camera"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="1dp"
            android:layout_weight="0.3"
            android:adjustViewBounds="true"
            android:background="@drawable/custom_button_blue"
            android:contentDescription="@string/add"
            android:maxWidth="75dp"
            android:scaleType="center"
            android:src="@drawable/ic_action_camera" >
        </ImageButton>

        <ImageButton
            android:id="@+id/video_customized_camera"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.3"
            android:adjustViewBounds="true"
            android:background="@null"
            android:contentDescription="@string/add"
            android:maxWidth="75dp"
            android:scaleType="center"
            android:src="@drawable/ic_action_video" >

            <!-- android:background="@drawable/custom_button_blue" -->

        </ImageButton>
    </LinearLayout>

    <TextView
        android:id="@+id/camera_timer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/button_holder_customized_cam"
        android:layout_alignParentRight="true"
        android:padding="15dp"
        android:text="@string/no_time"
        android:textColor="@color/red"
        android:textSize="20sp"
        android:visibility="invisible" >
    </TextView>

</RelativeLayout>

【问题讨论】:

    标签: android camera android-4.4-kitkat aspect-ratio


    【解决方案1】:

    您提供给CameraPreview.getOptimalPreviewSize()widthheight应根据当前屏幕布局计算,并考虑系统在屏幕上显示的所有元素(例如导航栏)以及元素(例如button_holder_customized_cam),您的应用逻辑决定显示。

    使用前者,您的控制有限(例如切换到全屏布局);使用后者,您可以调整它们的大小以填充预览表面周围的间隙(使用正确的纵横比)。

    根据我的理解,如有必要,可以在一些边距中填充非侵入性的背景颜色。您可以将预览表面与其他视图叠加,这在视觉上相当于裁剪预览图像。实际上,裁剪预览图像本身在技术上是可行的(但并不容易)。

    【讨论】:

    • 感谢您的建议。奇怪的是,我把状态栏和导航栏都隐藏起来了,查看纵横比是否仍然关闭,即使相机预览占据了整个屏幕,它仍然被轻微压扁。所以我不确定发生了什么。
    • 通常,相机支持 4:3 或 16:9 预览;物理屏幕通常要高一些,在导航栏上方有一个标准的 16:9 区域。
    • 包含相机预览的FrameLayout的宽度和高度设置为wrap_contents,所以我认为它不会拉伸预览,即使物理屏幕不是16:9。你建议如何防止拉伸?
    • 我通常使用match_parent 预览SurfaceView,但这并没有太大区别,因为我覆盖了onMeasure()set measured dimensions 以适应预期的纵横比。
    猜你喜欢
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多