Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / xfa / src / fxbarcode / src / BC_QRCodeReader.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4 \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #include "barcode.h"\r
8 #include "include/BC_Reader.h"\r
9 #include "include/BC_QRCodeReader.h"\r
10 #include "include/BC_LuminanceSource.h"\r
11 #include "include/BC_BufferedImageLuminanceSource.h"\r
12 #include "include/BC_Binarizer.h"\r
13 #include "include/BC_GlobalHistogramBinarizer.h"\r
14 #include "include/BC_BinaryBitmap.h"\r
15 #include "include/BC_QRCodeReader.h"\r
16 #include "include/BC_QRCoderMode.h"\r
17 #include "include/BC_ResultPoint.h"\r
18 #include "include/BC_QRCoderDecoder.h"\r
19 #include "include/BC_QRDetector.h"\r
20 #include "include/BC_QRDetectorResult.h"\r
21 #include "include/BC_QRCoderErrorCorrectionLevel.h"\r
22 #include "include/BC_QRDataMask.h"\r
23 #include "include/BC_ReedSolomonGF256.h"\r
24 #include "include/BC_QRCoderVersion.h"\r
25 #include "include/BC_CommonDecoderResult.h"\r
26 #include "include/BC_QRCodeReader.h"\r
27 CBC_QRCodeReader::CBC_QRCodeReader(): m_decoder(NULL)\r
28 {\r
29 }\r
30 void CBC_QRCodeReader::Init()\r
31 {\r
32     m_decoder = FX_NEW CBC_QRCoderDecoder;\r
33     m_decoder->Init();\r
34 }\r
35 CBC_QRCodeReader::~CBC_QRCodeReader()\r
36 {\r
37     if(m_decoder != NULL) {\r
38         delete m_decoder;\r
39     }\r
40     m_decoder = NULL;\r
41 }\r
42 CFX_ByteString CBC_QRCodeReader::Decode(CBC_BinaryBitmap *image, FX_INT32 hints, FX_INT32 &e)\r
43 {\r
44     CBC_CommonBitMatrix *matrix = image->GetMatrix(e);\r
45     BC_EXCEPTION_CHECK_ReturnValue(e, "");\r
46     CBC_QRDetector detector(matrix);\r
47     CBC_QRDetectorResult* qdr = detector.Detect(hints, e);\r
48     BC_EXCEPTION_CHECK_ReturnValue(e, "");\r
49     CBC_AutoPtr<CBC_QRDetectorResult> detectorResult(qdr);\r
50     CBC_CommonDecoderResult* qdr2 = m_decoder->Decode(detectorResult->GetBits(), 0, e);\r
51     BC_EXCEPTION_CHECK_ReturnValue(e, "");\r
52     CBC_AutoPtr<CBC_CommonDecoderResult> decodeResult(qdr2);\r
53     return (decodeResult->GetText());\r
54 }\r
55 CFX_ByteString CBC_QRCodeReader::Decode(const CFX_WideString &filename, FX_INT32 hints, FX_INT32 byteModeDecode, FX_INT32 &e)\r
56 {\r
57     CBC_BufferedImageLuminanceSource source(filename);\r
58     source.Init(e);\r
59     BC_EXCEPTION_CHECK_ReturnValue(e, "");\r
60     CBC_GlobalHistogramBinarizer binarizer(&source);\r
61     CBC_BinaryBitmap bitmap(&binarizer);\r
62     CFX_ByteString bs = Decode(&bitmap, hints, e);\r
63     BC_EXCEPTION_CHECK_ReturnValue(e, "");\r
64     return bs;\r
65 }\r
66 CFX_ByteString CBC_QRCodeReader::Decode(CFX_DIBitmap *pBitmap, FX_INT32 hints, FX_INT32 byteModeDecode, FX_INT32 &e)\r
67 {\r
68     CBC_BufferedImageLuminanceSource source(pBitmap);\r
69     CBC_GlobalHistogramBinarizer binarizer(&source);\r
70     CBC_BinaryBitmap bitmap(&binarizer);\r
71     CFX_ByteString bs = Decode(&bitmap, hints, e);\r
72     BC_EXCEPTION_CHECK_ReturnValue(e, "");\r
73     return bs;\r
74 }\r
75 CFX_ByteString CBC_QRCodeReader::Decode(CBC_BinaryBitmap *image, FX_INT32 &e)\r
76 {\r
77     CFX_ByteString bs = Decode(image, 0, e);\r
78     BC_EXCEPTION_CHECK_ReturnValue(e, "");\r
79     return bs;\r
80 }\r
81 void CBC_QRCodeReader::ReleaseAll()\r
82 {\r
83     if(CBC_ReedSolomonGF256 ::QRCodeFild) {\r
84         delete CBC_ReedSolomonGF256 ::QRCodeFild;\r
85         CBC_ReedSolomonGF256 ::QRCodeFild = NULL;\r
86     }\r
87     if(CBC_ReedSolomonGF256 ::DataMatrixField) {\r
88         delete CBC_ReedSolomonGF256 ::DataMatrixField;\r
89         CBC_ReedSolomonGF256 ::DataMatrixField = NULL;\r
90     }\r
91     CBC_QRCoderMode::Destroy();\r
92     CBC_QRCoderErrorCorrectionLevel::Destroy();\r
93     CBC_QRDataMask::Destroy();\r
94     CBC_QRCoderVersion::Destroy();\r
95 }\r