本文介绍了displayTransform 调用后图像方向错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下方法从当前 ARFrame 获取图像:

if let imageBuffer = sceneView.session.currentFrame?.capturedImage {让方向 = UIApplication.shared.statusBarOrientation让 viewportSize = sceneView.bounds.size让转换 = sceneView.session.currentFrame?.displayTransform(for:orientation, viewportSize: viewportSize)让 ciImage = CIImage(cvPixelBuffer: imageBuffer).transformed(by:transformation)}



对于景观,它效果很好.对于肖像,我以错误的角度(旋转 180 度)获取图像.知道为什么吗?

解决方案

一个问题是,当您将 Portrait 图像(ARFrame 包含的内容)转换为 CIImage 或 CGImage 时,它​​会失去方向并逆时针旋转 180 度.此问题仅影响 Portrait 图像.Landscape 完全不受影响.

发生这种情况是因为 Portrait 图像在转换阶段没有关于其方向的信息,因此,即使转换为 CIImage 或 CGImage,纵向图像仍保持纵向模式.>

要解决这个问题,您应该将标准"横向的 宽度/高度非标准"纵向的进行比较 width/height,如果这些值不同,则将图像旋转到顺时针 180 度(或应用方向情况 ).

希望这会有所帮助.

I am trying to get the image from the current ARFrame by using:

if let imageBuffer = sceneView.session.currentFrame?.capturedImage {
    let orientation = UIApplication.shared.statusBarOrientation
    let viewportSize = sceneView.bounds.size
    let transformation = sceneView.session.currentFrame?.displayTransform(for: orientation, viewportSize: viewportSize)

    let ciImage = CIImage(cvPixelBuffer: imageBuffer).transformed(by: transformation)
}



For landscape, it works great. For portrait, I get the image at a wrong angle (rotated by 180). Any idea why?

Output:

Expected:

解决方案

A problem is, when you convert an Portrait image, what ARFrame contains, to CIImage or CGImage, it loses its orientation and rotates it 180 degrees CCW. This issue affects only Portrait images. Landscape ones are not affected at all.

This happens because Portrait image doesn't have an info about its orientation at conversion stage, and thus, an image in portrait remains in portrait mode even though it's converted to CIImage or CGImage.

To fix this you should compare "standard" landscape's width/height with a "non-standard" portrait's width/height, and if these values are different, rotate an image to 180 degrees CW (or apply orientation case .portraitUpsideDown).

Hope this helps.

这篇关于displayTransform 调用后图像方向错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:32