Skip to content

Commit

Permalink
example: support writing barcode as SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
axxel committed May 9, 2022
1 parent f009fe7 commit a2c64fe
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
24 changes: 24 additions & 0 deletions core/src/BitMatrixIO.cpp
Expand Up @@ -20,6 +20,7 @@
#include "BitArray.h"

#include <fstream>
#include <sstream>

namespace ZXing {

Expand All @@ -44,6 +45,29 @@ std::string ToString(const BitMatrix& matrix, char one, char zero, bool addSpace
return result;
}

std::string ToSVG(const BitMatrix& matrix)
{
// see https://stackoverflow.com/questions/10789059/create-qr-code-in-vector-image/60638350#60638350

const int width = matrix.width();
const int height = matrix.height();
std::ostringstream out;

out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
<< "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 " << width << " " << height
<< "\" stroke=\"none\">\n"
<< "<path d=\"";

for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x)
if (matrix.get(x, y))
out << "M" << x << "," << y << "h1v1h-1z";

out << "\"/>\n</svg>";

return out.str();
}

BitMatrix ParseBitMatrix(const std::string& str, char one, bool expectSpace)
{
auto lineLength = str.find('\n');
Expand Down
4 changes: 2 additions & 2 deletions core/src/BitMatrixIO.h
Expand Up @@ -22,8 +22,8 @@

namespace ZXing {

std::string ToString(const BitMatrix& matrix, char one = 'X', char zero = ' ', bool addSpace = true,
bool printAsCString = false);
std::string ToString(const BitMatrix& matrix, char one = 'X', char zero = ' ', bool addSpace = true, bool printAsCString = false);
std::string ToSVG(const BitMatrix& matrix);
BitMatrix ParseBitMatrix(const std::string& str, char one = 'X', bool expectSpace = true);
void SaveAsPBM(const BitMatrix& matrix, const std::string filename, int quietZone = 0);

Expand Down
11 changes: 9 additions & 2 deletions example/ZXingWriter.cpp
Expand Up @@ -16,13 +16,15 @@

#include "BarcodeFormat.h"
#include "BitMatrix.h"
#include "BitMatrixIO.h"
#include "MultiFormatWriter.h"
#include "TextUtfEncoding.h"
#include "CharacterSetECI.h"

#include <algorithm>
#include <cctype>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>

Expand All @@ -42,7 +44,9 @@ static void PrintUsage(const char* exePath)
<< "Supported formats are:\n";
for (auto f : BarcodeFormatsFromString("Aztec Codabar Code39 Code93 Code128 DataMatrix EAN8 EAN13 ITF PDF417 QRCode UPCA UPCE"))
std::cout << " " << ToString(f) << "\n";
std::cout << "Format can be lowercase letters, with or without '-'.\n";

std::cout << "Format can be lowercase letters, with or without '-'.\n"
<< "Output format is determined by file name, supported are png, jpg and svg.\n";
}

static bool ParseSize(std::string str, int* width, int* height)
Expand Down Expand Up @@ -128,14 +132,17 @@ int main(int argc, char* argv[])

try {
auto writer = MultiFormatWriter(format).setMargin(margin).setEncoding(encoding).setEccLevel(eccLevel);
auto bitmap = ToMatrix<uint8_t>(writer.encode(TextUtfEncoding::FromUtf8(text), width, height));
auto matrix = writer.encode(TextUtfEncoding::FromUtf8(text), width, height);
auto bitmap = ToMatrix<uint8_t>(matrix);

auto ext = GetExtension(filePath);
int success = 0;
if (ext == "" || ext == "png") {
success = stbi_write_png(filePath.c_str(), bitmap.width(), bitmap.height(), 1, bitmap.data(), 0);
} else if (ext == "jpg" || ext == "jpeg") {
success = stbi_write_jpg(filePath.c_str(), bitmap.width(), bitmap.height(), 1, bitmap.data(), 0);
} else if (ext == "svg") {
success = (std::ofstream(filePath) << ToSVG(matrix)).good();
}

if (!success) {
Expand Down

0 comments on commit a2c64fe

Please sign in to comment.