mirror of
https://gitlab.com/cc-ru/ocelot/ocelot-desktop.git
synced 2026-03-25 08:52:47 +01:00
Disposing webcams on cleanup
This commit is contained in:
@@ -9,9 +9,11 @@
|
||||
</File>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<Root level="debug">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="MyFile"/>
|
||||
</Root>
|
||||
|
||||
<Logger name="com.github.sarxos.webcam" level="info"/>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
|
||||
@@ -120,6 +120,7 @@ object OcelotDesktop extends Logging {
|
||||
UiHandler.start()
|
||||
|
||||
logger.info("Cleaning up")
|
||||
WebcamCapture.cleanup()
|
||||
Settings.save(settingsFile)
|
||||
ResourceManager.freeResources()
|
||||
UiHandler.terminate()
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.awt.Color
|
||||
import java.awt.image.BufferedImage
|
||||
import scala.collection.mutable
|
||||
|
||||
object WebcamCapture extends Logging {
|
||||
object WebcamCapture extends {
|
||||
private final val frameTimeout: Long = 500
|
||||
private final val deviceTimeout: Long = 5000
|
||||
private final val maxDistance: Float = 32f
|
||||
@@ -16,30 +16,46 @@ object WebcamCapture extends Logging {
|
||||
def getInstance(name: String): WebcamCapture = instances.getOrElseUpdate(name, new WebcamCapture(name))
|
||||
def getInstance(webcam: Webcam): WebcamCapture = getInstance(webcam.getName)
|
||||
def getDefault: WebcamCapture = getInstance(Webcam.getDefault)
|
||||
|
||||
def cleanup(): Unit = {
|
||||
for (instance <- instances.values)
|
||||
instance.interrupt()
|
||||
}
|
||||
}
|
||||
|
||||
class WebcamCapture(webcamName: String) extends Thread {
|
||||
class WebcamCapture(webcamName: String) extends Thread with Logging {
|
||||
private val webcam: Webcam = Webcam.getWebcamByName(webcamName)
|
||||
private var frame: Option[BufferedImage] = None
|
||||
private var lastUsageTime: Long = -1
|
||||
start()
|
||||
|
||||
override def run(): Unit = {
|
||||
while (true) {
|
||||
if (System.currentTimeMillis() - lastUsageTime >= WebcamCapture.deviceTimeout) {
|
||||
webcam.close()
|
||||
synchronized {
|
||||
wait()
|
||||
logger.debug(s"Started thread for webcam '$name'")
|
||||
|
||||
while (!isInterrupted) {
|
||||
try {
|
||||
if (System.currentTimeMillis() - lastUsageTime >= WebcamCapture.deviceTimeout) {
|
||||
webcam.close()
|
||||
synchronized {
|
||||
wait()
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!webcam.isOpen)
|
||||
webcam.open()
|
||||
|
||||
frame = Option(webcam.getImage)
|
||||
Thread.sleep(WebcamCapture.frameTimeout)
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!webcam.isOpen)
|
||||
webcam.open()
|
||||
|
||||
frame = Option(webcam.getImage)
|
||||
Thread.sleep(WebcamCapture.frameTimeout)
|
||||
catch {
|
||||
case _: InterruptedException => interrupt()
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug(s"Cleaning up webcam '$name'")
|
||||
if (webcam.isOpen)
|
||||
webcam.close()
|
||||
}
|
||||
|
||||
def ray(x: Float, y: Float): Float = {
|
||||
|
||||
Reference in New Issue
Block a user