js获取图片信息




function getImageData(img) {
	return new Promise(function(resolve, reject) {
		function handleBinaryFile(binFile) {
			var data = findEXIFinJPEG(binFile);
			var iptcdata = findIPTCinJPEG(binFile);
			resolve({
				exif: data || {},
				iptc: iptcdata || {}
			})
		}

		if (/^data\:/i.test(img)) { // Data URI
			var arrayBuffer = base64ToArrayBuffer(img);
			handleBinaryFile(arrayBuffer);
		} else if (/^blob\:/i.test(img)) { // Object URL
			var fileReader = new FileReader();
			fileReader.onload = function(e) {
				handleBinaryFile(e.target.result);
			};
			objectURLToBlob(img, function(blob) {
				fileReader.readAsArrayBuffer(blob);
			});
		} else {
			if (typeof window === 'object' && 'document' in window) {
				var http = new XMLHttpRequest();
				http.onload = function() {
					if (this.status == 200 || this.status === 0) {
						handleBinaryFile(http.response);
					} else {
						throw "Could not load image";
					}
					http = null;
				};
				http.open("GET", img, true);
				http.responseType = "arraybuffer";
				http.send(null);
				return
			}
			if (typeof plus === 'object') {
				plus.io.resolveLocalFileSystemURL(getLocalFilePath(img), function(entry) {
					entry.file(function(file) {
						var fileReader = new plus.io.FileReader()
						fileReader.onload = function(data) {
							var arrayBuffer = base64ToArrayBuffer(data.target.result);
							handleBinaryFile(arrayBuffer)
						}
						fileReader.onerror = function(error) {
							reject(error)
						}
						fileReader.readAsDataURL(file)
					}, function(error) {
						reject(error)
					})
				}, function(error) {
					reject(error)
				})
				return
			}
			if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
				wx.getFileSystemManager().readFile({
					filePath: img,
					success: function(res) {
						handleBinaryFile(res.data)
					},
					fail: function(error) {
						reject(error)
					}
				})
				return
			}
			reject(new Error('not support'))
		}
	})
}

function ToDigital(strDu, strFen, strMiao, len) {
    len = (len > 6 || typeof (len) == "undefined") ? 6 : len;//精确到小数点后最多六位   
    strDu = (typeof (strDu) == "undefined" || strDu == "") ? 0 : parseFloat(strDu);
    strFen = (typeof (strFen) == "undefined" || strFen == "") ? 0 : parseFloat(strFen) / 60;
    strMiao = (typeof (strMiao) == "undefined" || strMiao == "") ? 0 : parseFloat(strMiao) / 3600;
    var digital = strDu + strFen + strMiao;
    if (digital == 0) {
        return "";
    } else {
        return digital.toFixed(len);
    }
}

function getFloatLocationByExif(exif){
	if(!exif.GPSLatitude||!exif.GPSLongitude){
		return null
	}
	let lat = (exif.GPSLatitudeRef == 'S'?-1:1)*ToDigital(exif.GPSLatitude[0],exif.GPSLatitude[1],exif.GPSLatitude[2])
	let lon = (exif.GPSLongitudeRef == 'W'?-1:1)*ToDigital(exif.GPSLongitude[0],exif.GPSLongitude[1],exif.GPSLongitude[2])
	
	return {lat:lat,lon:lon}
}

function getLocalFilePath(path) {
	if (path.indexOf('_www') === 0 || path.indexOf('_doc') === 0 || path.indexOf('_documents') === 0 || path.indexOf(
			'_downloads') === 0) {
		return path
	}
	if (path.indexOf('file://') === 0) {
		return path
	}
	if (path.indexOf('/storage/emulated/0/') === 0) {
		return path
	}
	if (path.indexOf('/') === 0) {
		var localFilePath = plus.io.convertAbsoluteFileSystem(path)
		if (localFilePath !== path) {
			return localFilePath
		} else {
			path = path.substr(1)
		}
	}
	return '_www/' + path
}

function objectURLToBlob(url, callback) {
	var http = new XMLHttpRequest();
	http.open("GET", url, true);
	http.responseType = "blob";
	http.onload = function(e) {
		if (this.status == 200 || this.status === 0) {
			callback(this.response);
		}
	};
	http.send();
}


function base64ToArrayBuffer(base64, contentType) {
	contentType = contentType || base64.match(/^data\:([^\;]+)\;base64,/mi)[1] || ''; // e.g. 'data:image/jpeg;base64,...' => 'image/jpeg'
	base64 = base64.replace(/^data\:([^\;]+)\;base64,/gmi, '');
	var binary = atob(base64);
	var len = binary.length;
	var buffer = new ArrayBuffer(len);
	var view = new Uint8Array(buffer);
	for (var i = 0; i < len; i++) {
		view[i] = binary.charCodeAt(i);
	}
	return buffer;
}

function findEXIFinJPEG(file) {
	var dataView = new DataView(file);

	if (debug) console.log("Got file of length " + file.byteLength);
	if ((dataView.getUint8(0) != 0xFF) || (dataView.getUint8(1) != 0xD8)) {
		if (debug) console.log("Not a valid JPEG");
		return false; // not a valid jpeg
	}

	var offset = 2,
		length = file.byteLength,
		marker;

	while (offset < length) {
		if (dataView.getUint8(offset) != 0xFF) {
			if (debug) console.log("Not a valid marker at offset " + offset + ", found: " + dataView.getUint8(offset));
			return false; // not a valid marker, something is wrong
		}

		marker = dataView.getUint8(offset + 1);
		if (debug) console.log(marker);

		// we could implement handling for other markers here,
		// but we're only looking for 0xFFE1 for EXIF data

		if (marker == 225) {
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值