简体   繁体   中英

Capturing image that exists within video preview layer

I'd like to capture only the image within the video preview layer. Currently, I set it like this:

[self setCaptureManager:[[[CaptureSessionManager alloc] init] autorelease]];
[[self captureManager] addVideoInputFrontCamera:YES]; // set to YES for Front Camera, No for Back camera
[[self captureManager] addStillImageOutput];

[[self captureManager] addVideoPreviewLayer];
CGRect layerRect = [[[self view] layer] bounds];
[[[self captureManager] previewLayer] setBounds:CGRectMake(0, 0, 320, 400)];//was layerRect
[[[self captureManager] previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];// was CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))

[[[self view] layer] addSublayer:[[self captureManager] previewLayer]];

As it stands, the preview layer represents what I want to be captured, but the camera captures whatever is outside those bounds as well. I can't take a picture and crop it afterwards because the image isn't orientation up, so CGImageRef doesn't work. Is there anyway to capture just what's in the preview layer?

This is not what the preview layer is supposed to do. If you need to take a picture you should use the stillImageOutput or the data buffer. According to the way you are capturing samples you shoul take care about the oreintation of the camera vs the device orientation. Orientation for jpg images is saved in a metadata dictionary called EXIF.
Apple has different samples code showing how to do still image capture. Here is also a blog post I've written about EXIF orientation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM