TestReaderMain.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2016 Nu-book Inc.
  3. * Copyright 2017 Axel Waggershauser
  4. */
  5. // SPDX-License-Identifier: Apache-2.0
  6. #include "BlackboxTestRunner.h"
  7. #include "ImageLoader.h"
  8. #include "ReadBarcode.h"
  9. #include "ZXAlgorithms.h"
  10. #include <cstdlib>
  11. #include <cstring>
  12. #include <fstream>
  13. #include <iostream>
  14. #include <set>
  15. using namespace ZXing;
  16. using namespace ZXing::Test;
  17. int getEnv(const char* name, int fallback = 0)
  18. {
  19. auto var = getenv(name);
  20. return var ? atoi(var) : fallback;
  21. }
  22. int main(int argc, char** argv)
  23. {
  24. if (argc <= 1) {
  25. std::cout << "Usage: " << argv[0] << " <test_path_prefix>" << std::endl;
  26. return 0;
  27. }
  28. fs::path pathPrefix = argv[1];
  29. if (Contains({".png", ".jpg", ".pgm", ".gif"}, pathPrefix.extension())) {
  30. auto opts = ReaderOptions().setTryHarder(!getEnv("FAST", false)).setTryRotate(true).setIsPure(getEnv("IS_PURE"));
  31. if (getenv("FORMATS"))
  32. opts.setFormats(BarcodeFormatsFromString(getenv("FORMATS")));
  33. int rotation = getEnv("ROTATION");
  34. for (int i = 1; i < argc; ++i) {
  35. Barcode barcode = ReadBarcode(ImageLoader::load(argv[i]).rotated(rotation), opts);
  36. std::cout << argv[i] << ": ";
  37. if (barcode.isValid())
  38. std::cout << ToString(barcode.format()) << ": " << barcode.text() << "\n";
  39. else
  40. std::cout << "FAILED\n";
  41. if (barcode.isValid() && getenv("WRITE_TEXT")) {
  42. std::ofstream f(fs::path(argv[i]).replace_extension(".txt"));
  43. f << barcode.text();
  44. }
  45. }
  46. return 0;
  47. } else {
  48. std::set<std::string> includedTests;
  49. for (int i = 2; i < argc; ++i) {
  50. if (std::strlen(argv[i]) > 2 && argv[i][0] == '-' && argv[i][1] == 't') {
  51. includedTests.insert(argv[i] + 2);
  52. }
  53. }
  54. return runBlackBoxTests(pathPrefix, includedTests);
  55. }
  56. }