Created
December 29, 2017 14:15
-
-
Save hawkeye64/e67cb5aae8f46d9c94ea2c9a4bb9247b to your computer and use it in GitHub Desktop.
Using request to get snapshot from ONVIF camera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Previous gist: https://gist.github.com/hawkeye64/a30e43c8301626f7687b985b5e7b04bd | |
Axios couldn't handle working with Axis camera authentication because it uses Realm Digest. I have since changed my code to use the 'request' package. | |
NOTE: It is very *important* to have request do `'sendImmediately': false` which means that request won't send the authentication until the server (camera) responds in the header with *www-authenticate*, then request will send the authorization at that time. | |
commandFetchSnapshot (id) { | |
let self = this | |
return new Promise((resolve, reject) => { | |
let camera = self.cameras[id] | |
if (!camera) { | |
reject(new Error('Not Found')) | |
} | |
else { | |
// check that we have a snapshotUri attached to this camera | |
if ('snapshotUri' in camera) { | |
let snapshotUri = camera.snapshotUri.uri | |
request({ | |
method: 'GET', | |
uri: snapshotUri, | |
gzip: true, | |
encoding: 'binary', | |
'auth': { | |
'user': camera.username, | |
'pass': camera.password, | |
'sendImmediately': false | |
} | |
}, | |
function (error, response, body) { | |
if (error) { | |
console.error('commandFetchSnapshot error', error) | |
} | |
else { | |
// example: write image to file file to test | |
// const outputFilename = '/tmp/testfile.jpg' | |
// fs.writeFileSync(outputFilename, body, { encoding: 'binary' }) | |
// body is the decompressed response body | |
let mimeType = response.headers['content-type'] | |
let b64encoded = Buffer.from(body, 'binary').toString('base64') | |
let image = 'data:' + mimeType + ';base64,' + b64encoded | |
let data = { | |
mimeType: mimeType, | |
image: image | |
} | |
self.cameraOnline(camera) | |
resolve(data) | |
} | |
}) | |
} | |
else { | |
reject(new Error('No snapshotUri in commandFetchSnapshot')) | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment