我想從AVCaptureStillImageOutput捕獲像素數據,並注意到裁剪圖像到CGImage時,它變得重新定向。爲了測試這個,我輸出一個臨時圖像到照片庫。現在我已經注意到,即使在圖像被裁剪之前,它的縮略圖也是旋轉的,而完整的圖像不是。 (當我將UIImage傳遞給需要適當尺寸的圖像的自定義像素數據管理器時,以後會成爲問題。)從AVCaptureStillImageOutput UIImage方向不正確
設置captureVideoPreviewLayer.orientation = AVCaptureVideoOrientationPortrait;
和[videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
似乎沒有任何改變。
任何想法??? 我將它分解成部分...
1)建立會話,輸入和輸出:
// 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);
//......
}
我會嘗試尋找[圖像imageOrientation],看到它把它在什麼方向,如果它被設置錯了,你可以先旋轉圖像,然後將其寫入到相冊。希望有所幫助! – msgambel
謝謝,我今晚會看一看。我其實甚至不需要相冊輸出。我只是做一個快速檢查來解釋爲什麼我的像素數據表現得像圖像旋轉一樣,而且足夠確定。 –