PageRenderTime 33ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/barcode/mobile/ios/zxing/cpp/magick/src/main.cpp

https://bitbucket.org/plepic/titanium_modules
C++ | 241 lines | 180 code | 36 blank | 25 comment | 35 complexity | 7288ffbdc1b69deb1237cbe7d6601f35 MD5 | raw file
  1. /*
  2. * main.cpp
  3. * zxing
  4. *
  5. * Copyright 2010 ZXing authors All rights reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include <iostream>
  20. #include <fstream>
  21. #include <string>
  22. #include <Magick++.h>
  23. #include "MagickBitmapSource.h"
  24. #include <zxing/common/Counted.h>
  25. //#include <zxing/qrcode/QRCodeReader.h>
  26. #include <zxing/Binarizer.h>
  27. #include <zxing/MultiFormatReader.h>
  28. #include <zxing/Result.h>
  29. #include <zxing/ReaderException.h>
  30. #include <zxing/common/GlobalHistogramBinarizer.h>
  31. #include <zxing/common/HybridBinarizer.h>
  32. #include <exception>
  33. #include <zxing/Exception.h>
  34. #include <zxing/common/IllegalArgumentException.h>
  35. #include <zxing/BinaryBitmap.h>
  36. #include <zxing/DecodeHints.h>
  37. //#include <zxing/qrcode/detector/Detector.h>
  38. //#include <zxing/qrcode/detector/QREdgeDetector.h>
  39. //#include <zxing/qrcode/decoder/Decoder.h>
  40. using namespace Magick;
  41. using namespace std;
  42. using namespace zxing;
  43. //using namespace zxing::qrcode;
  44. static bool raw_dump = false;
  45. static bool show_format = false;
  46. static bool tryHarder = false;
  47. static bool show_filename = false;
  48. static const int MAX_EXPECTED = 1024;
  49. Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints) {
  50. Ref<Reader> reader(new MultiFormatReader);
  51. return reader->decode(image, hints);
  52. }
  53. int test_image(Image& image, bool hybrid, string expected = "") {
  54. string cell_result;
  55. int res = -1;
  56. Ref<BitMatrix> matrix(NULL);
  57. Ref<Binarizer> binarizer(NULL);
  58. const char* result_format = "";
  59. try {
  60. Ref<MagickBitmapSource> source(new MagickBitmapSource(image));
  61. if (hybrid) {
  62. binarizer = new HybridBinarizer(source);
  63. } else {
  64. binarizer = new GlobalHistogramBinarizer(source);
  65. }
  66. DecodeHints hints(DecodeHints::DEFAULT_HINT);
  67. hints.setTryHarder(tryHarder);
  68. Ref<BinaryBitmap> binary(new BinaryBitmap(binarizer));
  69. Ref<Result> result(decode(binary, hints));
  70. cell_result = result->getText()->getText();
  71. result_format = barcodeFormatNames[result->getBarcodeFormat()];
  72. res = 0;
  73. } catch (ReaderException e) {
  74. cell_result = "zxing::ReaderException: " + string(e.what());
  75. res = -2;
  76. } catch (zxing::IllegalArgumentException& e) {
  77. cell_result = "zxing::IllegalArgumentException: " + string(e.what());
  78. res = -3;
  79. } catch (zxing::Exception& e) {
  80. cell_result = "zxing::Exception: " + string(e.what());
  81. res = -4;
  82. } catch (std::exception& e) {
  83. cell_result = "std::exception: " + string(e.what());
  84. res = -5;
  85. }
  86. if (cell_result.compare(expected)) {
  87. res = -6;
  88. if (!raw_dump) {
  89. cout << (hybrid ? "Hybrid" : "Global") << " binarizer failed:\n";
  90. if (expected.length() >= 0) {
  91. cout << " Expected: " << expected << "\n";
  92. }
  93. cout << " Detected: " << cell_result << endl;
  94. }
  95. }
  96. if (raw_dump && !hybrid) {/* don't print twice, and global is a bit better */
  97. cout << cell_result;
  98. if (show_format) {
  99. cout << " " << result_format;
  100. }
  101. cout << endl;
  102. }
  103. return res;
  104. }
  105. int test_image_hybrid(Image& image, string expected = "") {
  106. return test_image(image, true, expected);
  107. }
  108. int test_image_global(Image& image, string expected = "") {
  109. return test_image(image, false, expected);
  110. }
  111. string get_expected(string imagefilename) {
  112. string textfilename = imagefilename;
  113. int dotpos = textfilename.rfind(".");
  114. textfilename.replace(dotpos+1, textfilename.length() - dotpos - 1, "txt");
  115. char data[MAX_EXPECTED];
  116. FILE *fp = fopen(textfilename.data(), "rb");
  117. if (!fp) {
  118. // could not open file
  119. return "";
  120. }
  121. // get file size
  122. fseek(fp, 0, SEEK_END);
  123. int toread = ftell(fp);
  124. rewind(fp);
  125. if (toread > MAX_EXPECTED) {
  126. cerr << "MAX_EXPECTED = " << MAX_EXPECTED << " but file '" << textfilename << "' has " << toread
  127. << " bytes! Skipping..." << endl;
  128. fclose(fp);
  129. return "";
  130. }
  131. int nread = fread(data, sizeof(char), toread, fp);
  132. if (nread != toread) {
  133. cerr << "Could not read entire contents of file '" << textfilename << "'! Skipping..." << endl;
  134. fclose(fp);
  135. return "";
  136. }
  137. fclose(fp);
  138. data[nread] = '\0';
  139. string expected(data);
  140. return expected;
  141. }
  142. int main(int argc, char** argv) {
  143. if (argc <= 1) {
  144. cout << "Usage: " << argv[0] << " [--dump-raw] [--show-format] [--try-harder] [--show-filename] <filename1> [<filename2> ...]" << endl;
  145. return 1;
  146. }
  147. int total = 0;
  148. int gonly = 0;
  149. int honly = 0;
  150. int both = 0;
  151. int neither = 0;
  152. if (argc == 2) raw_dump = true;
  153. for (int i = 1; i < argc; i++) {
  154. string infilename = argv[i];
  155. if (infilename.substr(infilename.length()-3,3).compare("txt") == 0) {
  156. continue;
  157. }
  158. if (infilename.compare("--dump-raw") == 0) {
  159. raw_dump = true;
  160. continue;
  161. }
  162. if (infilename.compare("--show-format") == 0) {
  163. show_format = true;
  164. continue;
  165. }
  166. if (infilename.compare("--try-harder") == 0) {
  167. tryHarder = true;
  168. continue;
  169. }
  170. if (infilename.compare("--show-filename") == 0) {
  171. show_filename = true;
  172. continue;
  173. }
  174. if (!raw_dump)
  175. cerr << "Processing: " << infilename << endl;
  176. if (show_filename)
  177. cout << infilename << " ";
  178. Image image;
  179. try {
  180. image.read(infilename);
  181. } catch (...) {
  182. cerr << "Unable to open image, ignoring" << endl;
  183. continue;
  184. }
  185. string expected;
  186. expected = get_expected(infilename);
  187. int gresult = 1;
  188. int hresult = 1;
  189. hresult = test_image_hybrid(image, expected);
  190. gresult = test_image_global(image, expected);
  191. gresult = gresult == 0;
  192. hresult = hresult == 0;
  193. gonly += gresult && !hresult;
  194. honly += hresult && !gresult;
  195. both += gresult && hresult;
  196. neither += !gresult && !hresult;
  197. total = total + 1;
  198. }
  199. if (!raw_dump)
  200. cout << (honly+both) << " passed hybrid, " << (gonly+both) << " passed global, "
  201. << both << " pass both, " << neither << " pass neither, " << honly
  202. << " passed only hybrid, " << gonly << " passed only global, of " << total
  203. << " total." << endl;
  204. return 0;
  205. }