summaryrefslogtreecommitdiff
path: root/mv_barcode/barcode_detector/src/BarcodeUtils.cpp
blob: 57219eb612ab581d2760fb7b2641668372923d84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
46
47
48
49
50
51
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * 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 "BarcodeUtils.h"
#include "mv_common_c.h"

#include <mv_private.h>

#include <zbar.h>

namespace MediaVision
{
namespace Barcode
{

int convertSourceMV2Zbar(mv_source_h mvSource, zbar::Image& zbarSource)
{
    int err = MEDIA_VISION_ERROR_NONE;
    unsigned char *buffer = NULL;
    unsigned int height = 0;
    unsigned int width = 0;
    unsigned int size = 0;
    mv_colorspace_e colorspace = MEDIA_VISION_COLORSPACE_INVALID;

    err = mv_source_get_colorspace_c(mvSource, &colorspace);
    if (err != MEDIA_VISION_ERROR_NONE)
    {
        LOGW("Can't determine mv_source_h colorspace to convert to ZBar colorspace. Conversion failed");
        return err;
    }

    switch(colorspace)
    {
        case MEDIA_VISION_COLORSPACE_Y800:
            zbarSource.set_format("Y800");
            break;
        case MEDIA_VISION_COLORSPACE_I420:
            zbarSource.set_format("I420");
            break;
        case MEDIA_VISION_COLORSPACE_NV12:
            zbarSource.set_format("NV12");
            break;
        case MEDIA_VISION_COLORSPACE_YV12:
            zbarSource.set_format("YV12");
            break;
        case MEDIA_VISION_COLORSPACE_NV21:
            zbarSource.set_format("NV21");
            break;
        case MEDIA_VISION_COLORSPACE_YUYV:
            zbarSource.set_format("YUYV");
            break;
        case MEDIA_VISION_COLORSPACE_UYVY:
            zbarSource.set_format("UYVY");
            break;
        case MEDIA_VISION_COLORSPACE_422P:
            zbarSource.set_format("422P");
            break;
        case MEDIA_VISION_COLORSPACE_RGB565:
            zbarSource.set_format("RGBP");
            break;
        case MEDIA_VISION_COLORSPACE_RGB888:
            zbarSource.set_format("RGB3");
            break;
        case MEDIA_VISION_COLORSPACE_RGBA:
            zbarSource.set_format("RGB4");
            break;
        default:
            LOGE("Media vision colorspace is not supported by ZBar symbol");
            return MEDIA_VISION_ERROR_NOT_SUPPORTED_FORMAT;
    }

    err = mv_source_get_buffer_c(mvSource, &buffer, &size);
    if (err != MEDIA_VISION_ERROR_NONE)
    {
        LOGW("Can't get mv_source_h buffer to convert to ZBar image. Conversion failed");
        return err;
    }

    err = mv_source_get_height_c(mvSource, &height);
    if (err != MEDIA_VISION_ERROR_NONE)
    {
        LOGW("Can't get mv_source_h height for conversion. Conversion failed");
        return err;
    }

    err = mv_source_get_width_c(mvSource, &width);
    if (err != MEDIA_VISION_ERROR_NONE)
    {
        LOGW("Can't get mv_source_h width for conversion. Conversion failed");
        return err;
    }

    zbarSource.set_size(width, height);
    zbarSource.set_data(buffer, size);

    return err;
}

} /* Barcode */
} /* MediaVision */