src/QRCommand.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 14 Apr 2021 20:56:18 +0200
branchv_0
changeset 1 79d699ed88ab
parent 0 0a0cb749c10f
child 2 6cc693048318
permissions -rw-r--r--
dirty and buggy first version

/**
 * Relational pipes
 * Copyright © 2021 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/>.
 */
#pragma once

#include <codecvt>
#include <memory>
#include <algorithm>
#include <iostream>
#include <stdexcept>

#include <Magick++.h>
#include <zbar.h>

#include "Configuration.h"

namespace relpipe {
namespace in {
namespace qr {

class QRCommand {
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: use platform encoding as default / check zbar encoding


public:

	void process(Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer, std::function<void() > relationalWriterFlush) {

		Magick::Image magick("/dev/stdin");
		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;

		writer->startRelation(L"qr",{
			{L"type", relpipe::writer::TypeId::STRING},
			// {L"addon_name", relpipe::writer::TypeId::STRING},
			{L"value", relpipe::writer::TypeId::STRING},
			// {L"xml", relpipe::writer::TypeId::STRING},
			{L"location_size", relpipe::writer::TypeId::INTEGER},
			{L"x_1", relpipe::writer::TypeId::INTEGER},
			{L"y_1", relpipe::writer::TypeId::INTEGER},
			{L"x_2", relpipe::writer::TypeId::INTEGER},
			{L"y_2", relpipe::writer::TypeId::INTEGER},
			{L"x_3", relpipe::writer::TypeId::INTEGER},
			{L"y_3", relpipe::writer::TypeId::INTEGER},
			{L"x_4", relpipe::writer::TypeId::INTEGER},
			{L"y_4", relpipe::writer::TypeId::INTEGER},
		}, true);

		int n = scanner.scan(image);

		for (zbar::Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol) {
			writer->writeAttribute(convertor.from_bytes(symbol->get_type_name()));
			// writer->writeAttribute(convertor.from_bytes(symbol->get_addon_name()));
			writer->writeAttribute(convertor.from_bytes(symbol->get_data()));
			// writer->writeAttribute(convertor.from_bytes(symbol->xml()));

			relpipe::common::type::Integer locationSize = symbol->get_location_size();
			writer->writeAttribute(&locationSize, typeid (locationSize));
			
			for (int i = 0; i < 4; i++) {
				relpipe::common::type::Integer x = -5;
				relpipe::common::type::Integer y = -6;
				if (i < locationSize) {
					x = symbol->get_location_x(i);
					y = symbol->get_location_y(i);
				}
				writer->writeAttribute(&x, typeid (x));
				writer->writeAttribute(&y, typeid (y));
			}


		}

		image.set_data(nullptr, 0);
	}
};

}
}
}