Skip to content

Commit 5de0ca1

Browse files
committed
ZXingReader: support reading image file from stdin by passing '-'
This fixes #817.
1 parent d979b76 commit 5de0ca1

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

example/ZXingReader.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static bool ParseOptions(int argc, char* argv[], ReaderOptions& options, CLI& cl
7979
#endif
8080

8181
for (int i = 1; i < argc; ++i) {
82-
auto is = [&](const char* str) { return strncmp(argv[i], str, strlen(argv[i])) == 0; };
82+
auto is = [&](const char* str) { return strlen(argv[i]) > 1 && strncmp(argv[i], str, strlen(argv[i])) == 0; };
8383
if (is("-fast")) {
8484
options.setTryHarder(false);
8585
#ifdef ZXING_EXPERIMENTAL_API
@@ -207,8 +207,10 @@ int main(int argc, char* argv[])
207207

208208
for (const auto& filePath : cli.filePaths) {
209209
int width, height, channels;
210-
std::unique_ptr<stbi_uc, void (*)(void*)> buffer(stbi_load(filePath.c_str(), &width, &height, &channels, cli.forceChannels),
211-
stbi_image_free);
210+
std::unique_ptr<stbi_uc, void (*)(void*)> buffer(
211+
filePath == "-" ? stbi_load_from_file(stdin, &width, &height, &channels, cli.forceChannels)
212+
: stbi_load(filePath.c_str(), &width, &height, &channels, cli.forceChannels),
213+
stbi_image_free);
212214
if (buffer == nullptr) {
213215
std::cerr << "Failed to read image: " << filePath << " (" << stbi_failure_reason() << ")" << "\n";
214216
return -1;

0 commit comments

Comments
 (0)