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

Ios XCode条形码扫描仪Phonegap自动对焦,ios,cordova,zxing,autofocus,Ios,Cordova,Zxing,Autofocus,我开发了phonegap ios应用程序。我使用了条形码扫描仪zxing库。但我有个问题 如何实现相机自动对焦 多谢各位 我的代码: -(NSString*)setUpCaptureSession { NSError* error = nil; AVCaptureSession* captureSession = [[[AVCaptureSession alloc] init] autorelease]; self.captureSession = captureSes

我开发了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
}

您使用的是zxing的哪个实现?zxingObjC有一个关于捕获对象“setFocusMode”的方法不,我使用phonegap插件。这个链接:setSession代码:Paul谢谢。我不是objective c程序员。我是初学者。我检查了我的插件代码和setSession代码块。如何实现您的代码?我尝试过,但没有改变。我添加了我的设置会话代码块OK,我有点困惑,因为你发布的代码不是来自你链接到的模块。不管你的变量
设备
与我的变量
设备
在答案中是一样的,所以你可以添加代码来设置自动对焦模式,就像我的一样-从
NSError*error
开始,我更改了我的代码。ios 6版本正在运行,但ios 7没有自动对焦。问题是什么?你的想法?谢谢您在iOS 7设备上使用的摄像头是否支持自动对焦?您可以调用
[设备isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]
获取布尔值指示Paul,我正在测试代码。我发现了一只虫子。问题是,调度获取主队列。此代码是我的camare预览做模糊问题。焦点不工作。但ı创建了新的调度线程。每件事都很好,但扫描后是不工作的。为什么?