Created
July 6, 2019 14:55
-
-
Save cxdy/aaa438f6f7a5b685533944dc0c1fd060 to your computer and use it in GitHub Desktop.
callback mania
This file contains 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
var ExifImage = require('exif').ExifImage; | |
var image = 'DSC_8041.jpg'; | |
// Step 5, send data off | |
function finish(EXIF) { | |
console.log(`Finished EXIF: ` + JSON.stringify(EXIF)); | |
return EXIF; | |
} | |
// Step 4, reformat data | |
function doMath(result, string, Image, Camera, Settings, callback) { | |
var nameJeff = `${result[0]}/${result[1]}`; | |
console.log(`${result[0]}/${result[1]}`); | |
//console.log('This: ' + Settings.shutterSpeed); | |
Settings.shutterSpeed = nameJeff; | |
console.log('========================'); | |
console.log('Updated Image: ' + JSON.stringify(Image)); | |
console.log('Updated Camera: ' + JSON.stringify(Camera)); | |
console.log('Updated Settings: ' + JSON.stringify(Settings)); | |
var EXIF = { | |
'Image': { | |
'Filename': Image.Filename, | |
'Author': Image.Author, | |
'dateTaken': Image.dateTaken, | |
'Resolution': Image.Resolution | |
}, | |
'Camera': { | |
'Make': Camera.Make, | |
'Model': Camera.Model, | |
'Lens': Camera.LensModel | |
}, | |
'Settings': { | |
'focalLength': Settings.FocalLength, | |
'shutterSpeed': Settings.shutterSpeed, | |
'Aperture': Settings.Aperture, | |
'ISO': Settings.ISO, | |
'Flash': Settings.Flash | |
} | |
}; | |
callback(EXIF); | |
} | |
// Step 3, convert shutter | |
function convertShutter(string, callback, Image, Camera, Settings) { | |
var input = string; | |
var length = string.length; | |
var index = []; | |
for (i = 0; i < length; i++) { | |
var theIndex = input.substr(i, 1) | |
index[i] = theIndex; | |
} | |
var arrLength = index.length; | |
var partOne = []; // Zeros | |
var partTwo = []; // Numbers larger than 0 | |
for (i = 0; i < arrLength; i++) { | |
var value = index[i]; | |
if (value <= 0) { | |
partOne += value | |
} else if (value === '.') { | |
partOne += value | |
} else if (value > 0 && value != null) { | |
partTwo += value | |
} else { | |
console.log(`${value} at ${i} did not fall in a category!`) | |
} | |
} | |
var arr1Len = partOne.length - 2; | |
var arr2Len = partTwo.length; | |
var power = Math.pow(10, arr1Len + arr2Len); | |
// Factoring Numerator, the first index of the second array (125 in this case) | |
var stepOneNumerator = partTwo; | |
// Take the power (100,000) and divide by the numerator (125) to get 800 (125 goes into 100,000 800 times) | |
var factoring = power / stepOneNumerator | |
// result[0] = Numerator, result[0] = denominator (result = regular shutter speed) | |
var result = [1, factoring]; | |
callback(result, input, Image, Camera, Settings, finish); | |
} | |
// Get Exif Data | |
// Step Two | |
function cb(Image, Camera, Settings) { | |
convertShutter(Settings.shutterSpeed, doMath, Image, Camera, Settings); | |
} | |
// Step One | |
function getEXIF(image, callback) { | |
try { | |
new ExifImage({ image: image }, function (error, exifData) { | |
if (error) | |
console.log('EXIF Error: ' + error.message); | |
else | |
// convert shutter to string | |
var oldSpeed = "" + exifData.exif.ExposureTime + ""; | |
var Image = {'Filename': image, 'Author': 'Cody Kaczynski', 'dateTaken': exifData.exif.DateTimeOriginal, 'Resolution': `${exifData.exif.FocalPlaneXResolution} x ${exifData.exif.FocalPlaneYResolution}`}; | |
var Camera = {'Make': exifData.image.Make, 'Model': exifData.image.Model, 'Lens': exifData.image.LensModel}; | |
var Settings = {'focalLength': exifData.exif.FocalLength, 'shutterSpeed': oldSpeed, 'Aperture': exifData.exif.FNumber, 'ISO': exifData.exif.ISO, 'Flash': exifData.exif.Flash}; | |
console.log(`Image Info: ` + JSON.stringify(Image)); | |
console.log(`Camera Info: ` + JSON.stringify(Camera)); | |
console.log(`Settings: ` + JSON.stringify(Settings)); | |
callback(Image, Camera, Settings) | |
}); | |
} catch (error) { | |
console.log('Error: ' + error.message); | |
} | |
} | |
// Step One | |
getEXIF(image, cb); | |
// getEXIF grabs EXIF info, formats it a little, passes it to a callback that converts the shutter speed, then passes that to a callback that formats data AGAIN, then passes to another callback for the final output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OUTPUT: