File: BarcodeDetectorTest.java

package info (click to toggle)
opencv 4.10.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 282,092 kB
  • sloc: cpp: 1,178,079; xml: 682,621; python: 49,092; lisp: 31,150; java: 25,469; ansic: 11,039; javascript: 6,085; sh: 1,214; cs: 601; perl: 494; objc: 210; makefile: 173
file content (55 lines) | stat: -rw-r--r-- 1,948 bytes parent folder | download
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
package org.opencv.test.barcode;

import java.util.List;
import org.opencv.core.Mat;
import org.opencv.objdetect.BarcodeDetector;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.test.OpenCVTestCase;
import java.util.ArrayList;

public class BarcodeDetectorTest extends OpenCVTestCase {

    private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
    private String testDataPath;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        // relys on https://developer.android.com/reference/java/lang/System
        isTestCaseEnabled = System.getProperties().getProperty("java.vm.name") != "Dalvik";

        if (isTestCaseEnabled) {
            testDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
            if (testDataPath == null)
                throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");
        }
    }

    public void testDetectAndDecode() {
        Mat img = Imgcodecs.imread(testDataPath + "/cv/barcode/multiple/4_barcodes.jpg");
        assertFalse(img.empty());
        BarcodeDetector detector = new BarcodeDetector();
        assertNotNull(detector);
        List < String > infos = new ArrayList< String >();
        List < String > types = new ArrayList< String >();

        boolean result = detector.detectAndDecodeWithType(img, infos, types);
        assertTrue(result);
        assertEquals(infos.size(), 4);
        assertEquals(types.size(), 4);
        final String[]  correctResults = {"9787122276124", "9787118081473", "9787564350840", "9783319200064"};
        for (int i = 0; i < 4; i++) {
            assertEquals(types.get(i), "EAN_13");
            result = false;
            for (int j = 0; j < 4; j++) {
                if (correctResults[j].equals(infos.get(i))) {
                    result = true;
                    break;
                }
            }
            assertTrue(result);
        }

    }
}