summaryrefslogtreecommitdiffstats
path: root/java/test/TestScanImage.java
blob: 86ed024c46ea3b4d530f11d0e621ec3882f56b4b (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import static org.junit.Assert.*;

import net.sourceforge.zbar.*;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.Iterator;

public class TestScanImage
{
    protected ImageScanner scanner;
    protected Image image;

    @Before public void setUp ()
    {
        scanner = new ImageScanner();
        image = new Image();
    }

    @After public void tearDown ()
    {
        image = null;
        scanner = null;
        System.gc();
    }

    public static final String encoded_widths =
        "9 111 212241113121211311141132 11111 311213121312121332111132 111 9";

    protected void generateY800 ()
    {
        int width = 114, height = 85;
        image.setSize(width, height);
        image.setFormat("Y800");
        int datalen = width * height;
        byte[] data = new byte[datalen];

        int y = 0;
        int p = 0;
        for(; y < 10 && y < height; y++)
            for(int x = 0; x < width; x++)
                data[p++] = -1;

        for(; y < height - 10; y++) {
            int x = 0;
            byte color = -1;
            CharacterIterator it = new StringCharacterIterator(encoded_widths);
            for(char c = it.first();
                c != CharacterIterator.DONE;
                c = it.next())
            {
                if(c == ' ')
                    continue;
                for(int dx = (int)c - 0x30; dx > 0; dx--) {
                    data[p++] = color;
                    x++;
                }
                color = (byte)~color;
            }
            for(; x < width; x++)
                data[p++] = (byte)~color;
        }

        for(; y < height; y++)
            for(int x = 0; x < width; x++)
                data[p++] = -1;
        assert(p == datalen);

        image.setData(data);
    }

    protected void checkResults (SymbolSet syms)
    {
        assertNotNull(syms);
        assert(syms.size() == 1);
        Iterator<Symbol> it = syms.iterator();
        assertTrue(it.hasNext());
        Symbol sym = it.next();
        assertNotNull(sym);
        assertFalse(it.hasNext());

        assertEquals(Symbol.EAN13, sym.getType());
        assertEquals(sym.EAN13, sym.getType()); // cached

        assertTrue(sym.getQuality() > 1);
        assertEquals(0, sym.getCount());

        SymbolSet comps = sym.getComponents();
        assertNotNull(comps);
        assertEquals(0, comps.size());
        it = comps.iterator();
        assertNotNull(it);
        assertFalse(it.hasNext());

        String data = sym.getData();
        assertEquals("6268964977804", data);

        byte[] bytes = sym.getDataBytes();
        byte[] exp = { '6','2','6','8','9','6','4','9','7','7','8','0','4' };
        assertArrayEquals(exp, bytes);

        int[] r = sym.getBounds();
        assertTrue(r[0] > 6);
        assertTrue(r[1] > 6);
        assertTrue(r[2] < 102);
        assertTrue(r[3] < 73);

        assertEquals(Orientation.UP, sym.getOrientation());
    }

    @Test public void generated ()
    {
        generateY800();
        int n = scanner.scanImage(image);
        assertEquals(1, n);

        checkResults(image.getSymbols());
        checkResults(scanner.getResults());
    }

    @Test public void config ()
    {
        generateY800();
        scanner.setConfig(Symbol.EAN13, Config.ENABLE, 0);
        int n = scanner.scanImage(image);
        assertEquals(0, n);
    }

    @Test public void cache ()
    {
        generateY800();
        scanner.enableCache(true);

        int n = 0;
        for(int i = 0; i < 10; i++) {
            n = scanner.scanImage(image);
            if(n > 0) {
                assertTrue(i > 1);
                break;
            }
        }

        assertEquals(1, n);
        checkResults(scanner.getResults());
    }

    @Test public void orientation()
    {
        generateY800();

        // flip the image
        int width = image.getWidth();
        int height = image.getHeight();
        byte[] data = image.getData();
        int p = 0;
        for(int y = 0; y < height; y++) {
            for(int x0 = 0; x0 < width / 2; x0++) {
                int x1 = width - x0 - 1;
                assert(x0 < x1);
                byte b = data[p + x0];
                data[p + x0] = data[p + x1];
                data[p + x1] = b;
            }
            p += width;
        }
        image.setData(data);

        int n = scanner.scanImage(image);
        assertEquals(1, n);

        SymbolSet syms = scanner.getResults();
        assert(syms.size() == 1);
        for(Symbol sym : syms) {
            assertEquals(Symbol.EAN13, sym.getType());
            assertEquals("6268964977804", sym.getData());
            assertEquals(Orientation.DOWN, sym.getOrientation());
        }
    }
}