我使用的是前置摄像头,当我捕捉到这些帧时,它们被旋转了90度。我想在CGContext中旋转捕获的图像(因为我读到Core Graphic比其他框架快得多)。
下面是我的代码:
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!)
}
发布于 2016-12-05 23:48:35
经过大量的搜索和阅读,我在AVCaptureConnection类中找到了我的解决方案!我只需要请求连接在缓冲区内旋转帧,并在捕获函数中使用以下示例代码!!
if connection.isVideoOrientationSupported
{
connection.videoOrientation = .portrait
}
if connection.isVideoMirroringSupported
{
connection.isVideoMirrored = true
}
就这样!
发布于 2018-05-31 17:29:34
如果您使用的是方法:
- (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection*)connection
您可以更改缓冲区的方向以获得正确的图像,下面是我制作的一个示例:
UIInterfaceOrientation iOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(iOrientation == UIInterfaceOrientationLandscapeLeft){
[connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
}
else{
[connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
}
希望能有所帮助。
https://stackoverflow.com/questions/40918526
复制相似问题