Expo相机拍摄照片并使用ImageManipulator处理照片

3
我有一个类似于这样的代码片段:

takePicture = async function() {
        if (this.camera) {
            this.camera
                .takePictureAsync()
                .then(data => {
                    FileSystem.moveAsync({
                        from: data.uri,
                        to: `${FileSystem.documentDirectory}photos/Photo_${
                            this.state.photoId
                        }.jpg`
                    }).then(() => {
                        this.setState({
                            photoId: this.state.photoId + 1
                        });
                        Vibration.vibrate();
                    });
                });
        }
    };

现在我的问题是我不知道如何将ImageManipulator插入到这个函数中。我的目的是在takePictureAsync()之后,将照片调整大小为108x192,然后将此照片移动到documentDirectory。非常感谢。

1个回答

9
我已经找到了解决方案,这里是代码。
takePicture = async function() {
        if (this.camera) {
            let photo = await this.camera.takePictureAsync();
            let resizedPhoto = await ImageManipulator.manipulate(
                photo.uri,
                [{ resize: { width: 108, height: 192 } }],
                { compress: 0, format: "jpg", base64: false }
            );
            FileSystem.moveAsync({
                from: resizedPhoto.uri,
                to: `${FileSystem.documentDirectory}photos/Photo_${
                    this.state.photoId
                }.jpg`
            });
            this.setState({ photoId: this.state.photoId + 1 });
            Vibration.vibrate();            
        }
    };

如果我错了,请纠正我,但这将导致错误,photo.uri可能未定义。 - FabricioG
拍照后会返回一个照片对象,因此URI不能为空。 - LeonHeart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接