-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_food.js
100 lines (83 loc) · 3.52 KB
/
add_food.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
95
96
97
98
99
100
document.addEventListener('DOMContentLoaded', function() {
const foodForm = document.getElementById('foodForm');
const foodNameInput = document.getElementById('foodName');
const expiryDateInput = document.getElementById('expiryDate');
const startScanButton = document.getElementById('startScanButton');
const barcodeScanner = document.getElementById('barcodeScanner');
const captureButton = document.getElementById('captureButton');
const messageBox = document.getElementById('messageBox');
const closeButton = document.getElementById('closeButton');
foodForm.addEventListener('submit', function(event) {
event.preventDefault();
const foodName = foodNameInput.value;
const expiryDate = expiryDateInput.value;
fetch('http://127.0.0.1:5000/api/add_food', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ foodName: foodName, expiryDate: expiryDate })
})
.then(response => response.json())
.then(data => {
// メッセージボックスを表示する
messageBox.style.display = 'block';
})
.catch(error => {
console.error('Error occurred while adding food:', error);
alert('エラーが発生しました。もう一度試してください。');
});
});
closeButton.addEventListener('click', () => {
// メッセージボックスを閉じる
messageBox.style.display = 'none';
});
startScanButton.addEventListener('click', () => {
// カメラ映像を表示する
barcodeScanner.style.display = 'block';
captureButton.style.display = 'block';
startScanButton.style.display = 'none';
// QuaggaJSを使ってバーコードをスキャンする
Quagga.init({
inputStream: {
name: 'Live',
type: 'LiveStream',
target: barcodeScanner
},
decoder: {
readers: ['ean_reader']
}
}, (err) => {
if (err) {
console.error(err);
return;
}
Quagga.start();
});
Quagga.onDetected((data) => {
const code = data.codeResult.code;
console.log(`バーコード: ${code}`);
// バーコードから食品名を取得する処理を追加
fetch(`https://world.openfoodfacts.org/api/v0/product/${code}.json`)
.then(response => response.json())
.then(data => {
if (data.status === 1) {
let product = data.product;
foodNameInput.value = product.product_name || `食品名(${code})`;
} else {
alert('商品情報が見つかりませんでした。');
}
})
.catch(error => {
console.error('Error occurred while fetching product info:', error);
alert('商品情報の取得中にエラーが発生しました。');
});
});
});
captureButton.addEventListener('click', () => {
Quagga.stop();
barcodeScanner.style.display = 'none';
captureButton.style.display = 'none';
startScanButton.style.display = 'block';
});
});