如何将元数据添加到Google相册中的图片

时间:2018-09-26 08:20:14

标签: google-photos

我刚刚通过api创建了我的第一张相册,上传了一张图片并添加到我的新相册中。但是如何指定时区,摄像机数据,位置等?

我真的需要通过exif嵌入那些信息吗?在那种情况下,我需要处理要避免的图像数据。

1 个答案:

答案 0 :(得分:0)

这是我的解决方法。我通过exif在nodejs中添加信息:

request.get('http://example.com/foto.jpg')
    .pipe(exifEnricher)
    .pipe(request.post('https://photoslibrary.googleapis.com/v1/uploads', {
        headers: {
            'User-Agent': 'MyService',
            'Authorization': 'Bearer ' + token,
            'Content-type': 'application/octet-stream',
            'X-Goog-Upload-File-Name': 'Filename.jpg',
            'X-Goog-Upload-Protocol': 'raw'
        }
    }, function(error, uploadResponse, uploadToken) {
        if(error) {
            response.end(error);
        } else {
            request.post('https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate', {
                headers: {
                    'User-Agent': 'MyService',
                    'Authorization': 'Bearer ' + token,
                    'Content-type': 'application/json'
                },
                json: {
                    "albumId": "abcdef...",
                    "newMediaItems": [
                        {
                            "description": "Haustür X",
                            "simpleMediaItem": {
                                "uploadToken": uploadToken
                            }
                        }
                    ],
                    "albumPosition": {
                        "position": "FIRST_IN_ALBUM"
                    }
                }
            }, (error, addToAlbumResponse, addToAlbumBody) => {
                if(error) {
                    response.end(error);
                } else {
                    response.end(JSON.stringify(addToAlbumBody));
                }
            });
        }
    }));

const exifEnricher = new Transform({
    transform(chunk, encoding, callback) {
        if(chunk[0] === 255 && chunk[1] === 216) {
            const data = chunk.toString("binary");
            const exifObj = piexif.load(data);
            exifObj["GPS"][piexif.GPSIFD.GPSVersionID] = [2, 2, 0, 0];
            exifObj["GPS"][piexif.GPSIFD.GPSLatitudeRef] = "N";
            exifObj["GPS"][piexif.GPSIFD.GPSLatitude] = [[lat_degree, 1], [lat_minutes, 1], [lat_seconds_x_10, 10]];
            exifObj["GPS"][piexif.GPSIFD.GPSLongitudeRef] = "E";
            exifObj["GPS"][piexif.GPSIFD.GPSLongitude] = [[lon_degree, 1], [lon_minutes, 1], [lon_seconds_x_10, 10]];
            exifObj["GPS"][piexif.GPSIFD.GPSAltitudeRef] = 0;
            exifObj["GPS"][piexif.GPSIFD.GPSAltitude] = [alt_in_cm, 100];
            exifObj["0th"] = exifObj["0th"] || {};
            exifObj["0th"][piexif.ImageIFD.Software] = "MyService";
            exifObj["0th"][piexif.ImageIFD.Make] = "Brand";
            exifObj["0th"][piexif.ImageIFD.Model] = "Device Name";
            exifObj["0th"][piexif.ImageIFD.DateTime] = dateFormat(new Date(), "yyyy:mm:dd HH:mm:ss");
            const exifbytes = piexif.dump(exifObj);
            const newData = piexif.insert(exifbytes, data);
            callback(null, Buffer.from(newData, 'binary'))
        } else {
            callback(null, chunk)
        }
    }
});