ui_qrcode.cpp 7.0 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "components/ui_qrcode.h"
17
#include "qrcodegen.hpp"
Z
zhangguyuan 已提交
18
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include "securec.h"

using qrcodegen::QrCode;
namespace OHOS {
UIQrcode::UIQrcode()
    : width_(0), needDraw_(false), backgroundColor_(Color::White()), qrColor_(Color::Black()), qrcodeVal_(nullptr)
{
    style_ = &(StyleDefault::GetBackgroundTransparentStyle());
    imageInfo_ = {{0}};
}

UIQrcode::~UIQrcode()
{
    if (qrcodeVal_ != nullptr) {
        UIFree(qrcodeVal_);
        qrcodeVal_ = nullptr;
    }

    if (imageInfo_.data != nullptr) {
        ImageCacheFree(imageInfo_);
        imageInfo_.data = nullptr;
    }
}

void UIQrcode::SetQrcodeInfo(const char* val, ColorType backgroundColor, ColorType qrColor)
{
    if (val == nullptr) {
46
        GRAPHIC_LOGE("UIQrcode::SetQrcodeInfo val is null!\n");
M
mamingshuai 已提交
47 48 49 50
        return;
    }
    uint32_t length = static_cast<uint32_t>(strlen(val));
    if ((length > QRCODE_VAL_MAX) || (length == 0)) {
51
        GRAPHIC_LOGE("UIQrcode::SetQrcodeInfo val length is equal 0 or greater than QRCODE_VAL_MAX!\n");
M
mamingshuai 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        return;
    }
    backgroundColor_ = backgroundColor;
    qrColor_ = qrColor;
    SetQrcodeVal(val, length);
    RefreshQrcode();
}

void UIQrcode::RefreshQrcode()
{
    Invalidate();
    if (!needDraw_) {
        needDraw_ = true;
    }
}

void UIQrcode::SetWidth(int16_t width)
{
    if (GetWidth() != width) {
        UIView::SetWidth(width);
        RefreshQrcode();
    }
}

void UIQrcode::SetHeight(int16_t height)
{
    if (GetHeight() != height) {
        UIView::SetHeight(height);
        RefreshQrcode();
    }
}

void UIQrcode::ReMeasure()
{
    if (!needDraw_) {
        return;
    }
    needDraw_ = false;
    if (qrcodeVal_ == nullptr) {
91
        GRAPHIC_LOGE("UIQrcode::ReMeasure qrcodeVal_ is null!\n");
M
mamingshuai 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        return;
    }
    QrCode qr = QrCode::encodeText(qrcodeVal_, QrCode::Ecc::LOW);
    SetImageInfo(qr);
    SetSrc(&imageInfo_);
}

void UIQrcode::SetQrcodeVal(const char* qrcodeVal, uint32_t length)
{
    if (qrcodeVal_ != nullptr) {
        UIFree(qrcodeVal_);
        qrcodeVal_ = nullptr;
    }

    uint32_t len = static_cast<uint32_t>(length + 1);
    qrcodeVal_ = static_cast<char*>(UIMalloc(len));
    if (qrcodeVal_ != nullptr) {
        if (memcpy_s(qrcodeVal_, len, qrcodeVal, len) != EOK) {
            UIFree(reinterpret_cast<void*>(qrcodeVal_));
            qrcodeVal_ = nullptr;
        }
    }
}

void UIQrcode::SetImageInfo(qrcodegen::QrCode& qrcode)
{
    int16_t width = GetWidth();
    int16_t height = GetHeight();
    width_ = (width >= height) ? height : width;
    if (width_ < qrcode.getSize()) {
122
        GRAPHIC_LOGE("UIQrcode::SetImageInfo width is less than the minimum qrcode width!\n");
M
mamingshuai 已提交
123 124 125 126 127
        return;
    }
    imageInfo_.header.width = width;
    imageInfo_.header.height = height;
    imageInfo_.header.colorMode = ARGB8888;
128 129
    width = UI_ALIGN_UP(width);
    imageInfo_.dataSize = width * imageInfo_.header.height * QRCODE_FACTOR_NUM;
M
mamingshuai 已提交
130 131 132 133 134 135
    if (imageInfo_.data != nullptr) {
        ImageCacheFree(imageInfo_);
        imageInfo_.data = nullptr;
    }
    imageInfo_.data = reinterpret_cast<uint8_t*>(ImageCacheMalloc(imageInfo_));
    if (imageInfo_.data == nullptr) {
136
        GRAPHIC_LOGE("UIQrcode::SetImageInfo imageInfo_.data is null!\n");
M
mamingshuai 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
        return;
    }
    GenerateQrCode(qrcode);
}

void UIQrcode::GenerateQrCode(qrcodegen::QrCode& qrcode)
{
    FillQrCodeBackgroundColor();

    FillQrCodeColor(qrcode);
}

void UIQrcode::FillQrCodeColor(qrcodegen::QrCode& qrcode)
{
    int32_t qrWidth = qrcode.getSize();
    if (qrWidth <= 0) {
153
        GRAPHIC_LOGE("UIQrcode::FillQrCodeColor generated qrcode size is less or equal 0!\n");
M
mamingshuai 已提交
154 155 156 157 158 159 160 161
        return;
    }
    int16_t width = imageInfo_.header.width;
    int16_t height = imageInfo_.header.height;
    uint16_t outFilePixelPrescaler = width_ / qrWidth;
    int32_t offsetX = (width - outFilePixelPrescaler * qrWidth) / 2;    // 2: half
    int32_t offsetY = (height - outFilePixelPrescaler * qrWidth) / 2;   // 2: half

162
    width = UI_ALIGN_UP(width);
M
mamingshuai 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    uint8_t* destData = nullptr;
    int64_t oneLinePixel = width * QRCODE_FACTOR_NUM * outFilePixelPrescaler;
    int64_t oneLineOffsetPixel = (offsetY * width * QRCODE_FACTOR_NUM) + (offsetX * QRCODE_FACTOR_NUM);
    for (int32_t y = 0; y < qrWidth; ++y) {
        destData = const_cast<uint8_t*>(imageInfo_.data) + (oneLinePixel * y) + oneLineOffsetPixel;
        for (int32_t x = 0; x < qrWidth; ++x) {
            if (qrcode.getModule(x, y)) {
                GetDestData(destData, outFilePixelPrescaler);
            }
            destData += QRCODE_FACTOR_NUM * outFilePixelPrescaler;
        }
    }
}

void UIQrcode::FillQrCodeBackgroundColor()
{
    uint8_t* initColorData = const_cast<uint8_t*>(imageInfo_.data);
    *(initColorData + 0) = backgroundColor_.blue;  // 0: B channel
    *(initColorData + 1) = backgroundColor_.green; // 1: G channel
    *(initColorData + 2) = backgroundColor_.red;   // 2: R channel
    *(initColorData + 3) = OPA_OPAQUE;             // 3: Alpha channel

185 186 187
    uint32_t width = imageInfo_.header.width;
    width = UI_ALIGN_UP(width);

M
mamingshuai 已提交
188
    uint8_t* tempColorData = initColorData;
189
    for (int16_t col = 1; col < width; ++col) {
M
mamingshuai 已提交
190 191
        initColorData += QRCODE_FACTOR_NUM;
        if (memcpy_s(initColorData, QRCODE_FACTOR_NUM, tempColorData, QRCODE_FACTOR_NUM) != EOK) {
192
            GRAPHIC_LOGE("UIQrcode::FillQrCodeBackgroundColor memcpy_s failed!\n");
M
mamingshuai 已提交
193 194 195 196
            return;
        }
    }
    initColorData = tempColorData;
197
    int32_t deltaWidth = QRCODE_FACTOR_NUM * width;
M
mamingshuai 已提交
198 199 200
    for (int16_t row = 1; row < imageInfo_.header.height; ++row) {
        initColorData += deltaWidth;
        if (memcpy_s(initColorData, deltaWidth, tempColorData, deltaWidth) != EOK) {
201
            GRAPHIC_LOGE("UIQrcode::FillQrCodeBackgroundColor memcpy_s failed!\n");
M
mamingshuai 已提交
202 203 204 205 206 207 208
            return;
        }
    }
}

void UIQrcode::GetDestData(uint8_t* destData, int32_t outFilePixelPrescaler)
{
209 210 211
    uint32_t width = imageInfo_.header.width;
    width = UI_ALIGN_UP(width);

M
mamingshuai 已提交
212
    for (int32_t x = 0; x < outFilePixelPrescaler; ++x) {
213
        uint8_t* tempData = destData + width * QRCODE_FACTOR_NUM * x;
M
mamingshuai 已提交
214 215 216 217 218 219 220 221 222 223
        for (int32_t y = 0; y < outFilePixelPrescaler; ++y) {
            *(tempData + 0) = qrColor_.blue;  // 0: B channel
            *(tempData + 1) = qrColor_.green; // 1: G channel
            *(tempData + 2) = qrColor_.red;   // 2: R channel
            *(tempData + 3) = OPA_OPAQUE;     // 3: Alpha channel
            tempData += QRCODE_FACTOR_NUM;
        }
    }
}
} // namespace OHOS