Fixes from Konstantin
[zxing.git] / symbian / ZXingBarcodeReader / src / DecodingOperations.cpp
1 #include "ZXingBarcodeReaderAppView.h"\r
2 #include <e32std.h>\r
3 \r
4 #include <zxing/qrcode/QRCodeReader.h>\r
5 \r
6 #include <zxing/common/GlobalHistogramBinarizer.h>\r
7 #include <zxing/Binarizer.h>\r
8 #include <zxing/BinaryBitmap.h>\r
9 #include <CameraImage.h>\r
10 #include <string>\r
11 \r
12 using namespace zxing;\r
13 using namespace zxing::qrcode;\r
14 \r
15 void CZXingBarcodeReaderAppView::StartTimer()\r
16         {\r
17         const TInt tickInterval=2000000;\r
18         iPeriodic=CPeriodic::NewL(0); // neutral priority\r
19 \r
20         //CleanupStack::PushL(iPeriodic);\r
21 \r
22         iPeriodic->Start(tickInterval,tickInterval,TCallBack(&CZXingBarcodeReaderAppView::Tick, this));\r
23 \r
24 //      CleanupStack::PopAndDestroy(iPeriodic);\r
25         }\r
26 \r
27 TInt CZXingBarcodeReaderAppView::Tick(TAny* aObject)\r
28         {\r
29         // cast, and call non-static function\r
30         ((CZXingBarcodeReaderAppView*)aObject)->decodeBackbufferImage();\r
31         return 1;\r
32         }\r
33 \r
34 void CZXingBarcodeReaderAppView::decodeBackbufferImage()\r
35         {\r
36         QRCodeReader decoder;\r
37 \r
38         CameraImage image;\r
39         image.setImage(iBackBuffer);\r
40 \r
41 \r
42         Ref<Result> res;\r
43 \r
44         try\r
45                 {\r
46                 Ref<LuminanceSource> imageRef(new CameraImage(image));\r
47                 GlobalHistogramBinarizer* binz = new GlobalHistogramBinarizer(imageRef);\r
48         \r
49                 Ref<Binarizer> bz (binz);\r
50                 BinaryBitmap* bb = new BinaryBitmap(bz);\r
51         \r
52                 Ref<BinaryBitmap> ref(bb);\r
53         \r
54                 res = decoder.decode(ref);\r
55         \r
56                 string string = res->getText()->getText();\r
57                 HBufC8 *pHeap8 = HBufC8::NewMaxLC(string.size());\r
58                 pHeap8->Des().Copy((const TUint8 *)string.c_str());\r
59                 \r
60                 HBufC *pHeap16 = HBufC::NewMaxLC(pHeap8->Length());\r
61                 pHeap16->Des().Copy(*pHeap8);\r
62                 \r
63                 ShowResultL(*pHeap16);\r
64                 }\r
65         catch(zxing::Exception& e)\r
66                 {\r
67                         string string = "Error...retrying...";\r
68                         HBufC8 *pHeap8 = HBufC8::NewMaxLC(string.size());\r
69                         pHeap8->Des().Copy((const TUint8 *)string.c_str());\r
70                         \r
71                         HBufC *pHeap16 = HBufC::NewMaxLC(pHeap8->Length());\r
72                         pHeap16->Des().Copy(*pHeap8);\r
73                         \r
74                         ShowResultL(*pHeap16);\r
75                 }\r
76         }\r
77 \r
78 void CZXingBarcodeReaderAppView::ShowResultL(TDesC16& message)\r
79         {\r
80         if (!iNote)\r
81                 {\r
82         // Create the note once\r
83         iNote = CAknInfoPopupNoteController::NewL();\r
84                 }\r
85         // Hide the note. The last note may be visible when creating the second\r
86         iNote->HideInfoPopupNote();\r
87 \r
88         // Set the time delay period before the popup is shown (in milliseconds)\r
89         iNote->SetTimeDelayBeforeShow(100);\r
90 \r
91         // Set the time period of how long the popup is in the view (in milliseconds)\r
92         iNote->SetTimePopupInView(2*1000);\r
93 \r
94         // Note text\r
95         iNote->SetTextL(message);\r
96         \r
97         TRect rect(Rect());\r
98         \r
99         // Note position\r
100         iNote->SetPositionAndAlignment(TPoint(rect.Width()/5,rect.Height()/7),EHLeftVTop);\r
101 \r
102         // Show note\r
103         iNote->ShowInfoPopupNote();\r
104         }\r