我正在构建一个相机扫描应用程序,该应用程序将使用外部SDK解码和处理所需输出的图像帧。 SDK公开的api用于读取视频输出的帧,并且具有类型为buffer: UnsafeMutablePointer<BYTES>
的参数,用于实际执行解码。
所以我想将视频帧的输出转换为UnsafeMutablePointer<UInt8>
我按照本教程https://medium.com/ios-os-x-development/ios-camera-frames-extraction-d2c0f80ed05a进行了操作,AVCaptureVideoDataOutputSampleBufferDelegate
我曾尝试将imageData转换为字节,但无法正常工作。
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
return
}
let width: Int = CVPixelBufferGetWidth(pixelBuffer)
let height = CVPixelBufferGetHeight(pixelBuffer)
let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else {
return
}
guard var imageData = UIImage(cgImage: cgImage).jpegData(compressionQuality: 1.0) else{
return
}
print("Width : \(width) & Height: \(height)")
print(imageData.count)
imageData.withUnsafeMutableBytes({(bytes: UnsafeMutablePointer<UInt8>) -> Void in
// Get Bytes Here
})
// I have tried bello code too - not working
/* let mem = UnsafeMutablePointer<UInt8>.allocate(capacity: imageData.count)
_ = imageData.copyBytes(to: UnsafeMutableBufferPointer(start: mem, count: 1))
let imageByte = mem.move()
mem.deallocate()
let imageByte = [UInt8](imageData) */
}
uint8_t *buffer;
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{ // got an image
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
size_t width = CVPixelBufferGetWidth (pixelBuffer);
size_t height = CVPixelBufferGetHeight (pixelBuffer);
size_t pitch = CVPixelBufferGetBytesPerRow(pixelBuffer);
uint8_t *source = (uint8_t *) CVPixelBufferGetBaseAddress(pixelBuffer);
if (buffer == NULL)
{
buffer = (uint8_t *) malloc(width * height);
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int sum;
sum = 77 * *source++;
sum += 151 * *source++;
sum += 28 * *source++;
*buffer++ = sum >> 8;
source++;
}
source += pitch - 4 * width;
}
buffer -= width * height;
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
// This works I can use the buffer now in the SDK
}
先谢谢了
因此,我希望能够在UnsafeMutablePointer中获取图像缓冲区。.谢谢