Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Issue with OpenCV and Swing - Video Display

I've been messing around with OpenCV, and have an issue with displaying images through Swing.

I'm using Swing, and was running into an issue where my JFrame and JPanel would close after I'd finished reading my video. After a good amount of debugging, this issue appears to be an interaction between the OpenCV VideoCapture and the JFrame. Once VideoCapture reads an empty frame, the JFrame closes, regardless of whether it has any link to the VideoCapture. Here's the simplest code I could write which still shows the issue. When the three commented lines (excluding the while loop) below are commented, this runs fine, and the JFrame stays up until I close it. When the three commented lines below are uncommented, the JFrame closes as soon as the reader reads a blank frame.

Note that there's two while loop lines, that was on purpose to test. The video is 84 frames.

If anyone could run this and see if it happens for them too, I'd appreciate it (or just explain why this happens).

I'm using Scala in IntelliJ on a Mac.

class scalaTest {
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME)

def runTest() : Unit = {
  //val reader = new VideoCapture("src/main/resources/myVideo.mp4")
  val frm : JFrame = new JFrame("TestWindow")
  val lbl : JLabel = new JLabel()
  frm.add(lbl)
  frm.pack()
  frm.setVisible(true)
  frm.setSize(600,600)

  val imgIcon = new ImageIcon("src/main/resources/testimage.jpg")
  lbl.setIcon(imgIcon)
  var img : Mat = new Mat()

  var count : Int = 0

  //reader.read(img)
  //while (!img.empty()) {
  while (count <=250) {
    //reader.read(img)
    println(count)
    count += 1
  }
}