1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 // Original code is licensed as follows:
7 /*
8  * Copyright 2007 ZXing authors
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #include "xfa/src/fxbarcode/barcode.h"
24 #include "xfa/src/fxbarcode/BC_Reader.h"
25 #include "xfa/src/fxbarcode/BC_LuminanceSource.h"
26 #include "xfa/src/fxbarcode/BC_BufferedImageLuminanceSource.h"
27 #include "xfa/src/fxbarcode/BC_Binarizer.h"
28 #include "xfa/src/fxbarcode/BC_BinaryBitmap.h"
29 #include "xfa/src/fxbarcode/BC_ResultPoint.h"
30 #include "xfa/src/fxbarcode/common/BC_GlobalHistogramBinarizer.h"
31 #include "xfa/src/fxbarcode/common/BC_CommonDecoderResult.h"
32 #include "xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h"
33 #include "BC_QRCodeReader.h"
34 #include "BC_QRCodeReader.h"
35 #include "BC_QRCoderMode.h"
36 #include "BC_QRCoderDecoder.h"
37 #include "BC_QRDetector.h"
38 #include "BC_QRDetectorResult.h"
39 #include "BC_QRCoderErrorCorrectionLevel.h"
40 #include "BC_QRDataMask.h"
41 #include "BC_QRCodeReader.h"
42 #include "BC_QRCoderVersion.h"
CBC_QRCodeReader()43 CBC_QRCodeReader::CBC_QRCodeReader() : m_decoder(NULL) {}
Init()44 void CBC_QRCodeReader::Init() {
45   m_decoder = new CBC_QRCoderDecoder;
46   m_decoder->Init();
47 }
~CBC_QRCodeReader()48 CBC_QRCodeReader::~CBC_QRCodeReader() {
49   if (m_decoder != NULL) {
50     delete m_decoder;
51   }
52   m_decoder = NULL;
53 }
Decode(CBC_BinaryBitmap * image,int32_t hints,int32_t & e)54 CFX_ByteString CBC_QRCodeReader::Decode(CBC_BinaryBitmap* image,
55                                         int32_t hints,
56                                         int32_t& e) {
57   CBC_CommonBitMatrix* matrix = image->GetMatrix(e);
58   BC_EXCEPTION_CHECK_ReturnValue(e, "");
59   CBC_QRDetector detector(matrix);
60   CBC_QRDetectorResult* qdr = detector.Detect(hints, e);
61   BC_EXCEPTION_CHECK_ReturnValue(e, "");
62   CBC_AutoPtr<CBC_QRDetectorResult> detectorResult(qdr);
63   CBC_CommonDecoderResult* qdr2 =
64       m_decoder->Decode(detectorResult->GetBits(), 0, e);
65   BC_EXCEPTION_CHECK_ReturnValue(e, "");
66   CBC_AutoPtr<CBC_CommonDecoderResult> decodeResult(qdr2);
67   return (decodeResult->GetText());
68 }
Decode(const CFX_WideString & filename,int32_t hints,int32_t byteModeDecode,int32_t & e)69 CFX_ByteString CBC_QRCodeReader::Decode(const CFX_WideString& filename,
70                                         int32_t hints,
71                                         int32_t byteModeDecode,
72                                         int32_t& e) {
73   CBC_BufferedImageLuminanceSource source(filename);
74   source.Init(e);
75   BC_EXCEPTION_CHECK_ReturnValue(e, "");
76   CBC_GlobalHistogramBinarizer binarizer(&source);
77   CBC_BinaryBitmap bitmap(&binarizer);
78   CFX_ByteString bs = Decode(&bitmap, hints, e);
79   BC_EXCEPTION_CHECK_ReturnValue(e, "");
80   return bs;
81 }
Decode(CFX_DIBitmap * pBitmap,int32_t hints,int32_t byteModeDecode,int32_t & e)82 CFX_ByteString CBC_QRCodeReader::Decode(CFX_DIBitmap* pBitmap,
83                                         int32_t hints,
84                                         int32_t byteModeDecode,
85                                         int32_t& e) {
86   CBC_BufferedImageLuminanceSource source(pBitmap);
87   CBC_GlobalHistogramBinarizer binarizer(&source);
88   CBC_BinaryBitmap bitmap(&binarizer);
89   CFX_ByteString bs = Decode(&bitmap, hints, e);
90   BC_EXCEPTION_CHECK_ReturnValue(e, "");
91   return bs;
92 }
Decode(CBC_BinaryBitmap * image,int32_t & e)93 CFX_ByteString CBC_QRCodeReader::Decode(CBC_BinaryBitmap* image, int32_t& e) {
94   CFX_ByteString bs = Decode(image, 0, e);
95   BC_EXCEPTION_CHECK_ReturnValue(e, "");
96   return bs;
97 }
ReleaseAll()98 void CBC_QRCodeReader::ReleaseAll() {
99   if (CBC_ReedSolomonGF256::QRCodeFild) {
100     delete CBC_ReedSolomonGF256::QRCodeFild;
101     CBC_ReedSolomonGF256::QRCodeFild = NULL;
102   }
103   if (CBC_ReedSolomonGF256::DataMatrixField) {
104     delete CBC_ReedSolomonGF256::DataMatrixField;
105     CBC_ReedSolomonGF256::DataMatrixField = NULL;
106   }
107   CBC_QRCoderMode::Destroy();
108   CBC_QRCoderErrorCorrectionLevel::Destroy();
109   CBC_QRDataMask::Destroy();
110   CBC_QRCoderVersion::Destroy();
111 }
112