Updates to C++ port:
[zxing.git] / cpp / magick / src / example.cpp
1 /*
2  *  example.cpp
3  *  zxing
4  *
5  *  Created by Ralf Kistner on 16/10/2009.
6  *  Copyright 2008 ZXing authors All rights reserved.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <iostream>
22 #include <fstream>
23 #include <string>
24 #include <Magick++.h>
25 #include "MagickBitmapSource.h"
26 #include <zxing/qrcode/QRCodeReader.h>
27 #include <zxing/common/GlobalHistogramBinarizer.h>
28 #include <zxing/common/LocalBlockBinarizer.h>
29 #include <zxing/Exception.h>
30
31 using namespace Magick;
32 using namespace std;
33 using namespace zxing;
34 using namespace zxing::qrcode;
35
36
37 void decode_image(Image& image, bool localized) {
38   try {
39     Ref<MagickBitmapSource> source(new MagickBitmapSource(image));
40     
41     Ref<Binarizer> binarizer(NULL);
42     if (localized) {
43       binarizer = new LocalBlockBinarizer(source);
44     } else {
45       binarizer = new GlobalHistogramBinarizer(source);
46     }
47     
48
49     Ref<BinaryBitmap> image(new BinaryBitmap(binarizer));
50     QRCodeReader reader;
51     Ref<Result> result(reader.decode(image));
52     
53     cout << result->getText()->getText() << endl;
54   } catch (zxing::Exception& e) {
55     cerr << "Error: " << e.what() << endl;
56   }
57 }
58
59
60 int main(int argc, char** argv) {
61   if (argc < 2) {
62     cout << "Usage: " << argv[0] << "<filename1> [<filename2> ...]" << endl;
63     return 1;
64   }
65   for (int i = 1; i < argc; i++) {
66     string infilename = argv[i];
67     cout << "Processing: " << infilename << endl;
68     Image image;
69     try {
70       image.read(infilename);
71     } catch (...) {
72       cerr << "Unable to open image, ignoring" << endl;
73       continue;
74     }
75     
76     bool local = true;  // Use local thresholding
77     
78     decode_image(image, local);
79   }
80   return 0;
81 }
82
83