File: TestWriterMain.cpp

package info (click to toggle)
zxing-cpp 3.0.0%2Bds-1~exp2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 30,204 kB
  • sloc: ansic: 69,384; cpp: 34,587; php: 2,790; python: 199; makefile: 30; sh: 3
file content (48 lines) | stat: -rw-r--r-- 1,207 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Copyright 2016 Nu-book Inc.
* Copyright 2019 Axel Waggershauser
*/
// SPDX-License-Identifier: Apache-2.0

#include "ZXingCpp.h"

#include <vector>

using namespace ZXing;
using namespace std::literals;
using enum BarcodeFormat;

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>

void savePng(ImageView iv, BarcodeFormat format)
{
	stbi_write_png((ToString(format) + ".png"s).c_str(), iv.width(), iv.height(), iv.pixStride(), iv.data(), iv.rowStride());
}

int main()
{
	std::string text = "zxing-cpp";
	for (auto format : BarcodeFormats::list(AllMatrix))
		if (format & AllCreatable && format != AztecRune)
			savePng(CreateBarcodeFromText(text, format).symbol(), format);

	text = "012345678901234567890123456789";
	using FormatSpecs = std::vector<std::pair<BarcodeFormat, size_t>>;
	for (const auto& [format, length] : FormatSpecs({
		// {Codabar, 0}, // needs to start with A, B, C or D
		{Code39, 0},
		{Code93, 0},
		{Code128, 0},
		{EAN8, 7},
		{EAN13, 12},
		{ITF, 0},
		{UPCA, 11},
		{UPCE, 7}
	}))
	{
		auto input = length > 0 ? text.substr(0, length) : text;
		if (format & AllCreatable)
			savePng(WriteBarcodeToImage(CreateBarcodeFromText(input, format)), format);
	}
}