簡體   English   中英

XCode條形碼掃描儀Phonegap自動對焦

[英]XCode Barcode Scanner Phonegap Auto Focus

我開發了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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM