1 /** |
|
2 * Relational pipes |
|
3 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, version 3 of the License. |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 */ |
|
17 |
|
18 #include <unistd.h> |
|
19 |
|
20 #include <Magick++.h> |
|
21 #include <zbar.h> |
|
22 |
|
23 #include <relpipe/xmlwriter/XMLWriter.h> |
|
24 |
|
25 #include "streamlet-common.h" |
|
26 |
|
27 /** |
|
28 * This streamlet extracts QR codes from image files. |
|
29 * |
|
30 * It provides three attributes: |
|
31 * - qr: first QR code found (if any) |
|
32 * - qr_count: number of QR codes found |
|
33 * - qr_xml: XML containing all QR cpdes found an additional metadata |
|
34 * |
|
35 */ |
|
36 class QRStreamlet : public Streamlet { |
|
37 private: |
|
38 |
|
39 const wstring XMLNS = L"tag:globalcode.info,2018:qr:FIXME:final-xmlns"; // FIXME: correct xmlns URI |
|
40 |
|
41 class Point { |
|
42 public: |
|
43 int x; |
|
44 int y; |
|
45 }; |
|
46 |
|
47 class Symbol { |
|
48 public: |
|
49 int id; |
|
50 std::wstring value; |
|
51 std::wstring type; |
|
52 int x; |
|
53 int y; |
|
54 int width; |
|
55 int height; |
|
56 std::vector<Point> polygon; |
|
57 }; |
|
58 |
|
59 std::vector<Symbol> findSymbols(std::wstring fileName) { |
|
60 std::vector<Symbol> result; |
|
61 |
|
62 Magick::Image magick(toBytes(fileName)); |
|
63 int width = magick.columns(); |
|
64 int height = magick.rows(); |
|
65 Magick::Blob blob; |
|
66 magick.modifyImage(); |
|
67 magick.write(&blob, "GRAY", 8); |
|
68 const void *raw = blob.data(); |
|
69 |
|
70 zbar::Image image(width, height, "Y800", raw, width * height); |
|
71 zbar::ImageScanner scanner; |
|
72 |
|
73 scanner.scan(image); |
|
74 |
|
75 int id = 0; |
|
76 for (zbar::Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol, id++) { |
|
77 Symbol s; |
|
78 |
|
79 |
|
80 s.id = id; |
|
81 s.type = fromBytes(symbol->get_type_name()); |
|
82 s.value = fromBytes(symbol->get_data()); |
|
83 |
|
84 int minX = 0; |
|
85 int minY = 0; |
|
86 int maxX = 0; |
|
87 int maxY = 0; |
|
88 |
|
89 // TODO: return original polygon in XML |
|
90 for (int i = 0, locationSize = symbol->get_location_size(); i < locationSize; i++) { |
|
91 int x = symbol->get_location_x(i); |
|
92 int y = symbol->get_location_y(i); |
|
93 minX = minX ? std::min(minX, x) : x; |
|
94 minY = minY ? std::min(minY, y) : y; |
|
95 maxX = std::max(maxX, x); |
|
96 maxY = std::max(maxY, y); |
|
97 s.polygon.push_back({x, y}); |
|
98 } |
|
99 |
|
100 s.x = minX; |
|
101 s.y = minY; |
|
102 s.width = maxX - minX; |
|
103 s.height = maxY - minY; |
|
104 |
|
105 result.push_back(s); |
|
106 } |
|
107 |
|
108 |
|
109 |
|
110 return result; |
|
111 } |
|
112 |
|
113 protected: |
|
114 |
|
115 std::vector<AttributeMetadata> getOutputAttributesMetadata() override { |
|
116 std::vector<AttributeMetadata> oam; |
|
117 int i = 0; |
|
118 oam.push_back({getAlias(i++, L"qr"), STRING}); |
|
119 oam.push_back({getAlias(i++, L"qr_count"), INTEGER}); |
|
120 oam.push_back({getAlias(i++, L"qr_xml"), STRING}); |
|
121 return oam; |
|
122 } |
|
123 |
|
124 std::vector<OutputAttribute> getOutputAttributes() override { |
|
125 bool validInput = false; |
|
126 bool hasSymbols = false; |
|
127 std::wstring first; |
|
128 std::stringstream xml; |
|
129 |
|
130 std::vector<Symbol> symbols; |
|
131 try { |
|
132 symbols = findSymbols(getCurrentFile()); |
|
133 validInput = true; |
|
134 } catch (...) { |
|
135 // just ignore the errors; |
|
136 // the file is probably not an image or we do not have read permissions |
|
137 validInput = false; |
|
138 } |
|
139 |
|
140 for (Symbol s : symbols) { |
|
141 // TODO: match against pattern (if any) |
|
142 first = s.value; |
|
143 hasSymbols = true; |
|
144 break; |
|
145 } |
|
146 |
|
147 relpipe::xmlwriter::XMLWriter xmlWriter(xml); |
|
148 xmlWriter.writeStartElement(L"qr",{L"xmlns", XMLNS}); |
|
149 |
|
150 // TODO: common metadata |
|
151 // xmlWriter.writeStartElement(L"source"); |
|
152 // xmlWriter.writeTextElement(L"height",{}, std::to_wstring(...)); |
|
153 // xmlWriter.writeTextElement(L"width",{}, std::to_wstring(...)); |
|
154 // xmlWriter.writeEndElement(); |
|
155 |
|
156 for (Symbol s : symbols) { |
|
157 xmlWriter.writeStartElement(L"symbol"); |
|
158 xmlWriter.writeTextElement(L"value",{}, s.value); |
|
159 |
|
160 // TODO: well-designed XML schema |
|
161 // TODO: synchronize/share common XML parts with relpipe-in-qr |
|
162 xmlWriter.writeStartElement(L"rectangular-box"); |
|
163 xmlWriter.writeTextElement(L"x",{}, std::to_wstring(s.x)); |
|
164 xmlWriter.writeTextElement(L"y",{}, std::to_wstring(s.y)); |
|
165 xmlWriter.writeTextElement(L"height",{}, std::to_wstring(s.height)); |
|
166 xmlWriter.writeTextElement(L"width",{}, std::to_wstring(s.width)); |
|
167 xmlWriter.writeEndElement(); |
|
168 |
|
169 xmlWriter.writeStartElement(L"polygon"); |
|
170 for (Point p : s.polygon) xmlWriter.writeEmptyElement(L"point",{L"x", std::to_wstring(p.x), L"y", std::to_wstring(p.y)}); |
|
171 xmlWriter.writeEndElement(); |
|
172 |
|
173 xmlWriter.writeEndElement(); |
|
174 } |
|
175 |
|
176 xmlWriter.writeEndElement(); |
|
177 |
|
178 std::vector<OutputAttribute> oa; |
|
179 // TODO: report also validInput (distinguish it from hasSymbols) |
|
180 oa.push_back({first, !hasSymbols}); |
|
181 oa.push_back({std::to_wstring(symbols.size()), false}); |
|
182 oa.push_back({fromBytes(xml.str()), false}); |
|
183 return oa; |
|
184 } |
|
185 }; |
|
186 |
|
187 STREAMLET_RUN(QRStreamlet) |
|