|
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 two attributes: |
|
31 * - qr: first QR code found (if any) |
|
32 * - qr_xml: XML containing all QR cpdes found an additional metadata |
|
33 * |
|
34 */ |
|
35 class QRStreamlet : public Streamlet { |
|
36 private: |
|
37 |
|
38 const wstring XMLNS = L"tag:globalcode.info,2018:qr:FIXME:final-xmlns"; // FIXME: correct xmlns URI |
|
39 |
|
40 class Symbol { |
|
41 public: |
|
42 int id; |
|
43 std::wstring value; |
|
44 std::wstring type; |
|
45 int x; |
|
46 int y; |
|
47 int width; |
|
48 int height; |
|
49 }; |
|
50 |
|
51 std::vector<Symbol> findSymbols(std::wstring fileName) { |
|
52 std::vector<Symbol> result; |
|
53 |
|
54 Magick::Image magick(toBytes(fileName)); |
|
55 int width = magick.columns(); |
|
56 int height = magick.rows(); |
|
57 Magick::Blob blob; |
|
58 magick.modifyImage(); |
|
59 magick.write(&blob, "GRAY", 8); |
|
60 const void *raw = blob.data(); |
|
61 |
|
62 zbar::Image image(width, height, "Y800", raw, width * height); |
|
63 zbar::ImageScanner scanner; |
|
64 |
|
65 scanner.scan(image); |
|
66 |
|
67 int id = 0; |
|
68 for (zbar::Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol, id++) { |
|
69 Symbol s; |
|
70 |
|
71 |
|
72 s.id = id; |
|
73 s.type = fromBytes(symbol->get_type_name()); |
|
74 s.value = fromBytes(symbol->get_data()); |
|
75 |
|
76 int minX = 0; |
|
77 int minY = 0; |
|
78 int maxX = 0; |
|
79 int maxY = 0; |
|
80 |
|
81 // TODO: return original polygon in XML |
|
82 for (int i = 0, locationSize = symbol->get_location_size(); i < locationSize; i++) { |
|
83 int x = symbol->get_location_x(i); |
|
84 int y = symbol->get_location_y(i); |
|
85 minX = minX ? std::min(minX, x) : x; |
|
86 minY = minY ? std::min(minY, y) : y; |
|
87 maxX = std::max(maxX, x); |
|
88 maxY = std::max(maxY, y); |
|
89 } |
|
90 |
|
91 s.x = minX; |
|
92 s.y = minY; |
|
93 s.width = maxX - minX; |
|
94 s.height = maxY - minY; |
|
95 |
|
96 result.push_back(s); |
|
97 } |
|
98 |
|
99 |
|
100 |
|
101 return result; |
|
102 } |
|
103 |
|
104 protected: |
|
105 |
|
106 std::vector<AttributeMetadata> getOutputAttributesMetadata() override { |
|
107 std::vector<AttributeMetadata> oam; |
|
108 int i = 0; |
|
109 oam.push_back({getAlias(i++, L"qr"), STRING}); |
|
110 oam.push_back({getAlias(i++, L"qr_xml"), STRING}); |
|
111 return oam; |
|
112 } |
|
113 |
|
114 std::vector<OutputAttribute> getOutputAttributes() override { |
|
115 bool validInput = false; |
|
116 bool hasSymbols = false; |
|
117 std::wstring first; |
|
118 std::stringstream xml; |
|
119 |
|
120 std::vector<Symbol> symbols; |
|
121 try { |
|
122 symbols = findSymbols(getCurrentFile()); |
|
123 validInput = true; |
|
124 } catch (...) { |
|
125 // just ignore the errors; |
|
126 // the file is probably not an image or we do not have read permissions |
|
127 validInput = false; |
|
128 } |
|
129 |
|
130 for (Symbol s : symbols) { |
|
131 // TODO: match against pattern (if any) |
|
132 first = s.value; |
|
133 hasSymbols = true; |
|
134 break; |
|
135 } |
|
136 |
|
137 relpipe::xmlwriter::XMLWriter xmlWriter(xml); |
|
138 xmlWriter.writeStartElement(L"qr",{L"xmlns", XMLNS}); |
|
139 |
|
140 // TODO: common metadata |
|
141 // xmlWriter.writeStartElement(L"source"); |
|
142 // xmlWriter.writeTextElement(L"height",{}, std::to_wstring(...)); |
|
143 // xmlWriter.writeTextElement(L"width",{}, std::to_wstring(...)); |
|
144 // xmlWriter.writeEndElement(); |
|
145 |
|
146 for (Symbol s : symbols) { |
|
147 xmlWriter.writeStartElement(L"symbol"); |
|
148 xmlWriter.writeTextElement(L"value",{}, s.value); |
|
149 xmlWriter.writeTextElement(L"x",{}, std::to_wstring(s.x)); |
|
150 xmlWriter.writeTextElement(L"y",{}, std::to_wstring(s.y)); |
|
151 xmlWriter.writeTextElement(L"height",{}, std::to_wstring(s.height)); |
|
152 xmlWriter.writeTextElement(L"width",{}, std::to_wstring(s.width)); |
|
153 // TODO: polygon |
|
154 xmlWriter.writeEndElement(); |
|
155 } |
|
156 |
|
157 xmlWriter.writeEndElement(); |
|
158 |
|
159 std::vector<OutputAttribute> oa; |
|
160 // TODO: report also validInput (distinguish it from hasSymbols) |
|
161 oa.push_back({first, !hasSymbols}); |
|
162 oa.push_back({fromBytes(xml.str()), false}); |
|
163 return oa; |
|
164 } |
|
165 }; |
|
166 |
|
167 STREAMLET_RUN(QRStreamlet) |