Created
April 11, 2017 16:30
-
-
Save JayGoldberg/627e6a7ce21ef1e3ef0b1a74ca93a884 to your computer and use it in GitHub Desktop.
Google Cloud Function (HTTP trigger) to read the COM (comment) fields of a JPEG image
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
| // Get a reference to the Cloud Storage component | |
| var storage = require('@google-cloud/storage'); | |
| function findCOMmarkers(buffer) { | |
| var comment = []; | |
| for (var i=0; i < buffer.length; i++) { | |
| if (buffer[i] == '0xFF') { | |
| if (buffer[i+1] == '0xFE') { // JPEG COM marker | |
| comment.push(readcomment(buffer, i+4)); | |
| }; | |
| }; | |
| }; | |
| return comment; | |
| }; | |
| function readcomment(buffer, pos) { | |
| //TODO(): a static length would prob help performance | |
| var commentString = []; | |
| for (true; pos < buffer.length; pos++) { | |
| if (buffer[pos] != '0xFF') { | |
| commentString.push(buffer[pos]); | |
| } else { | |
| break; | |
| }; | |
| }; | |
| return commentString; | |
| }; | |
| function bytesToHex(bytes) { | |
| for (var hex = [], i = 0; i < bytes.length; i++) { | |
| hex.push((bytes[i] >>> 4).toString(16)); | |
| hex.push((bytes[i] & '0xF').toString(16)); | |
| } | |
| return hex.join(""); | |
| }; | |
| exports.readJPGCOM = function readbytes(req, res) { | |
| var pos = 0; | |
| var comment = 0; | |
| var buflength = 300; // all comments should be in the first 300 bytes ;-) | |
| var gcs = storage({ | |
| projectId: process.env.GCP_PROJECT, | |
| keyFilename: "./camfunctions01-925a41b2138f.json" | |
| }); | |
| // Reference an existing bucket object | |
| file = gcs.bucket('camimages').file('09/09_00_09.trig-01.jpg') | |
| file.createReadStream({ | |
| start: 0, | |
| end: buflength | |
| }) | |
| .on('error', function(err) {}) | |
| .on('end', function(data) { | |
| while ( pos + 2 <= 300 ) { | |
| pos += 2; | |
| } | |
| res.send(); // required or Cloud Function will hang | |
| }) | |
| .on('data', function(data) { | |
| comments = findCOMmarkers(data); | |
| }); | |
| for (i=0; i < comments.length; i++) { | |
| console.log(String.fromCharCode.apply(null, comments[i])) | |
| }; | |
| return; | |
| }; | |
| // check if we are running in local Node.js or Cloud Functions | |
| if (process.env['NODE_ENV'] != 'production') { | |
| var fs = require('fs'); | |
| var path = "21_49_29.trig+00.jpg"; | |
| var buflength = 200; | |
| var buffer = new Buffer(buflength); | |
| fs.open(path, 'r', function(status, fd) { | |
| if (status) { | |
| console.log(status.message); | |
| return; | |
| } | |
| fs.read(fd, buffer, 0, buffer.length, 0, function(err, num) { | |
| var comments = findCOMmarkers(buffer); | |
| for (j=0; j < comments.length; j++) { | |
| console.log(String.fromCharCode.apply(null, comments[j])); | |
| }); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment