问题
The Camera preview component from the npm package 'expo-camera' doesn't show anything but a black screen. I've even created a brand new managed Expo project with nothing but this code:
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Camera } from 'expo-camera';
export default function App() {
return (
<View style={styles.container}>
<Camera />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
This is my package.json file:
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject"
},
"dependencies": {
"expo": "^35.0.0",
"expo-camera": "^7.0.0",
"react": "16.8.3",
"react-dom": "16.8.3",
"react-native": "https://github.com/expo/react-native/archive/sdk-35.0.0.tar.gz",
"react-native-web": "^0.11.7"
},
"devDependencies": {
"babel-preset-expo": "^7.1.0"
},
"private": true
}
And this is my app.json:
{
"expo": {
"name": "testcamera",
"slug": "testcamera",
"privacy": "public",
"sdkVersion": "35.0.0",
"platforms": [
"ios",
"android",
"web"
],
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"updates": {
"fallbackToCacheTimeout": 0
},
"assetBundlePatterns": [
"**/*"
],
"ios": {
"supportsTablet": true
}
}
}
It just stopped working out of the blue. Last thing I did was publish the Expo project, and it was working before that.
回答1:
Working example with hook and functional component, request permissions:
https://snack.expo.io/@djalik/camera
回答2:
// this is in the constructor
props.navigation.addListener('focus', () => {
console.log("hey man you are looking to me");
this.setState({loaded:true});
});
props.navigation.addListener('blur', () => {
console.log("hey man where are you going?");
this.setState({loaded:false});
});
//this is in the render
{
this.state.loaded &&
<Camera
style={ styles.camera }
type={this.state.camType}
ratio={ " "+CameraRatio[0]+":"+CameraRatio[1] +" "}
ref={ref => {
this.camera = ref;
}}
/>
}
来源:https://stackoverflow.com/questions/58575757/expo-camera-preview-component-started-displaying-black-screen-out-of-the-blue