At last update to JUnit 4.x
[zxing.git] / core / test / src / com / google / zxing / oned / rss / expanded / RSSExpandedInternalTestCase.java
1 /*
2  * Copyright (C) 2010 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * These authors would like to acknowledge the Spanish Ministry of Industry,
19  * Tourism and Trade, for the support in the project TSI020301-2008-2
20  * "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
21  * Mobile Dynamic Environments", led by Treelogic
22  * ( http://www.treelogic.com/ ):
23  *
24  *   http://www.piramidepse.com/
25  */
26
27 package com.google.zxing.oned.rss.expanded;
28
29 import java.awt.image.BufferedImage;
30 import java.io.File;
31 import java.util.Vector;
32
33 import javax.imageio.ImageIO;
34
35 import com.google.zxing.BinaryBitmap;
36 import com.google.zxing.NotFoundException;
37 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
38 import com.google.zxing.common.BitArray;
39 import com.google.zxing.common.GlobalHistogramBinarizer;
40 import com.google.zxing.oned.rss.DataCharacter;
41 import com.google.zxing.oned.rss.FinderPattern;
42
43 import org.junit.Assert;
44 import org.junit.Test;
45
46 /**
47  * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
48  * @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es)
49  */
50 public final class RSSExpandedInternalTestCase extends Assert {
51
52   @Test
53   public void testFindFinderPatterns() throws Exception {
54     RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
55
56     String path = "test/data/blackbox/rssexpanded-1/2.jpg";
57     BufferedImage image = ImageIO.read(new File(path));
58     BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));
59     int rowNumber = binaryMap.getHeight() / 2;
60     BitArray row = binaryMap.getBlackRow(rowNumber, null);
61     Vector<ExpandedPair> previousPairs = new Vector<ExpandedPair>();
62
63     ExpandedPair pair1 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber);
64     previousPairs.add(pair1);
65     assertEquals(0, pair1.getFinderPattern().getValue());
66     assertFalse(pair1.mayBeLast());
67
68     ExpandedPair pair2 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber);
69     previousPairs.add(pair2);
70     assertEquals(1, pair2.getFinderPattern().getValue());
71     assertFalse(pair2.mayBeLast());
72
73     ExpandedPair pair3 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber);
74     previousPairs.add(pair3);
75     assertEquals(1, pair3.getFinderPattern().getValue());
76     assertTrue(pair3.mayBeLast());
77
78     try{
79       rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber);
80       //   the previous was the last pair
81       fail(NotFoundException.class.getName() + " expected");
82     }catch(NotFoundException nfe){
83       // ok
84     }
85   }
86
87   @Test
88   public void testRetrieveNextPairPatterns() throws Exception {
89     RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
90
91     String path = "test/data/blackbox/rssexpanded-1/3.jpg";
92     BufferedImage image = ImageIO.read(new File(path));
93     BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));
94     int rowNumber = binaryMap.getHeight() / 2;
95     BitArray row = binaryMap.getBlackRow(rowNumber, null);
96     Vector<ExpandedPair> previousPairs = new Vector<ExpandedPair>();
97
98     ExpandedPair pair1 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber);
99     previousPairs.add(pair1);
100     assertEquals(0, pair1.getFinderPattern().getValue());
101     assertFalse(pair1.mayBeLast());
102
103     ExpandedPair pair2 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber);
104     previousPairs.add(pair2);
105     assertEquals(0, pair2.getFinderPattern().getValue());
106     assertTrue(pair2.mayBeLast());
107   }
108
109   @Test
110   public void testDecodeCheckCharacter() throws Exception {
111     RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
112
113     String path = "test/data/blackbox/rssexpanded-1/3.jpg";
114     BufferedImage image = ImageIO.read(new File(path));
115     BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));
116     BitArray row = binaryMap.getBlackRow(binaryMap.getHeight() / 2, null);
117
118     int[] startEnd = {145, 243};//image pixels where the A1 pattern starts (at 124) and ends (at 214)
119     int value = 0;// A
120     FinderPattern finderPatternA1 = new FinderPattern(value, startEnd, startEnd[0], startEnd[1], image.getHeight() / 2);
121     //{1, 8, 4, 1, 1};
122     DataCharacter dataCharacter = rssExpandedReader.decodeDataCharacter(row, finderPatternA1, true, true);
123
124     assertEquals(98, dataCharacter.getValue());
125   }
126
127   @Test
128   public void testDecodeDataCharacter() throws Exception {
129     RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
130
131     String path = "test/data/blackbox/rssexpanded-1/3.jpg";
132     BufferedImage image = ImageIO.read(new File(path));
133     BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));
134     BitArray row = binaryMap.getBlackRow(binaryMap.getHeight() / 2, null);
135
136     int[] startEnd = {145, 243};//image pixels where the A1 pattern starts (at 124) and ends (at 214)
137     int value = 0; // A
138     FinderPattern finderPatternA1 = new FinderPattern(value, startEnd, startEnd[0], startEnd[1], image.getHeight() / 2);
139     //{1, 8, 4, 1, 1};
140     DataCharacter dataCharacter = rssExpandedReader.decodeDataCharacter(row, finderPatternA1, true, false);
141
142     assertEquals(19, dataCharacter.getValue());
143     assertEquals(1007, dataCharacter.getChecksumPortion());
144   }
145 }