Init Tizen 2.2.1
[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     if (__pQrCodeGeneratorImpl != null)
34     {
35         delete __pQrCodeGeneratorImpl;
36         __pQrCodeGeneratorImpl = null;
37     }
38 }
39
40 result
41 QrCodeGenerator::Construct(void)
42 {
43     SysAssertf(__pQrCodeGeneratorImpl == null, "Already constructed! ",
44             "Calling Construct() twice or more on a same instance is not allowed for this class.");
45
46     std::unique_ptr<_QrCodeGeneratorImpl> pQrCodeGeneratorImpl(new (std::nothrow) _QrCodeGeneratorImpl());
47     SysSecureTryReturn(NID_UIX, pQrCodeGeneratorImpl != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
48     __pQrCodeGeneratorImpl = pQrCodeGeneratorImpl.release();
49
50     return E_SUCCESS;
51 }
52
53 Tizen::Base::ByteBuffer*
54 QrCodeGenerator::EncodeToBufferN(const Tizen::Base::String& message, QrCodeMode mode, QrCodeErrorCorrectionLevel error_level, bool compatibility, int& width, int& height)
55 {
56     SysAssertf(__pQrCodeGeneratorImpl != null, "Not yet constructed! Consruct() should be called before use.");
57
58     SysSecureTryReturnResult(NID_UIX, !message.IsEmpty(), null,
59             "message must not be empty. [E_INVALID_ARG]");
60
61     Tizen::Text::Utf8Encoding utf8;
62     std::unique_ptr<Tizen::Base::ByteBuffer> pMessageBuffer(utf8.GetBytesN(message));
63     __pQrCodeGeneratorImpl->Encode((char*) pMessageBuffer->GetPointer(), mode, error_level, compatibility);
64     __pQrCodeGeneratorImpl->GetSize(width, height);
65
66     std::unique_ptr<Tizen::Base::ByteBuffer> pBuffer(new (std::nothrow) Tizen::Base::ByteBuffer());
67     SysSecureTryReturn(NID_UIX, pBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
68
69     pBuffer->Construct(width * height);
70
71     if (__pQrCodeGeneratorImpl->SaveToBuffer((unsigned char*) pBuffer->GetPointer()))
72     {
73         return pBuffer.release();
74     }
75     else
76     {
77         return null;
78     }
79
80 }
81
82 result
83 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)
84 {
85     result r =   E_SUCCESS;
86     SysAssertf(__pQrCodeGeneratorImpl != null, "Not yet constructed! Consruct() should be called before use.");
87
88     SysSecureTryReturnResult(NID_UIX, !message.IsEmpty(), E_INVALID_ARG,
89             "message must not be empty. [E_INVALID_ARG]");
90
91     SysSecureTryReturnResult(NID_UIX, !imageFilePath.IsEmpty(), E_INVALID_ARG,
92             "image path must not be empty. [E_INVALID_ARG]");
93
94     Tizen::Text::Utf8Encoding utf8;
95     std::unique_ptr<Tizen::Base::ByteBuffer> pBuffer(utf8.GetBytesN(message));
96     if (!__pQrCodeGeneratorImpl->Encode((char*) pBuffer->GetPointer(), mode, errorCorrectionLevel, compatibility))
97     {
98         return E_FAILURE;
99     }
100
101     if(!__pQrCodeGeneratorImpl->SaveToFile(imageFilePath, imageFormat,width, height))
102     {
103         r = GetLastResult();
104         if( r == E_ILLEGAL_ACCESS || r == E_OUT_OF_RANGE)
105         {
106                 r = E_FILE_NOT_FOUND;
107         }
108         else if (r == E_UNSUPPORTED_FORMAT)
109         {
110                 r = E_INVALID_ARG;
111         }
112         else
113         {
114                 r = E_FAILURE;
115         }
116
117     }
118
119     return r;
120 }
121
122 Tizen::Base::Collection::IListT<Tizen::Media::ImageFormat>*
123 QrCodeGenerator::GetSupportedImageFileFormatsListN(void)
124 {
125     std::unique_ptr<Tizen::Base::Collection::ArrayListT<Tizen::Media::ImageFormat> > pFormatList(
126             new (std::nothrow) Tizen::Base::Collection::ArrayListT<Tizen::Media::ImageFormat>);
127     SysSecureTryReturn(NID_UIX, pFormatList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
128
129
130     pFormatList->Add(Tizen::Media::IMG_FORMAT_BMP);
131     pFormatList->Add(Tizen::Media::IMG_FORMAT_JPG);
132     pFormatList->Add(Tizen::Media::IMG_FORMAT_PNG);
133     return pFormatList.release();
134 }
135
136 } } } //Tizen::Uix::Vision