ios Swift 3:如何在48字节的CFData中访问matrix_float3x3的值?

7hiiyaii  于 2023-05-23  发布在  iOS
关注(0)|答案(3)|浏览(153)

我试图访问这个answer后面的固有矩阵。
通过运行下面的commission,我能够得到一个48字节的AnyObject,并进一步将其转换为CFData

let camData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil)

但是,我检查了CMSampleBuffer.h中sampleBuffer的输出:

/*! @constant   kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix
     @abstract  Indicates the 3x3 camera intrinsic matrix applied to the current sample buffer.
     @discussion Camera intrinsic matrix is a CFData containing a matrix_float3x3, which is column-major.
        ....
 */
CM_EXPORT const CFStringRef kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix  // CFData (matrix_float3x3) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_11_0);

如何从CFData访问matrix_float3x3中的值?

0qx6xfy6

0qx6xfy61#

这应该可以工作:

  • 使用从CFDataNSData再到Data的桥接,以及
  • withUnsafeBytes方法获得指向数据字节的所需类型的指针,
  • .pointee取消引用指针。

示例:

if let camData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil) as? Data {
    let matrix: matrix_float3x3 = camData.withUnsafeBytes { $0.pointee }

    // ...

}

指针类型(闭包内部的$0)从上下文推断为UnsafePointer<matrix_float3x3>

bf1o4zei

bf1o4zei2#

以下是Martin R在withUnsafeBytes被弃用后的回答的更新:

if let camData = CMGetAttachment(sampleBuffer, 
                                 key: kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, 
                                 attachmentModeOut: nil) as? Data {
    let matrix = camData.withUnsafeBytes { $0.load(as: matrix_float3x3.self) }
    ...
}
bis0qfac

bis0qfac3#

这是Martin R对Objective-c的回答:

CFTypeRef cameraIntrinsicData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil);
if(cameraIntrinsicData != nil){
    CFDataRef cfdr = (CFDataRef)(cameraIntrinsicData);
    matrix_float3x3 *camMatrix = (matrix_float3x3 *)(CFDataGetBytePtr(cfdr));
}

相关问题