Skip to content

Commit

Permalink
ZXingReader: support reading image file from stdin by passing '-'
Browse files Browse the repository at this point in the history
This fixes zxing-cpp#817.
  • Loading branch information
axxel committed Aug 26, 2024
1 parent d979b76 commit 5de0ca1
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions example/ZXingReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -207,8 +207,10 @@ int main(int argc, char* argv[])

for (const auto& filePath : cli.filePaths) {
int width, height, channels;
std::unique_ptr<stbi_uc, void (*)(void*)> buffer(stbi_load(filePath.c_str(), &width, &height, &channels, cli.forceChannels),
stbi_image_free);
std::unique_ptr<stbi_uc, void (*)(void*)> 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;
Expand Down

0 comments on commit 5de0ca1

Please sign in to comment.