gpt4 book ai didi

ios - 在后台捕获图像?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:11:03 25 4
gpt4 key购买 nike

我试图在不加载相机或预览界面的情况下从相机捕获背景中的图像。

在我的应用程序中,照片是在后台拍摄的,没有预览屏幕,只有正常的应用程序屏幕,稍后会显示给用户。

有人能给我指出正确的方向吗?

最佳答案

您必须使用 AVCaptureSession 和 AVCaptureDeviceInput。

这是部分代码,可能对您有帮助:

@interface MyViewController : UIViewController
{
AVCaptureStillImageOutput *_output;
AVCaptureConnection *_videoConnection;
bool _isCaptureSessionStarted;
}

@property (retain, nonatomic) AVCaptureDevice *frontalCamera;

- (void)takePhoto;

实现:

@interface MyViewController ()

@end

@implementation MyViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_isCaptureSessionStarted = false;
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

// Finding frontal camera
NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

for (int i = 0; i < cameras.count; i++) {
AVCaptureDevice *camera = [cameras objectAtIndex:i];

if (camera.position == AVCaptureDevicePositionFront) {
self.frontalCamera = camera;

[self.frontalCamera addObserver:self forKeyPath:@"adjustingExposure" options:NSKeyValueObservingOptionNew context:nil];
[self.frontalCamera addObserver:self forKeyPath:@"adjustingWhiteBalance" options:NSKeyValueObservingOptionNew context:nil];
}
}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (!self.frontalCamera.adjustingExposure && !self.frontalCamera.adjustingWhiteBalance) {
if (_isCaptureSessionStarted) {
[self captureStillImage];
}
}
}

- (void)takePhoto
{
if (self.frontalCamera != nil) {
// Add camera to session
AVCaptureSession *session = [[AVCaptureSession alloc] init];

NSError *error;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:self.frontalCamera error:&error];

if (!error && [session canAddInput:input]) {
[session addInput:input];

// Capture still image
_output = [[AVCaptureStillImageOutput alloc] init];

// Captured image settings
[_output setOutputSettings:[[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil]];

if ([session canAddOutput:_output]) {
[session addOutput:_output];

_videoConnection = nil;

for (AVCaptureConnection *connection in _output.connections) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
_videoConnection = connection;
break;
}
}
if (_videoConnection) {
break;
}
}

if (_videoConnection) {
[session startRunning];
NSLock *lock = [[[NSLock alloc] init] autorelease];
[lock lock];
_isCaptureSessionStarted = true;
[lock unlock];
}
}
} else {
NSLog(@"%@",[error localizedDescription]);
}
}
}

- (void) captureStillImage
{
[_output captureStillImageAsynchronouslyFromConnection:_videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

NSLock *lock = [[[NSLock alloc] init] autorelease];
[lock lock];
_isCaptureSessionStarted = false;
[lock unlock];

if (imageDataSampleBuffer != NULL) {
NSData *bitmap = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

// You can get image here via [[UIImage alloc] initWithData:bitmap]
}
}];
}

关于ios - 在后台捕获图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18736189/

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