gpt4 book ai didi

ios - XCode 条形码扫描仪 Phonegap 自动对焦

转载 作者:行者123 更新时间:2023-11-29 12:50:49 25 4
gpt4 key购买 nike

我开发了 phonegap ios 应用程序。我使用了条码扫描器 zxing 库。但我有一个问题如何实现相机自动对焦?

谢谢

我的代码:

-(NSString*)setUpCaptureSession {
NSError* error = nil;

AVCaptureSession* captureSession = [[[AVCaptureSession alloc] init] autorelease];
self.captureSession = captureSession;

AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (!device) return @"unable to obtain video capture device";

AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) return @"unable to obtain video capture device input";

AVCaptureVideoDataOutput* output = [[[AVCaptureVideoDataOutput alloc] init] autorelease];
if (!output) return @"unable to obtain video capture output";

NSDictionary* videoOutputSettings = [NSDictionary
dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
forKey:(id)kCVPixelBufferPixelFormatTypeKey
];


output.alwaysDiscardsLateVideoFrames = YES;
output.videoSettings = videoOutputSettings;

[output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

if (![captureSession canSetSessionPreset:AVCaptureSessionPresetMedium]) {
return @"unable to preset medium quality video capture";
}

captureSession.sessionPreset = AVCaptureSessionPresetMedium;

if ([captureSession canAddInput:input]) {
[captureSession addInput:input];
}
else {
return @"unable to add video capture device input to session";
}

if ([captureSession canAddOutput:output]) {
[captureSession addOutput:output];
}
else {
return @"unable to add video capture output to session";
}

// setup capture preview layer
self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];

// run on next event loop pass [captureSession startRunning]
[captureSession performSelector:@selector(startRunning) withObject:nil afterDelay:0];
return nil;
}

最佳答案

不幸的是,您使用的插件似乎没有直接公开捕获设备。但是,它确实通过 captureSession 属性公开了 AVCaptureSession。从这个属性你应该能够向后工作以获得 AVCaptureInputDevice

AVCaptureSession *session=[zxing captureSession];  //Assuming zxing the variable holding a reference to your zxing instance
NSArray *inputs= [session inputs];
AVCaptureInputDevice *input=(AVCaptureInputDevice *)inputs[0]; // Obtain first input device
AVCaptureDevice *device=input.device;

NSError *error;

if ([device lockForConfiguration:&error])
{
device.focusMode=AVCaptureFocusModeContinuousAutoFocus;
[device unlockForConfiguration];
}
else
{
// TODO Handle the device lock error
}

关于ios - XCode 条形码扫描仪 Phonegap 自动对焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22490392/

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