gpt4 book ai didi

iphone - AVCaptureStillImageOutput 的 UIImage 方向不正确

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:18:34 24 4
gpt4 key购买 nike

我正在尝试从 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 setOutputSettings:outputSettings];

[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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com