BarcodeDetectorTest.java 1.9 KB

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