フロントカメラで作業していますが、フレームをキャプチャするとCCWが90度回転します。キャプチャされたイメージをCGContextで回転させたいのです(Core Graphicは他のフレームワークよりもはるかに高速です)。CGContextのSamplebufferからキャプチャされたフレームを回転する
はここに私のコードです:
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))
let width = CVPixelBufferGetWidthOfPlane(imageBuffer!, 0)
let height = CVPixelBufferGetHeightOfPlane(imageBuffer!, 0)
let bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer!, 0)
let lumaBuffer = CVPixelBufferGetBaseAddressOfPlane(imageBuffer!, 0)
let grayColorSpace = CGColorSpaceCreateDeviceGray()
let context = CGContext(data: lumaBuffer,
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: bytesPerRow,
space: grayColorSpace,
bitmapInfo: CGImageAlphaInfo.none.rawValue);
let transform1 = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
context!.concatenate(transform1)
//context!.rotate(by: CGFloat(M_PI_2)) // also not working!
let dstImage = context!.makeImage()
detect(image: dstImage!)
}