streamlet-examples/qr-decode.cpp
branchv_0
changeset 89 25a11859975b
parent 88 8eb799bf1d39
child 90 59d12eee189f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/streamlet-examples/qr-decode.cpp	Sun Apr 25 18:47:57 2021 +0200
@@ -0,0 +1,187 @@
+/**
+ * Relational pipes
+ * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <unistd.h>
+
+#include <Magick++.h>
+#include <zbar.h>
+
+#include <relpipe/xmlwriter/XMLWriter.h>
+
+#include "streamlet-common.h"
+
+/**
+ * This streamlet extracts QR codes from image files.
+ * 
+ * It provides three attributes:
+ *  - qr: first QR code found (if any)
+ *  - qr_count: number of QR codes found
+ *  - qr_xml: XML containing all QR cpdes found an additional metadata
+ *	
+ */
+class QRStreamlet : public Streamlet {
+private:
+
+	const wstring XMLNS = L"tag:globalcode.info,2018:qr:FIXME:final-xmlns"; // FIXME: correct xmlns URI
+
+	class Point {
+	public:
+		int x;
+		int y;
+	};
+
+	class Symbol {
+	public:
+		int id;
+		std::wstring value;
+		std::wstring type;
+		int x;
+		int y;
+		int width;
+		int height;
+		std::vector<Point> polygon;
+	};
+
+	std::vector<Symbol> findSymbols(std::wstring fileName) {
+		std::vector<Symbol> result;
+
+		Magick::Image magick(toBytes(fileName));
+		int width = magick.columns();
+		int height = magick.rows();
+		Magick::Blob blob;
+		magick.modifyImage();
+		magick.write(&blob, "GRAY", 8);
+		const void *raw = blob.data();
+
+		zbar::Image image(width, height, "Y800", raw, width * height);
+		zbar::ImageScanner scanner;
+
+		scanner.scan(image);
+
+		int id = 0;
+		for (zbar::Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol, id++) {
+			Symbol s;
+
+
+			s.id = id;
+			s.type = fromBytes(symbol->get_type_name());
+			s.value = fromBytes(symbol->get_data());
+
+			int minX = 0;
+			int minY = 0;
+			int maxX = 0;
+			int maxY = 0;
+
+			// TODO: return original polygon in XML
+			for (int i = 0, locationSize = symbol->get_location_size(); i < locationSize; i++) {
+				int x = symbol->get_location_x(i);
+				int y = symbol->get_location_y(i);
+				minX = minX ? std::min(minX, x) : x;
+				minY = minY ? std::min(minY, y) : y;
+				maxX = std::max(maxX, x);
+				maxY = std::max(maxY, y);
+				s.polygon.push_back({x, y});
+			}
+
+			s.x = minX;
+			s.y = minY;
+			s.width = maxX - minX;
+			s.height = maxY - minY;
+
+			result.push_back(s);
+		}
+
+
+
+		return result;
+	}
+
+protected:
+
+	std::vector<AttributeMetadata> getOutputAttributesMetadata() override {
+		std::vector<AttributeMetadata> oam;
+		int i = 0;
+		oam.push_back({getAlias(i++, L"qr"), STRING});
+		oam.push_back({getAlias(i++, L"qr_count"), INTEGER});
+		oam.push_back({getAlias(i++, L"qr_xml"), STRING});
+		return oam;
+	}
+
+	std::vector<OutputAttribute> getOutputAttributes() override {
+		bool validInput = false;
+		bool hasSymbols = false;
+		std::wstring first;
+		std::stringstream xml;
+
+		std::vector<Symbol> symbols;
+		try {
+			symbols = findSymbols(getCurrentFile());
+			validInput = true;
+		} catch (...) {
+			// just ignore the errors;
+			// the file is probably not an image or we do not have read permissions
+			validInput = false;
+		}
+
+		for (Symbol s : symbols) {
+			// TODO: match against pattern (if any)
+			first = s.value;
+			hasSymbols = true;
+			break;
+		}
+
+		relpipe::xmlwriter::XMLWriter xmlWriter(xml);
+		xmlWriter.writeStartElement(L"qr",{L"xmlns", XMLNS});
+
+		// TODO: common metadata
+		// xmlWriter.writeStartElement(L"source");
+		// xmlWriter.writeTextElement(L"height",{}, std::to_wstring(...));
+		// xmlWriter.writeTextElement(L"width",{}, std::to_wstring(...));
+		// xmlWriter.writeEndElement();
+
+		for (Symbol s : symbols) {
+			xmlWriter.writeStartElement(L"symbol");
+			xmlWriter.writeTextElement(L"value",{}, s.value);
+			
+			// TODO: well-designed XML schema
+			// TODO: synchronize/share common XML parts with relpipe-in-qr
+			xmlWriter.writeStartElement(L"rectangular-box");
+			xmlWriter.writeTextElement(L"x",{}, std::to_wstring(s.x));
+			xmlWriter.writeTextElement(L"y",{}, std::to_wstring(s.y));
+			xmlWriter.writeTextElement(L"height",{}, std::to_wstring(s.height));
+			xmlWriter.writeTextElement(L"width",{}, std::to_wstring(s.width));
+			xmlWriter.writeEndElement();
+
+			xmlWriter.writeStartElement(L"polygon");
+			for (Point p : s.polygon) xmlWriter.writeEmptyElement(L"point",{L"x", std::to_wstring(p.x), L"y", std::to_wstring(p.y)});
+			xmlWriter.writeEndElement();
+
+			xmlWriter.writeEndElement();
+		}
+
+		xmlWriter.writeEndElement();
+
+		std::vector<OutputAttribute> oa;
+		// TODO: report also validInput (distinguish it from hasSymbols)
+		oa.push_back({first, !hasSymbols});
+		oa.push_back({std::to_wstring(symbols.size()), false});
+		oa.push_back({fromBytes(xml.str()), false});
+		return oa;
+	}
+};
+
+STREAMLET_RUN(QRStreamlet)