-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo_code.js
94 lines (77 loc) · 4.65 KB
/
demo_code.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const vision = require('@google-cloud/vision');
const Jimp = require('jimp');
const jsQR = require('jsqr');
const fs = require('fs');
const imagePath = '';
const SectorPath = 'left_bottom_sector.jpg';
const CREDENTIALS = JSON.parse(JSON.stringify({
"type": "service_account",
"project_id": "jovial-hawk-404515",
"private_key_id": "baea310b5d076508c5a9e7807dfc023b71dadf36",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC/j6+PFMSuyZCO\nR7+3nyJ0T8ZJ7kNYJPqDKxLSWq1hZDqXlw1l1k/HrqV9J+ZOyRRjLCb2AGPOZJ5/\ncTLywqzmJOq8LGJk+V6iDIxexayd2aXjNks//GpqlIuenwiWbfVYTNXJkWsH85i5\nsNhV22FAHnUlg0i20nU2KH/GEpdxe5IUJfBZOs1i8ARLBfXTFAUubCNg2Mv37y+y\nvz0h7gHjSL6+R1g+S1uxNUQ2+xE3itnQllroDAtDtWr9OC7inX4PkCvmbA8Zq/VL\ncMBlsP0Syooqf03bOh73PdUaYDxCCzLunKj2hAhkK49DAa915wrvfOOHOHwVvIAl\nKhBnmNCDAgMBAAECggEAGqhEJgUd2bTzCNmzNDpQ8R6o2WDuvjVnJxm3ZM55kjQA\nc1fx5VoNgBwtq10CD7SrwaFDSriDKxWVeOan0GCG7v/0KnuCtcp+AM7+ObIIc272\nRsgncKAPrwU86LX42/YxTjhjB8nUuyaK+kxBzrDYt7OFWdMLK2XeKcs4/LdZm9D4\n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9hk=\n-----END PRIVATE KEY-----\n",
"client_email": "compserinstance-jovial-hawk-40@jovial-hawk-404515.iam.gserviceaccount.com",
"client_id": "108716351021941201299",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/compserinstance-jovial-hawk-40%40jovial-hawk-404515.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}
))
const CONFIG = {
credentials: {
private_key: CREDENTIALS.private_key,
client_email: CREDENTIALS.client_email
}
};
const client = new vision.ImageAnnotatorClient(CONFIG);
const jsonFilePath = 'image.json';
const detectTextAndSave = async (file_path) => {
try {
let [result] = await client.textDetection(file_path);
const detectedText = result.fullTextAnnotation.text;
// Read existing data from the JSON file
let jsonData = {};
try {
const existingData = fs.readFileSync(jsonFilePath, 'utf-8');
jsonData = JSON.parse(existingData);
} catch (error) {
// File doesn't exist or is empty
}
// Update data in memory
jsonData.detectedText = detectedText;
// Write the updated data back to the JSON file
fs.writeFileSync(jsonFilePath, JSON.stringify(jsonData, null, 2));
console.log('Text detected and saved to result.json:', detectedText);
} catch (error) {
console.error('Error:', error.message);
}
};
// Example usage
detectTextAndSave(imagePath);
async function scanQRCodeFromImage(imagePath) {
try {
// Read the image file using Jimp
const image = await Jimp.read(imagePath);
// Convert the image to raw pixel data
const { data, width, height } = image.bitmap;
// Decode the QR code using jsQR
const code = jsQR(data, width, height);
if (code) {
console.log('QR Code detected and decoded:');
console.log(code.data);
// Save the detected QR code to a JSON file
const jsonFilePath = 'qrResult.json';
const jsonData = {
detectedQRCode: code.data,
};
fs.writeFileSync(jsonFilePath, JSON.stringify(jsonData, null, 2));
console.log('Detected QR Code saved to qrResult.json');
} else {
console.log('No QR Code detected in the image.');
}
} catch (error) {
console.error('Error:', error.message);
}
}
scanQRCodeFromImage(SectorPath);