Skip to content

Commit 9860104

Browse files
committed
Making sure the DIVOC QR can be read
1 parent b2f366b commit 9860104

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

app/screens/QRReader.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import {importSHC} from '../utils/ImportSHC';
1111
import {importDCC} from '../utils/ImportDCC';
1212
import {importVDS} from '../utils/ImportVDS';
1313

14+
import {decode} from './QRDecoder/index.js'
15+
import JSZip from 'jszip';
16+
17+
1418
const screenHeight = Math.round(Dimensions.get('window').height);
1519

1620
function QRReader({ navigation }) {
@@ -34,8 +38,33 @@ function QRReader({ navigation }) {
3438
}
3539
}
3640

37-
const fromHexString = hexString =>
38-
new Uint8Array(hexString.substring(1).match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
41+
const fromHexQR = async hexString => {
42+
const byteArray = new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
43+
44+
// Manually decode the array
45+
const str0 = decode(byteArray, 28).text;
46+
47+
const byteArray2 = new Uint8Array(hexString.substring(1).match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
48+
49+
// invalid QRs have existed for the deployment in India. These two lines help solve it.
50+
const str1 = new TextDecoder().decode( byteArray2);
51+
const str2 = String.fromCharCode.apply(null, byteArray2);
52+
53+
try {
54+
await new JSZip().loadAsync(str0);
55+
return str0;
56+
} catch {
57+
try {
58+
await new JSZip().loadAsync(str1);
59+
return str1;
60+
} catch {
61+
return str2;
62+
}
63+
}
64+
}
65+
66+
const toHexString = binaryString =>
67+
Array.from(new TextEncoder().encode(binaryString)).map(c => c.toString(16).padStart(2, '0')).join('');
3968

4069
const onQRRead = async (e) => {
4170
console.log(e);
@@ -60,8 +89,8 @@ function QRReader({ navigation }) {
6089
return;
6190
}
6291

63-
let string = "PK"+String.fromCharCode.apply(null, fromHexString(e.rawData))
64-
await checkResult(await importDivoc(string));
92+
// Removing the first byte because it's the QR mode
93+
await checkResult(await importDivoc(await fromHexQR(e.rawData)));
6594
return;
6695
}
6796

0 commit comments

Comments
 (0)