【问题标题】:Android custom camera result image orientationAndroid自定义相机结果图像方向
【发布时间】:2012-02-07 16:47:01
【问题描述】:

我正在编写一个遵循本教程的自定义相机应用程序: http://marakana.com/forums/android/examples/39.html

现在我可以在用户单击按钮后使用相机并获取图像数据。 但是我遇到了预览是横向的问题,所以我在surfaceCreated中添加了以下代码

@Override
public void surfaceCreated(SurfaceHolder holder) {
    //  Open the camera when the preview is created
    Log.d("Callback", "[surfaceCreated]");
    theCamera = Camera.open();

    Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    int displayRotation = display.getRotation();
    switch (displayRotation)  {
        case Surface.ROTATION_0:        theCamera.setDisplayOrientation(90);    break;
        case Surface.ROTATION_90:       break;
        case Surface.ROTATION_180:      break;
        case Surface.ROTATION_270:      theCamera.setDisplayOrientation(180);   break;
    }
}

预览很好。 但是,当我得到图像数据时,我使用BitmapFactory.decodeByteArray 来获取位图,并将其设置为在我的 xml 布局中定义的 imageView。

这是我的 main.xml

<SurfaceView
    android:id="@+id/previewSurfaceView"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="center" />

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="250px" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:maxHeight="100dp"
        android:maxWidth="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="250px" >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:maxHeight="100dp"
        android:maxWidth="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

previewSurfaceView 是用于相机预览的小视图

button1和button2是我点击时的按钮,摄像头捕捉图像并设置imageView1/imageView2对应的imagedata

有一些问题:

  1. 我将布局宽度和高度以及最大宽度和高度设置为 100dp,并将比例类型设置为 fitCenter。但它似乎没有显示为我想要的。我希望图像显示在边界内。我在布局设置中做错了吗?

  2. 即使我以纵向模式拍照,图像也始终是横向的。

此外,我想添加一个按钮来切换相机的开/关,但我不知道如何做到这一点。

有人可以为我建议解决方案吗?谢谢。

【问题讨论】:

    标签: android camera


    【解决方案1】:

    看看我的回答here,因为它似乎与您遇到的问题相同。

    【讨论】:

    • 对不起,我在上面发布了错误的代码。我在表面上没有问题。但是我怎样才能得到图像数据的方向。好像无法从数据中获取EXIF
    【解决方案2】:

    需要在相机参数对象上调用setRotation方法,然后在相机上设置参数。

    实现OrientationListener 以使用setRotation 中的值进行轮换

    示例代码使用here。

    【讨论】:

    • 我尝试将其硬编码为 0、90、180 和 270。但图像数据仍然是横向的。我希望图像始终以纵向模式拍摄并将其显示为纵向模式
    • 将设备保持在纵向模式并将旋转设置为90,您应该可以在纵向模式下获得一张照片。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    相关资源
    最近更新 更多