java android 相机在横向模式下拉伸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7580887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:34:50  来源:igfitidea点击:

android camera stretched in landscape mode

javaandroidcameralandscapestretched

提问by Joey Roosing

The app I'm writing requires camera functionality. So to learn about how to operate the camera, I followed this script:

我正在编写的应用程序需要相机功能。所以为了学习如何操作相机,我遵循了这个脚本:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

I have put the activity in my manifest, set the screen orientation for it on landscape mode.

我已将活动放在我的清单中,在横向模式下为其设置屏幕方向。

The problem I'm having is, when the camera is held sideways (so I hold my Galaxy Tab P1000 in landscape position) the view is stretched out.

我遇到的问题是,当相机侧放时(所以我将 Galaxy Tab P1000 保持在横向位置),视图会被拉长。

To be more specific about my script, I used an exact copy of code that Google made. It can be found in the android-sdk\samples\android-8\ApiDemos\src\com\example\android\apis\graphics\

为了更具体地说明我的脚本,我使用了 Google 制作的代码的精确副本。可以在android-sdk\samples\android-8\ApiDemos\src\com\example\android\apis\graphics\

The file itself is called CameraPreview.

该文件本身称为 CameraPreview。

I really have no clue why the screen looks so stretched. Of course, the format is weird and not square, but still, when using the default camera app installed on the device, it doesn't deform at all. This camera deforms the image when I hold it sideways and move the camera even a little.

我真的不知道为什么屏幕看起来如此拉伸。当然,格式很奇怪而且不是方形的,但是,当使用设备上安装的默认相机应用程序时,它根本不会变形。当我将它放在一边并稍微移动相机时,该相机会使图像变形。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

What I did was: I held my galaxy tab to take a picture of an object (laptop in this case) then took a picture with my phone of my Galaxy. On the Galaxy I have the camera screen open in the app i'm making. This counts for both images. One I hold sideways and one I hold in portrait view. The pics are a bit unclear but you can see that in the landscape picture, the camera has become massively wide.

我所做的是:我拿着我的 Galaxy Tab 给一个物体(在这种情况下是笔记本电脑)拍照,然后用我的 Galaxy 手机拍了一张照片。在 Galaxy 上,我在我正在制作的应用程序中打开了相机屏幕。这对两个图像都很重要。一个我侧着拿着,一个我在纵向视图中拿着。图片有点不清楚,但你可以看到在风景图片中,相机变得非常宽。

采纳答案by Lord

I faced the same problem yesterday. After a researching "Camera" sources I found a reason for camera preview being stretched.

我昨天遇到了同样的问题。在研究了“相机”来源后,我发现了相机预览被拉伸的原因。

The reason is: SurfaceView aspect ratio (width/height) MUST be same as Camera.Size aspect ratio used in preview parameters. And if aspect ratio is not the same, you've got stretched image.

原因是:SurfaceView 纵横比(宽度/高度)必须与预览参数中使用的 Camera.Size 纵横比相同。如果纵横比不一样,你就会得到拉伸的图像。

So, the fastest workaround is to set SurfaceView to size like 320px x 240px - smallest supported size from Parameters.getSupportedPreviewSizes().

因此,最快的解决方法是将 SurfaceView 设置为 320px x 240px 之类的大小——Parameters.getSupportedPreviewSizes() 支持的最小大小。

Also, you can look at Camera standard application sources, it uses the custom layout for controlling the SurfaceView size (see PreviewFrameLayout.java, onMeasure() function).

此外,您可以查看 Camera 标准应用程序源,它使用自定义布局来控制 SurfaceView 大小(请参阅 PreviewFrameLayout.java、onMeasure() 函数)。

Use

利用

git clone https://android.googlesource.com/platform/packages/apps/Camera.git

git 克隆https://android.googlesource.com/platform/packages/apps/Camera.git

to get Camera sources.

获取相机来源。

回答by Quoc Nguyen

public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub

        try {
            camera = Camera.open();

            camera.setDisplayOrientation(90);
            camera.setPreviewDisplay(holder);
            Camera.Parameters parameters = camera.getParameters();
            List<Size> sizes = parameters.getSupportedPictureSizes();
            parameters.setPictureSize(sizes.get(0).width, sizes.get(0).height); // mac dinh solution 0
            parameters.set("orientation","portrait");
            //parameters.setPreviewSize(viewWidth, viewHeight);
            List<Size> size = parameters.getSupportedPreviewSizes();
            parameters.setPreviewSize(size.get(0).width, size.get(0).height);
            camera.setParameters(parameters);
            camera.startPreview();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

You need only getSupportedPreviewSizes()and save it to a List:

您只需getSupportedPreviewSizes()要将其保存到一个List

List<Size> size = parameters.getSupportedPreviewSizes();
            parameters.setPreviewSize(size.get(0).width, size.get(0).height);
            camera.setParameters(parameters); 



camera.startPreview(); 

I hope this helps you.

我希望这可以帮助你。