2

我正在使用 expo-camera 库拍摄自拍图像。预览是镜像的,但保存的图像会恢复到正常方向。如何防止这种效果发生(我希望图像保持镜像),或者如果我不能,如何水平翻转图像?

以下是我目前拍摄的照片:

const takePic = async () => {
  if (cameraRef) {
    const photo = await cameraRef.current.takePictureAsync();
    setFrontProfile(photo.uri);
  }
};
4

2 回答 2

3

使用ImageManipulator翻转它。

于 2021-05-01T18:09:21.393 回答
1

我知道@Kartikey 已经回答了这个问题,但我想直接回答作者的问题。

@Kartikey 给出的链接中的示例将旋转设置为 90 而不是 180。我的答案是在应用操作之前检查照片是否来自前置摄像头,并且正好适合给定的代码。

import { manipulateAsync, FlipType, SaveFormat } from 'expo-image-manipulator';

...

const takePic = async () => {
    if (!cameraRef) return;
    
    let photo = await cameraRef.takePictureAsync();

    if (cameraType === Camera.Constants.Type.front) {
        photo = await manipulateAsync(
            photo.localUri || photo.uri,
            [
                { rotate: 180 },
                { flip: FlipType.Vertical },
            ],
            { compress: 1, format: SaveFormat.PNG }
        );
    }

    setFrontProfile(photo.uri);
};
于 2021-11-17T23:55:08.327 回答