Webcam with Scala

Hi
I am trying to use webcam using the Java library from a git - https://github.com/sarxos/webcam-capture.
When I try to run the Scala equivivalent of Java code, I am getting empty result.

JAVA CODE:
import com.github.sarxos.webcam.Webcam

public class TakePictureExample {

public static void main(String[] args) throws IOException{
    Webcam webcam = Webcam.getDefault();
    System.out.println(webcam);
    
    if (webcam != null) {
        System.out.println("Webcam: " + webcam.getName());
        webcam.open();
        ImageIO.write(webcam.getImage(), "PNG", new File("hello-world.png"));
        
    } else {
        System.out.println("No webcam detected");
    }
}

}

Returns:
[JavaFX Application Thread] INFO com.github.sarxos.webcam.Webcam - WebcamDefaultDriver capture driver will be used - "Webcam: FaceTime HD Camera CC253577UNXG1HNAL"
It works perfectly alright with Java.


However, if try the samething with scala as follows,
import com.github.sarxos.webcam.Webcam

Webcam webcam = Webcam.getDefault() // FaceTime HD Camera CC253577UNXG1HNAL	
println (webcam)

I get the result -

[JavaFX Application Thread] INFO com.github.sarxos.webcam.Webcam - WebcamDefaultDriver capture driver will be used.

In scala it should be

val webcam = Webcam.getDefault() // FaceTime HD Camera CC253577UNXG1HNAL	
println (webcam)

Generally your version should not compile, but in this case the interpretation as

Webcam.webcam = ...

may be accidentally valid if there is an object Webcam with a method webcam which has a setter.

best
Siddhartha

Thank you Siddhartha

I tried
val webcam = Webcam.getDefault() // FaceTime HD Camera CC253577UNXG1HNAL
println (webcam)

and

var webcam = Webcam.getDefault() // FaceTime HD Camera CC253577UNXG1HNAL	
println (webcam)

In both cases, it hangs in between. as follows

[JavaFX Application Thread] INFO com.github.sarxos.webcam.Webcam - WebcamDefaultDriver capture driver will be used.

The only thing I can think of (I don’t know java) is that you may need some java dependency in your classpath, which is injected at runtime - I got bitten this way once.

The code that does the rest of the printing that you see in Java is

System.out.println("Webcam: " + webcam.getName());

You don’t have the equivalent of that in your Scala code.

If that’s not it you might want to show the complete code that you’re trying to run and the exact output that you get. Seeing as your first Scala snippet wasn’t even valid Scala code I suspect you didn’t do that.

Thanks. I will try that too

Hi Jasper

Thanks for the reply. The Scala code I wrote was :
var webcam = Webcam.getDefault()
println (webcam)

It returns nothing but the following

JavaFX Application Thread] INFO com.github.sarxos.webcam.Webcam - WebcamDefaultDriver capture driver will be used


However, in Java code
Webcam webcam = Webcam.getDefault();
System.out.println(webcam);
it returns
Webcam FaceTime HD Camera CC253577UNXG1HNAL for

I just tried this, and here is code that seems to work in the ammonite REPL.

Welcome to the Ammonite Repl 1.0.1
(Scala 2.12.3 Java 1.8.0_101)
If you like Ammonite, please support our development at www.patreon.com/lihaoyi
@ import $ivy.`com.github.sarxos:webcam-capture:0.3.11` 
import $ivy.$                                        

@ import com.github.sarxos.webcam.Webcam 
import com.github.sarxos.webcam.Webcam

@ val webcam = Webcam.getDefault() 
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
webcam: Webcam = Webcam Venus USB2.0 Camera /dev/video0

@ println(webcam) 
Webcam Venus USB2.0 Camera /dev/video0

It captures images as well. I suggest you:

  • download ammonite REPL for scala experimentations
  • check your classpaths, and share them when you have questions.

regards,
Siddhartha

Excellent Siddhartha. I will try it… Cheers

Hi Siddhartha

It works in terminal as you have suggested. But not with SBT. In build.sbt I have added as follows…

libraryDependencies += “com.github.sarxos” % “webcam-capture” % “0.3.10”

It does not work with sbt. Any suggestion…?

Thanks Siddhartha for your wonderful suggestion.

This reminds me of an issue I had with the Java sound API. More specifically SBT has its own class loader that can wreak havoc with some libraries. Look at the following suggestion that may help you:

  1. Change the SBT build to for a process so that the default class loader is used
    See: https://stackoverflow.com/questions/18676712/java-sound-devices-found-when-run-in-intellij-but-not-in-sbt

  2. Change the class loaders dynamically when using the Java Sound API
    See: https://stackoverflow.com/questions/24145198/why-does-audiosystem-getmixerinfo-return-different-results-under-sbt-vs-scala
    https://stackoverflow.com/questions/31727385/sbt-scala-audiosystem

Also note that solution one did not solve the problem when using ScalaTest. You need solution (2) for that.

HTHs

Thank you HMF… I will try it too

Just a note here — things might move more quickly if you prepare an example project that demonstrates the issues you’re having and post the link to it on GitHub. :slight_smile: Cheers!

2 Likes