Coding idoms applied. isActive functional added.
[framework/osp/vision.git] / src / FUixVisionQrCodeGenerator.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <FUixVisionQrCodeGenerator.h>
19 #include "FUixVision_QrCodeGeneratorImpl.h"
20 #include <FText.h>
21 #include <FBaseSysLog.h>
22
23 namespace Tizen { namespace Uix { namespace Vision
24 {
25
26 QrCodeGenerator::QrCodeGenerator(void)
27     : __pQrCodeGeneratorImpl(0)
28 {
29 }
30
31 QrCodeGenerator::~QrCodeGenerator(void)
32 {
33 }
34
35 result
36 QrCodeGenerator::Construct(void)
37 {
38     SysAssertf(__pQrCodeGeneratorImpl == null, "Already constructed! ",
39             "Calling Construct() twice or more on a same instance is not allowed for this class.");
40
41     std::unique_ptr<_QrCodeGeneratorImpl> pQrCodeGeneratorImpl(new (std::nothrow) _QrCodeGeneratorImpl());
42     SysTryReturn(NID_UIX, pQrCodeGeneratorImpl != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
43     __pQrCodeGeneratorImpl = pQrCodeGeneratorImpl.release();
44
45     return E_SUCCESS;
46 }
47
48 Tizen::Base::ByteBuffer*
49 QrCodeGenerator::EncodeToBufferN(const Tizen::Base::String& message, QrCodeMode mode, QrCodeErrorCorrectionLevel error_level, bool compatibility, int& width, int& height)
50 {
51     SysAssertf(__pQrCodeGeneratorImpl != null, "Not yet constructed! Consruct() should be called before use.");
52
53     SysTryReturnResult(NID_UIX, !message.IsEmpty(), null,
54             "message must not be empty. [E_INVALID_ARG]");
55
56     Tizen::Text::Utf8Encoding utf8;
57     __pQrCodeGeneratorImpl->Encode((char*) utf8.GetBytesN(message)->GetPointer(), mode, error_level, compatibility);
58     __pQrCodeGeneratorImpl->GetSize(width, height);
59
60     Tizen::Base::ByteBuffer* buffer = new Tizen::Base::ByteBuffer;
61     buffer->Construct(width * height);
62
63     if (__pQrCodeGeneratorImpl->SaveToBuffer((unsigned char*) buffer->GetPointer()))
64     {
65         return buffer;
66     }
67     else
68     {
69         delete buffer;
70         return null;
71     }
72
73 }
74
75 result
76 QrCodeGenerator::EncodeToFile(const Tizen::Base::String& message, QrCodeMode mode, QrCodeErrorCorrectionLevel errorCorrectionLevel, bool compatibility, const Tizen::Base::String& imageFilePath, Tizen::Media::ImageFormat imageFormat, int& width, int& height)
77 {
78     SysAssertf(__pQrCodeGeneratorImpl != null, "Not yet constructed! Consruct() should be called before use.");
79
80      SysTryReturnResult(NID_UIX, !message.IsEmpty(), E_INVALID_ARG,
81              "message must not be empty. [E_INVALID_ARG]");
82
83      SysTryReturnResult(NID_UIX, !imageFilePath.IsEmpty(), E_INVALID_ARG,
84              "image path must not be empty. [E_INVALID_ARG]");
85
86      Tizen::Text::Utf8Encoding utf8;
87      if (!__pQrCodeGeneratorImpl->Encode((char*) utf8.GetBytesN(message)->GetPointer(), mode, errorCorrectionLevel, compatibility))
88      {
89          return E_FAILURE;
90      }
91
92      return __pQrCodeGeneratorImpl->SaveToFile(imageFilePath, imageFormat) ? E_SUCCESS : E_FAILURE;
93 }
94
95 Tizen::Base::Collection::IListT<Tizen::Media::ImageFormat>*
96 QrCodeGenerator::GetSupportedImageFileFormatsListN(void)
97 {
98     Tizen::Base::Collection::ArrayListT<Tizen::Media::ImageFormat>* formatList = new Tizen::Base::Collection::ArrayListT<Tizen::Media::ImageFormat>;
99     formatList->Add(Tizen::Media::IMG_FORMAT_BMP);
100     formatList->Add(Tizen::Media::IMG_FORMAT_JPG);
101     formatList->Add(Tizen::Media::IMG_FORMAT_PNG);
102     return formatList;
103 }
104
105 } } } //Tizen::Uix::Vision