I am using the react-qr-reader component to build a qr scanner modal for my website.
The modal is created as expected and I can scan different qr codes but after dismissing the ion-modal the scanner keeps the users camera active but stops detecting qr codes.
How can I deactivate the users camera?
In my research I found the following question already asked. There the close function I am using is proposed with
ref.current.stopCamera()
at the end. Somehow I can not define ref because ref is not in "IntrinsicAttributes & QrReaderProps" of the QrReader component.
So, how can I reference the react component and is the close function with the added
ref.current.stopCamera()
the correct way of deactivation the camera?
Thanks for your help!
Here is my code:
export const ScannerModal: React.FC<ScannerProps> = ({ triggerID, handleScannedNode }) => {
const modal = useRef<HTMLIonModalElement>(null);
const scannerConstraints: MediaTrackConstraints = {
}
function onDissMiss(event: CustomEvent<OverlayEventDetail>){
console.log(event)
close()
}
function handleScan(result: string){
close()
handleScannedNode(result)
}
async function close(){
console.log("closing")
const stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: true,
});
stream.getTracks().forEach(function (track) {
track.stop();
track.enabled = false;
});
//ref.current.stopCamera()
}
return (
<>
<IonModal trigger={triggerID} onWillDismiss={onDissMiss} ref={modal}>
<IonTitle>Scanne einen QR-Code</IonTitle>
<IonPage>
<QrReader constraints={scannerConstraints} ref={}
onResult={(result, error) => {
if (!!result) {
handleScan(result?.getText());
}
if (!!error) {
//console.info(error);
}
}}/>
</IonPage>
</IonModal>
</>
);
};
I tried referencing the QrReader component via:
const reader = useRef(null);
<QrReader constraints={scannerConstraints} ref={reader}
onResult={(result, error) => {
if (!!result) {
handleScan(result?.getText());
}
if (!!error) {
//console.info(error);
}
}}/>
The error I received was:
Property 'ref' does not exist on type 'IntrinsicAttributes & QrReaderProps'.
1条答案
按热度按时间y1aodyip1#
对于每个人发现这个线程只想实现一个简单的React二维码扫描仪:
我使用这个qr-scanner project找到了一个工作解决方案。
我构建组件如下: