简体   繁体   English

如何使用 expo React Native 相机拍照?

[英]How to take a pic using expo react native camera?

I have just started using React Native with Expo so I am kind of confused.我刚刚开始将 React Native 与 Expo 一起使用,所以我有点困惑。 So, I have made a camera component which I imported in the main screen.所以,我制作了一个我在主屏幕中导入的相机组件。 Everything looks good.一切看起来都很好。 But I can't take pictures.但是我不能拍照。 I cannot click the snap icon and save the image.我无法单击快照图标并保存图像。 Is there a component that I missed?有没有我错过的组件? I have only posted the CameraComponent function below.我只在下面发布了 CameraComponent function。

CameraScreen.js CameraScreen.js

const CameraScreen = () => {


    const [hasPermission, setHasPermission] = useState(null);
    const [type, setType] = useState(Camera.Constants.Type.back);

    useEffect(() => {
        (async () => {
            const { status } = await Camera.requestCameraPermissionsAsync();
            setHasPermission(status === 'granted');
        })();
    }, []);

    if (hasPermission === null) {
        return <View />;
    }
    if (hasPermission === false) {
        return <Text>No access to camera</Text>;
    }

    const takePicture = () => {
        if (Camera) {
            Camera.takePictureAsync({ onPictureSaved: onPictureSaved });
        }
    };

    const onPictureSaved = photo => {
        console.log(photo);
    }

    return (

        <View style={styles.container}>
            <Camera style={styles.camera}
                type={type}
                ref={(r) => {
                    camera = r
                }}>
                <View style={styles.buttonContainer}>
                    <View style={styles.flip}>
                        <TouchableOpacity
                            style={styles.button}
                            onPress={() => {
                                setType(
                                    type === Camera.Constants.Type.back
                                        ? Camera.Constants.Type.front
                                        : Camera.Constants.Type.back
                                );
                            }}
                        >
                            <Text style={styles.text}> Flip </Text>
                        </TouchableOpacity>
                    </View>

                    <View style={styles.Shot}>
                        <TouchableOpacity style={styles.button} onPress={takePicture}>
                            <Text style={styles.text}> Shot </Text>
                        </TouchableOpacity>
                    </View>
                </View>
            </Camera>
        </View>
    );
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
    },
    camera: {
        flex: 1,
        justifyContent: 'flex-end',
        paddingBottom: 25,
        paddingLeft: 10,
        paddingRight: 10
    },
    flip:{ 
    },
    Shot:{
    },
    buttonContainer: {
        flexDirection: 'row',
        backgroundColor: 'transparent',
        justifyContent: 'space-between',
        alignItems: 'flex-end',
    },
    text: {
        fontSize: 25,
        color: 'white',
    },
});

export default CameraScreen

The button(Shot) in the bottom should automatically take and save image.底部的按钮(拍摄)应自动拍摄并保存图像。

Instead of代替

Camera.takePictureAsync({ onPictureSaved: onPictureSaved });

A callback invoked when picture is saved.保存图片时调用的回调。 If set, the promise of this method will resolve immediately with no data after picture is captured.如果设置,该方法的promise在抓图后会立即解析为无数据。 The data that it should contain will be passed to this callback.它应包含的数据将传递给此回调。 If displaying or processing a captured photo right after taking it is not your case, this callback lets you skip waiting for it to be saved.如果您不希望在拍摄后立即显示或处理拍摄的照片,则此回调可让您跳过等待照片保存的过程。

You should use the async promise from takePictureAsync function您应该使用 takePictureAsync function 中的异步takePictureAsync

Camera.takePictureAsync()
    .then(onPictureSaved);


//your onPictureSaved function
const onPictureSaved = ({ uri, width, height, exif, base64 }) => {
    console.log(uri);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM