+ )
}
\ No newline at end of file
diff --git a/components/Form.js b/components/Form.js
index 557575f..2ae7992 100644
--- a/components/Form.js
+++ b/components/Form.js
@@ -1,192 +1,304 @@
-import jsQR from "jsqr"
import {saveAs} from 'file-saver'
-
+import {BrowserQRCodeReader} from '@zxing/browser'
+import React, {useEffect, useRef, useState} from "react"
import {decodeData} from "../src/decode"
-import { processJpeg, processPng, processPdf } from "../src/process"
+import {processPdf, processPng} from "../src/process"
import {createPass} from "../src/pass"
import Card from "../components/Card"
import Alert from "../components/Alert"
+import jsQR from "jsqr";
export default Form
function Form() {
- function readFileAsync(file) {
- return new Promise((resolve, reject) => {
- let reader = new FileReader();
-
- reader.onload = () => {
- resolve(reader.result);
- };
-
- reader.onerror = reject;
-
- reader.readAsArrayBuffer(file);
- })
- }
+ function readFileAsync(file) {
+ return new Promise((resolve, reject) => {
+ let reader = new FileReader();
- const error = function(heading, message) {
- const alert = document.getElementById('alert')
- alert.setAttribute('style', null)
-
- document.getElementById('heading').innerHTML = heading
- document.getElementById('message').innerHTML = message
+ reader.onload = () => {
+ resolve(reader.result);
+ };
- document.getElementById('spin').style.display = 'none'
- }
+ reader.onerror = reject;
- const processFile = async function() {
- document.getElementById('spin').style.display = 'block'
-
- const file = document.getElementById('file').files[0]
- const fileBuffer = await readFileAsync(file)
-
- let imageData
-
- switch (file.type) {
- case 'application/pdf':
- console.log('pdf')
- imageData = await processPdf(fileBuffer)
- break
- case 'image/jpeg':
- console.log('jpeg')
- imageData = await processJpeg(fileBuffer)
- break
- case 'image/png':
- console.log('png')
- imageData = await processPng(fileBuffer)
- break
- default:
- error('Error', 'Invalid file type')
- return
+ reader.readAsArrayBuffer(file);
+ })
}
- let code = jsQR(imageData.data, imageData.width, imageData.height, {
- inversionAttempts: 'dontInvert',
- })
+ const error = function (heading, message) {
+ const alert = document.getElementById('alert')
+ alert.setAttribute('style', null)
- if (code) {
- const rawData = code.data
- let decoded
+ document.getElementById('heading').innerHTML = heading
+ document.getElementById('message').innerHTML = message
- try {
- decoded = decodeData(rawData)
- } catch (error) {
- error('Invalid QR code found', 'Make sure that you picked the correct PDF')
- }
-
- return {decoded: decoded, raw: rawData}
- } else {
- error('No QR code found', 'Try scanning the PDF again')
- }
- }
-
- const addToWallet = async function(event) {
- event.preventDefault()
-
- let result
-
- try {
- result = await processFile()
- } catch {
- error('Error:', 'Could not extract QR code data from PDF')
+ document.getElementById('spin').style.display = 'none'
}
- if (typeof result === 'undefined') {
- return
- }
-
- const color = document.getElementById('color').value
-
- try {
- const pass = await createPass(
- {
- decoded: result.decoded,
- raw: result.raw,
- color: color
+ const processFile = async function () {
+ console.log(qrCode)
+ console.log(file)
+ if (!qrCode && !file) {
+ error("Error", "Please capture a QR Code or select a file to scan");
+ return;
}
- )
-
- if (!pass) {
- error('Error:', "Something went wrong.")
- } else {
- const passBlob = new Blob([pass], {type: "application/vnd.apple.pkpass"});
- saveAs(passBlob, 'covid.pkpass')
- }
- } catch (e) {
- error('Error:', e.message)
- } finally {
- document.getElementById('spin').style.display = 'none'
- }
- }
- return (
-
-
- } />
-
-
- Pick a background color for your pass.
-
-
-
-
-
-
-
-
- } />
-
-
- Data privacy is of special importance when processing health-related data.
- In order for you to make an informed decision, please read the Privacy Policy.
-
-
-
-
-
-
-
-
-
- } />
-
-
-
-
- )
+ document.getElementById('spin').style.display = 'block'
+
+ let rawData;
+
+ if (file) {
+ let imageData
+ const fileBuffer = await readFileAsync(file)
+
+ switch (file.type) {
+ case 'application/pdf':
+ console.log('pdf')
+ imageData = await processPdf(fileBuffer)
+ break
+ case 'image/png':
+ console.log('png')
+ imageData = await processPng(fileBuffer)
+ break
+ default:
+ error('Error', 'Invalid file type')
+ return
+ }
+
+ let code = jsQR(imageData.data, imageData.width, imageData.height, {
+ inversionAttempts: 'dontInvert',
+ })
+
+ rawData = code.data;
+
+ } else {
+ rawData = qrCode.getText()
+ }
+
+ if (rawData) {
+ let decoded
+
+ try {
+ decoded = decodeData(rawData)
+ } catch (e) {
+ error('Invalid QR code found', 'Try another method to select your certificate')
+ return;
+ }
+
+ return {decoded: decoded, raw: rawData}
+ } else {
+ error('No QR code found', 'Try another method to select your certificate')
+ }
+ }
+
+ const addToWallet = async function (event) {
+ event.preventDefault()
+
+ let result
+
+ try {
+ result = await processFile()
+ } catch (e) {
+ error('Error:', 'Could not extract QR code data from certificate')
+ }
+
+ if (typeof result === 'undefined') {
+ return
+ }
+
+ const color = document.getElementById('color').value
+
+ try {
+ const pass = await createPass(
+ {
+ decoded: result.decoded,
+ raw: result.raw,
+ color: color
+ }
+ )
+
+ if (!pass) {
+ error('Error:', "Something went wrong.")
+ } else {
+ const passBlob = new Blob([pass], {type: "application/vnd.apple.pkpass"});
+ saveAs(passBlob, 'covid.pkpass')
+ }
+ } catch (e) {
+ error('Error:', e.message)
+ } finally {
+ document.getElementById('spin').style.display = 'none'
+ }
+ }
+
+ const [isCameraOpen, setIsCameraOpen] = useState(false);
+ const [globalControls, setGlobalControls] = useState(undefined);
+ const [qrCode, setQrCode] = useState(undefined);
+ const [file, setFile] = useState(undefined);
+
+ const inputFile = useRef(undefined)
+
+ useEffect(() => {
+ if (inputFile && inputFile.current) {
+ inputFile.current.addEventListener('input', () => {
+ let selectedFile = inputFile.current.files[0];
+ if (selectedFile !== undefined) {
+ setQrCode(undefined);
+ setFile(selectedFile);
+ }
+ });
+ }
+ }, [inputFile])
+
+ async function showFileDialog() {
+ inputFile.current.click();
+
+ }
+
+
+ async function hideCameraView() {
+ if (globalControls !== undefined) {
+ globalControls.stop();
+ }
+ setIsCameraOpen(false);
+ }
+
+ async function showCameraView() {
+ const codeReader = new BrowserQRCodeReader();
+
+ // Needs to be called before any camera can be accessed
+ await BrowserQRCodeReader.listVideoInputDevices();
+
+ // Get preview Element to show camera stream
+ const previewElem = document.querySelector('#cameraPreview');
+
+ setGlobalControls(await codeReader.decodeFromVideoDevice(undefined, previewElem, (result, error, controls) => {
+
+ if (result !== undefined) {
+ setQrCode(result);
+ setFile(undefined);
+
+ controls.stop();
+
+ // Reset
+ setGlobalControls(undefined);
+ setIsCameraOpen(false);
+ }
+ }));
+
+ setIsCameraOpen(true);
+ }
+
+ return (
+
+
+ }/>
+
+
+ Pick a background color for your pass.
+
+
+
+
+
+
+
+
+ }/>
+
+
+ Data privacy is of special importance when processing health-related data.
+ In order for you to make an informed decision, please read the Privacy
+ Policy.
+