objective c - Capturing iSight image using AVFoundation on Mac -
i had code capture single image mac's isight camera using qtkit:
- (nserror*)takepicture { bool success; nserror* error; capturesession = [qtcapturesession new]; qtcapturedevice* device = [qtcapturedevice defaultinputdevicewithmediatype: qtmediatypevideo]; success = [device open: &error]; if (!success) { return error; } qtcapturedeviceinput* capturedeviceinput = [[qtcapturedeviceinput alloc] initwithdevice: device]; success = [capturesession addinput: capturedeviceinput error: &error]; if (!success) { return error; } qtcapturedecompressedvideooutput* capturevideooutput = [qtcapturedecompressedvideooutput new]; [capturevideooutput setdelegate: self]; success = [capturesession addoutput: capturevideooutput error: &error]; if (!success) { return error; } [capturesession startrunning]; return nil; } - (void)captureoutput: (qtcaptureoutput*)captureoutput didoutputvideoframe: (cvimagebufferref)imagebuffer withsamplebuffer: (qtsamplebuffer*)samplebuffer fromconnection: (qtcaptureconnection*)connection { cvbufferretain(imagebuffer); if (imagebuffer) { [capturesession removeoutput: captureoutput]; [capturesession stoprunning]; nsciimagerep* imagerep = [nsciimagerep imagerepwithciimage: [ciimage imagewithcvimagebuffer: imagebuffer]]; _result = [[nsimage alloc] initwithsize: [imagerep size]]; [_result addrepresentation: imagerep]; cvbufferrelease(imagebuffer); _done = yes; } }
however, found today qtkit has been deprecated , must use avfoundation. can me convert code avfoundation equivalent? seems though many methods have same name, @ same time, lot different , i'm @ complete loss here... help?
alright, found solution!! here is:
- (void)takepicture { nserror* error; avcapturedevice* device = [avcapturedevice defaultdevicewithmediatype: avmediatypevideo]; avcapturedeviceinput* input = [avcapturedeviceinput deviceinputwithdevice: device error: &error]; if (!input) { _error = error; _done = yes; return; } avcapturestillimageoutput* output = [avcapturestillimageoutput new]; [output setoutputsettings: @{(id)kcvpixelbufferpixelformattypekey: @(k32bgrapixelformat)}]; capturesession = [avcapturesession new]; capturesession.sessionpreset = avcapturesessionpresetphoto; [capturesession addinput: input]; [capturesession addoutput: output]; [capturesession startrunning]; avcaptureconnection* connection = [output connectionwithmediatype: avmediatypevideo]; [output capturestillimageasynchronouslyfromconnection: connection completionhandler: ^(cmsamplebufferref samplebuffer, nserror* error) { if (error) { _error = error; _result = nil; } else { cvimagebufferref imagebuffer = cmsamplebuffergetimagebuffer(samplebuffer); if (imagebuffer) { cvbufferretain(imagebuffer); nsciimagerep* imagerep = [nsciimagerep imagerepwithciimage: [ciimage imagewithcvimagebuffer: imagebuffer]]; _result = [[nsimage alloc] initwithsize: [imagerep size]]; [_result addrepresentation: imagerep]; cvbufferrelease(imagebuffer); } } _done = yes; }]; }
i hope helps whoever has problems in doing same thing.
Comments
Post a Comment