我正在尝试从 AVCaptureStillImageOutput 捕获像素数据,并注意到在将图像裁剪为 CGImage 后,它会重新定向。为了测试这一点,我将一个临时图像输出到照片库。现在我注意到,即使在图像被裁剪之前,它的缩略图也会旋转,而完整图像则不会。 (当我将 UIImage 传递给需要适当尺寸的图像的自定义 pixelDataManager 时,这后来成为一个问题。)
设置 captureVideoPreviewLayer.orientation = AVCaptureVideoOrientationPortrait;
和 [videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
似乎没有任何改变。
有什么想法吗??? 我会把它分成几个部分...
1) 设置 session 、输入和输出:
// Create a capture session
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
// Add capture layer to visible view
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResize;
captureVideoPreviewLayer.frame = screenBounds;
captureVideoPreviewLayer.bounds = screenBounds;
captureVideoPreviewLayer.orientation = AVCaptureVideoOrientationPortrait;
[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// Setup error handling
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
// Handle the error appropriately.
NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];
// Start session
[session startRunning];
// Output image data
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettingsutputSettings];
[session addOutput:stillImageOutput];
2) 设置视频连接:
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections)
{
for (AVCaptureInputPort *port in [connection inputPorts])
{
if ([[port mediaType] isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;
break;
}
}
if (videoConnection) { break; }
}
if ([videoConnection isVideoOrientationSupported])
{
[videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
}
3) 输出捕获的图像:
NSLog(@"about to request a capture from: %@", stillImageOutput);
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
//......
}
当您执行此操作时,虽然图像是作为 UIImage 提供给您的,但它实际上使用的是具有不同原点(我认为是左下角)的底层 Quartz CGImage 数据,这意味着当您使用图像时,它会旋转到一边。
我找到了一个 C 函数,您可以使用 UIImage 作为参数调用它来修复它并返回固定的 UIImage。
http://blog.logichigh.com/2008/06/05/uiimage-fix/
关于iphone - 来自 AVCaptureStillImageOutput 的 UIImage 方向不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7005211/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |