我正在尝试使用
Javascript从JPEG文件中提取IPTC照片字幕信息. (我知道我可以做这个服务器端,但我正在寻找一个Javascript解决方案.)
我发现了this script,它提取EXIF信息…但我不知道如何适应它抓住IPTC数据.
是否有任何现有的脚本提供这样的功能?如果没有,您将如何修改EXIF脚本以解析IPTC数据?
UPDATE
我修改了我上面链接的EXIF脚本.它排序我想要的,但它并没有抓住正确的数据100%的时间.
在401行之后,我补充说:
else if (iMarker == 237) { // 0xED = Application-specific 13 (Photoshop IPTC) if (bDebug) log("Found 0xFFED marker"); return readIPTCData(oFile,iOffset + 4,getShortAt(oFile,iOffset+2,true)-2); }
function readIPTCData(oFile,iStart,iLength) { exif = new Array(); if (getStringAt(oFile,9) != "Photoshop") { if (bDebug) log("Not valid Photoshop data! " + getStringAt(oFile,9)); return false; } var output = ''; var count = 0; two = new Array(); for (i=0; i<iLength; i++) { if (getByteAt(oFile,iStart + i) == 2 && getByteAt(oFile,iStart + i + 1) == 120) { var caption = getString2At(oFile,iStart + i + 2,800); } if (getByteAt(oFile,iStart + i + 1) == 80) { var credit = getString2At(oFile,300); } } exif['ImageDescription'] = caption; exif['Artist'] = credit; return exif; }
解决方法
对于什么是值得的,我推断了这一点…我没有做大量的测试,但几个测试图像我似乎工作.
var bDebug = false; var fieldMap = { 120 : 'caption',110 : 'credit',25 : 'keywords',85 : 'byline',122 : 'captionWriter',105 : 'headline',116 : 'copyright',15 : 'category' }; function readIPTCData(oFile,iLength) { var data = {}; if (oFile.getStringAt(iStart,9) != "Photoshop") { if (bDebug) log("Not valid Photoshop data! " + oFile.getStringAt(iStart,9)); return false; } var fileLength = oFile.getLength(); var length,offset,fieldStart,title,value; var FILE_SEPARATOR_CHAR = 28,START_OF_TEXT_CHAR = 2; for (var i = 0; i < iLength; i++) { fieldStart = iStart + i; if(oFile.getByteAt(fieldStart) == START_OF_TEXT_CHAR && oFile.getByteAt(fieldStart + 1) in fieldMap) { length = 0; offset = 2; while( fieldStart + offset < fileLength && oFile.getByteAt(fieldStart + offset) != FILE_SEPARATOR_CHAR && oFile.getByteAt(fieldStart + offset + 1) != START_OF_TEXT_CHAR) { offset++; length++; } if(!length) { continue; } title = fieldMap[oFile.getByteAt(fieldStart + 1)]; value = oFile.getStringAt(iStart + i + 2,length) || ''; value = value.replace('\000','').trim(); data[title] = value; i+=length-1; } } return data; } function findIPTCinJPEG(oFile) { var aMarkers = []; if (oFile.getByteAt(0) != 0xFF || oFile.getByteAt(1) != 0xD8) { return false; // not a valid jpeg } var iOffset = 2; var iLength = oFile.getLength(); while (iOffset < iLength) { if (oFile.getByteAt(iOffset) != 0xFF) { if (bDebug) console.log("Not a valid marker at offset " + iOffset + ",found: " + oFile.getByteAt(iOffset)); return false; // not a valid marker,something is wrong } var iMarker = oFile.getByteAt(iOffset+1); if (iMarker == 237) { if (bDebug) console.log("Found 0xFFED marker"); return readIPTCData(oFile,oFile.getShortAt(iOffset+2,true)-2); } else { iOffset += 2 + oFile.getShortAt(iOffset+2,true); } } } IPTC.readFromBinaryFile = function(oFile) { return findIPTCinJPEG(oFile); }