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