BarcodeDetectorTest.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package org.opencv.test.barcode;
  2. import java.util.List;
  3. import org.opencv.core.Mat;
  4. import org.opencv.barcode.BarcodeDetector;
  5. import org.opencv.imgcodecs.Imgcodecs;
  6. import org.opencv.test.OpenCVTestCase;
  7. import java.util.ArrayList;
  8. import static org.opencv.barcode.Barcode.EAN_13;
  9. public class BarcodeDetectorTest extends OpenCVTestCase {
  10. private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
  11. private String testDataPath;
  12. @Override
  13. protected void setUp() throws Exception {
  14. super.setUp();
  15. testDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
  16. if (testDataPath == null)
  17. throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");
  18. }
  19. public void testDetectAndDecode() {
  20. Mat img = Imgcodecs.imread(testDataPath + "/cv/barcode/multiple/4_barcodes.jpg");
  21. assertFalse(img.empty());
  22. BarcodeDetector detector = new BarcodeDetector();
  23. assertNotNull(detector);
  24. List < String > infos = new ArrayList< String >();
  25. List < Integer > types = new ArrayList< Integer >();
  26. boolean result = detector.detectAndDecode(img, infos, types);
  27. assertTrue(result);
  28. assertEquals(infos.size(), 4);
  29. assertEquals(types.size(), 4);
  30. final String[] correctResults = {"9787122276124", "9787118081473", "9787564350840", "9783319200064"};
  31. for (int i = 0; i < 4; i++) {
  32. assertEquals(types.get(i).intValue(), EAN_13);
  33. result = false;
  34. for (int j = 0; j < 4; j++) {
  35. if (correctResults[j].equals(infos.get(i))) {
  36. result = true;
  37. break;
  38. }
  39. }
  40. assertTrue(result);
  41. }
  42. }
  43. }