Similiar cases here:
Hi I’m trying to do a QR code scanner on expo for a web app. It’s working perfectly on IOS and android, but when I’m trying on web, the camera show up but it’s does not detect any QR code.
opened 01:35AM - 14 May 21 UTC
closed 12:11AM - 03 Feb 22 UTC
Platform: web
BarCodeScanner
Camera
stale
needs validation
### Summary
When building the web project. It is not possible to read QR Codes … in browsers.
### Managed or bare workflow? If you have `ios/` or `android/` directories in your project, the answer is bare!
managed
### What platform(s) does this occur on?
Web
### SDK Version (managed workflow only)
41.0.1
### Environment
```
Expo CLI 4.4.6 environment info:
System:
OS: macOS 11.2.3
Shell: 5.8 - /bin/zsh
Binaries:
Node: 15.11.0 - ~/.nvm/versions/node/v15.11.0/bin/node
Yarn: 1.22.10 - ~/.nvm/versions/node/v15.11.0/bin/yarn
npm: 7.6.0 - ~/.nvm/versions/node/v15.11.0/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.10.1 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: iOS 14.5, DriverKit 20.4, macOS 11.3, tvOS 14.5, watchOS 7.4
IDEs:
Android Studio: 4.1 AI-201.8743.12.41.7042882
Xcode: 12.5/12E5244e - /usr/bin/xcodebuild
npmPackages:
expo: ~41.0.1 => 41.0.1
react: 16.13.1 => 16.13.1
react-dom: 16.13.1 => 16.13.1
react-native: https://github.com/expo/react-native/archive/sdk-41.0.0.tar.gz => 0.63.2
react-native-web: ~0.13.12 => 0.13.18
npmGlobalPackages:
expo-cli: 4.4.6
Expo Workflow: managed
```
### Reproducible demo or steps to reproduce from a blank project
```
import React, { useState, useEffect, useContext } from 'react';
import { useNavigation } from '@react-navigation/core';
import { TouchableOpacity, Dimensions } from 'react-native';
import Icon from 'react-native-vector-icons/Feather';
import Toast from 'react-native-toast-message';
import { BarCodeScanningResult, Camera } from 'expo-camera';
import { BarCodeScanner } from 'expo-barcode-scanner';
import { Loading } from '@views/components/Loading';
import { Text } from '@views/components/Text';
import { QRCodeType } from '@domain/entities/scanner.entity';
import { QRCodeAdapter } from '@data/adapters/qrCode.adapter';
import { VinculateEstablishment } from '@data/usecases/vinculateEstablishment.usecase';
import { ThemeContext } from 'styled-components/native';
import { Container, CameraContent } from './styles';
import { Alert } from 'react-native';
export const Scanner = (): JSX.Element => {
const navigation = useNavigation();
const { colors } = useContext(ThemeContext);
const [loading, setLoading] = useState(true);
const [hasPermission, setHasPermission] = useState(false);
useEffect(() => {
(async () => {
const { status } = await Camera.requestPermissionsAsync();
if (status === 'granted') setHasPermission(true); setLoading(false);
})();
}, []);
const handleCodeScanned = async ({ data }: BarCodeScanningResult) => {
// It doesn't arrive here in the browser
setLoading(true);
Toast.show({
type: "error",
text1: 'Ops!',
});
// const { type, url } = QRCodeAdapter(JSON.parse(data));
// if (type === QRCodeType.VALIDATE) navigation.navigate('ConfirmPin', { url, type })
// if (type === QRCodeType.VINCULATE) {
// await VinculateEstablishment(url);
// navigation.reset({ index: 0, routes: [{ name: 'Home' }] });
// }
// if (type === QRCodeType.WITHDRAW) navigation.navigate('ConfirmPin', { url, type })
};
if (loading) return <Loading />;
if (!hasPermission) return <Text fontSize={16} color={colors.light} >Sem acesso a câmera</Text>;
return (
<Container>
<Camera
autoFocus={Camera.Constants.AutoFocus.on}
style={{ flex: 1 }}
type={Camera.Constants.Type.back}
ratio="16:9"
onBarCodeScanned={(scanningResult: BarCodeScanningResult) => handleCodeScanned(scanningResult)}
barCodeScannerSettings={{ barCodeTypes: [BarCodeScanner.Constants.BarCodeType.qr] }}
>
<CameraContent>
<Text fontSize={24} color={colors.light} fontWeight="bold">Leia o QR Code</Text>
<Icon name="maximize" size={Dimensions.get('window').width / 1.1} color={colors.primary} />
<TouchableOpacity onPress={() => navigation.navigate('Home')} style={{ padding: 16 }} >
<Text fontSize={16} color={colors.light}> Cancelar </Text>
</TouchableOpacity>
</CameraContent>
</Camera>
</Container>
);
};
```
I changed /node_modules/expo-camera/package.json, set @koale/useworker
to 4.0.1. Then I ran npm i
under react native project root dir. After that I checked @koale/useworker
in node_modules, and it was 4.0.1.
However, it still did not detect QR code on web.
I use Expo 42 and expo-camera 11.2.2
My package.json:
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"deploy": "gh-pages -d web-build",
"predeploy": "expo build:web --no-pwa"
},
"dependencies": {
"expo": "~42.0.1",
"expo-camera": "11.2.2",
"expo-status-bar": "~1.0.4",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-42.0.0.tar.gz",
"react-native-web": "~0.13.12",
"@koale/useworker": "4.0.1"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@types/react": "~16.9.35",
"@types/react-native": "~0.63.2",
"gh-pages": "^3.2.3",
"typescript": "~4.0.0"
},
"private": true,
"homepage": "https://uoynixnah.github.io/react-native-test/"
}
My App.tsx:
import { StatusBar } from 'expo-status-bar';
import React, {useState, useEffect} from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import { Camera } from 'expo-camera';
export default function App() {
const [hasPermission, setHasPermission] = useState(false);
const [type, setType] = useState(Camera.Constants.Type.back);
const [scanned, setScanned] = useState(false);
const handleScanned = ({type, data}) => {
setScanned(true);
alert(`Get: ${data}`);
}
useEffect(() => {
(async () => {
const { status } = await Camera.requestPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View style={styles.container}>
<Camera
style={styles.camera}
type={type}
onBarCodeScanned={scanned ? undefined : handleScanned}
>
<Text>0.0.7</Text>
</Camera>
{scanned && <Button title={'Try again'} onPress={()=>{setScanned(false)}} />}
{!scanned && <Button title={'waiting...'} onPress={()=>alert("waiting...")} />}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1
},
camera: {
flex: 1
}
});
Any advice would be appreciated.
I have solved the problem. For scanning QR code, barCodeScannerSetting
shoule be set.
barCodeScannerSettings={{
barCodeTypes: [BarCodeScanner.Constants.BarCodeType.qr],
}}
Sorry for such a beginner’s question.
1 Like
system
Closed
October 4, 2021, 11:35pm
3
This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.