streamlet-examples/barcode-reader.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 01 May 2021 18:18:01 +0200
branchv_0
changeset 92 cf4971342380
parent 91 streamlet-examples/qr-decode.cpp@cb1adcd17d0c
permissions -rw-r--r--
streamlet examples: QR: rename qr → barcode

/**
 * 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
 * 
 * Options:
 *  - value-pattern: regular expression describing expected value of QR code;
 *    if one or more options are given, the "qr" attribute will contain first value matching any of these patterns
 * 
 */
class QRStreamlet : public Streamlet {
private:

	const wstring XMLNS = L"tag:globalcode.info,2018:barcode";

	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;
	}

	bool matchesAny(const std::wstring& value, const std::vector<std::wregex>& valuePatterns) {
		for (const std::wregex& pattern : valuePatterns) {
			if (std::regex_match(value, pattern)) return true;
		}
		return false;
	}

	std::vector<std::wregex> getOptionsAsPatterns(std::wstring optionName) {
		std::vector<std::wregex> result;
		for (Option o : getOptions(optionName)) result.push_back(std::wregex(o.value));
		return result;
	}

protected:

	std::vector<AttributeMetadata> getOutputAttributesMetadata() override {
		std::vector<AttributeMetadata> oam;
		int i = 0;
		oam.push_back({getAlias(i++, L"barcode"), STRING});
		oam.push_back({getAlias(i++, L"barcode_count"), INTEGER});
		oam.push_back({getAlias(i++, L"barcode_xml"), STRING});
		return oam;
	}

	std::vector<OutputAttribute> getOutputAttributes() override {
		bool matchedFile = false;
		bool validInput = false;
		bool matchedFirst = false;
		std::wstring first;
		std::stringstream xml;

		std::vector<std::wregex> filePatterns = getOptionsAsPatterns(L"file-pattern");
		matchedFile = filePatterns.size() == 0 || matchesAny(getCurrentFile(), filePatterns);

		std::vector<Symbol> symbols;
		if (matchedFile) {
			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;
			}

			std::vector<std::wregex> valuePatterns = getOptionsAsPatterns(L"value-pattern");

			for (Symbol s : symbols) {
				if (valuePatterns.size() == 0 || matchesAny(s.value, valuePatterns)) {
					first = s.value;
					matchedFirst = true;
					break;
				}
			}

			relpipe::xmlwriter::XMLWriter xmlWriter(xml);
			xmlWriter.writeStartElement(L"barcodes-recognized",{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 and matchedFile (distinguish them from matchedFirst)?
		oa.push_back({first, !matchedFirst});
		oa.push_back({std::to_wstring(symbols.size()), !matchedFile});
		oa.push_back({fromBytes(xml.str()), !matchedFile});
		return oa;
	}
};

STREAMLET_RUN(QRStreamlet)