如何在 React 组件中使用 exif-js?

zen*_*god 3 javascript reactjs exif-js

我正在尝试获取在 React 中使用输入标签上传的图像的 exif 元数据,并将以下函数附加到输入标签的属性“onChange”:

    onChange(e) {
        const { input: { onChange } } = this.props;
        let img_arr = [];
        for (let i = 0; i < e.target.files.length; i++) {
            const file = e.target.files[i];
            console.log('exif data');
            console.log(EXIF.getData(file, () => {
                var orientation = EXIF.getTag(this, "Orientation");
                console.log('orientation');
                console.log(orientation);
            }));
            img_arr.push({file: file, url: URL.createObjectURL(file)});
        }
        //console.log("printing img arr");
        //console.log(img_arr);
        onChange(img_arr);
    }

Run Code Online (Sandbox Code Playgroud)

但是,我得到 EXIF 未定义。在exif-js页面,建议使用window.onload。https://github.com/exif-js/exif-js。如何在 React 组件中使用 window.onload ?

- 编辑 -

我将代码更改为这样,但方向仍然未定义:

    onChange(e) {
        const { input: { onChange } } = this.props;
        let img_arr = [];
        for (let i = 0; i < e.target.files.length; i++) {
            const file = e.target.files[i];
            console.log('exif data');
            EXIF.getData(file, function() {
                const url = URL.createObjectURL(file);
                const image = new Image();
                image.onload = function() {
                    URL.revokeObjectURL(url);
                    const orientation = EXIF.getTag(this, 'Orientation');
                    console.log(orientation);
                };
                image.src = url;
            });
            img_arr.push({file: file, url: URL.createObjectURL(file)});
        }
        onChange(img_arr);
    }

Run Code Online (Sandbox Code Playgroud)

Joh*_*ala 7

exif-js提供了 CommonJS 的等价物。如果 EXIF 没有定义,你只需要导入它:

import EXIF from 'exif-js'
Run Code Online (Sandbox Code Playgroud)

示例:codesandbox

要验证图像的 EXIF 数据,您可以检查网站。

您可以使用此图像进行测试:http://fredericiana.com/media/2013/monalisa.jpg