From 5de0ca11d0eb5785147b51e8be8482acd1750b3c Mon Sep 17 00:00:00 2001 From: axxel Date: Mon, 26 Aug 2024 15:35:24 +0200 Subject: [PATCH] ZXingReader: support reading image file from stdin by passing '-' This fixes #817. --- example/ZXingReader.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/example/ZXingReader.cpp b/example/ZXingReader.cpp index 8c17260264..059c735e4e 100644 --- a/example/ZXingReader.cpp +++ b/example/ZXingReader.cpp @@ -79,7 +79,7 @@ static bool ParseOptions(int argc, char* argv[], ReaderOptions& options, CLI& cl #endif for (int i = 1; i < argc; ++i) { - auto is = [&](const char* str) { return strncmp(argv[i], str, strlen(argv[i])) == 0; }; + auto is = [&](const char* str) { return strlen(argv[i]) > 1 && strncmp(argv[i], str, strlen(argv[i])) == 0; }; if (is("-fast")) { options.setTryHarder(false); #ifdef ZXING_EXPERIMENTAL_API @@ -207,8 +207,10 @@ int main(int argc, char* argv[]) for (const auto& filePath : cli.filePaths) { int width, height, channels; - std::unique_ptr buffer(stbi_load(filePath.c_str(), &width, &height, &channels, cli.forceChannels), - stbi_image_free); + std::unique_ptr buffer( + filePath == "-" ? stbi_load_from_file(stdin, &width, &height, &channels, cli.forceChannels) + : stbi_load(filePath.c_str(), &width, &height, &channels, cli.forceChannels), + stbi_image_free); if (buffer == nullptr) { std::cerr << "Failed to read image: " << filePath << " (" << stbi_failure_reason() << ")" << "\n"; return -1;