Android自定义相机结果图像方向

发布于 2025-01-03 08:17:11 字数 3121 浏览 1 评论 0原文

我正在编写一个遵循本教程的自定义相机应用程序: 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. 即使我以纵向模式拍摄照片,图像也始终为横向。

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

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

I'm writing a custom camera application which follow this tutorial:
http://marakana.com/forums/android/examples/39.html

Now I'm able to use the camera and get the image data after user click a button.
But I got the problem that the preview is sideway so I add the following code in 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;
    }
}

The preview was fine.
However when I got the image data, I use BitmapFactory.decodeByteArray to get the bitmap, and set it to my imageView that is defined in my xml layout.

Here is my 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 is the small view that for the camera preview

button1 and button2 are the button when I clicked on, the camera capture the image and set the imagedata corresponding to imageView1/imageView2

There are some problems:

  1. I set the layout width and height and also the max width and height to 100dp, also the scale type to fitCenter. But it seems not to display as what I want. I want the image is show inside the bound. Am I doing wrong in the layout setting?

  2. The image is always in landscape even I took the picture in portrait mode.

Besides, I want to add a button to toggle the on/off of camera but I can't figure out how to do this.

Can somebody suggest the solution for me? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俯瞰星空 2025-01-10 08:17:11

看看我的答案这里,因为它看起来是相同的你遇到的问题。

Have a look at my answer here as it appears to be the same issue you are having.

葬シ愛 2025-01-10 08:17:11

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

实现 OrientationListener 以获取旋转,在 setRotation 中使用该值

示例代码用法 此处

You need to call the method setRotation on the camera parameter object and then set the parameter on the camera.

Implement the OrientationListener to get the rotation use that value in setRotation

Sample code usage here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文