The goal of this project is to allow integrated or USB-connected webcams to be accessed directly from Java. Using provided libraries users are able to read camera images and detect motion. Main project consist of several sub projects - the root one, which contains required classes, build-in webcam driver compatible with Windows, Linux and Mac OS, which can stream images as fast as your camera can serve them (up to 50 FPS). Main project can be used standalone, but user is able to replace build-in driver with different one - such as OpenIMAJ, GStreamer, V4L4j, JMF, LTI-CIVIL, FMJ, etc.
Done by implementing two additional methods in Webcam class:
public void setCustomViewSizes(Dimension[] sizes);
public Dimension[] getCustomViewSizes();
Code example:
Webcam webcam = Webcam.getDefault();
webcam.setCustomViewSizes(new Dimension[] {
WebcamDefaultDevice.SIZE_HD720, // standard HD 1280x720
WebcamDefaultDevice.SIZE_SXGA, // 1280x1024
});
webcam.setViewSize(WebcamDefaultDevice.SIZE_HD720);
WebcamUtils.capture(webcam, "test");
This will store 1280x720 px picture in test.png file located in current folder.
In case of predefined dimensions (up to 1024x768, which is XGA), there is a very high probability that all of them are supported by most modern webcams, but in case of higher ones, e.g. HD, one have to be aware that used camera has to support this specific resolution.
Rainy day scenario:
Let's assume that your webcam does not support some specific resolution (e.g. my HP HD Webcam does not support SXGA, but it supports HD720). In this case no error will be returned, but lower image resolution will be used instead. User can observe warning in log output:
22:06:03.734 [main] WARN c.g.s.w.d.b.WebcamDefaultDevice - Different size obtained vs requested - [1280x1024] vs [1280x720]. Setting correct one. New size is [1280x720]
However, in this case WebcamDefaultDevice behavior can be changed to throw exception when requested image size is different than actually received. Please note that not all drivers can handle unsupported resolutions. In case of default driver, Webcam Capture will simply use lower resolution, but in case of other one, some error can be generated, even in native layer.
If you don't use non-default driver, and you would like to have exception instead of log warning, you can configure this:
Exception in thread "main" com.github.sarxos.webcam.WebcamException: Different size obtained vs requested - [1280x1024] vs [1280x720]
at com.github.sarxos.webcam.ds.buildin.WebcamDefaultDevice.open(WebcamDefaultDevice.java:210)
at com.github.sarxos.webcam.Webcam.open(Webcam.java:116)
at com.github.sarxos.webcam.Webcam.getImage(Webcam.java:265)
at com.github.sarxos.webcam.WebcamUtils.capture(WebcamUtils.java:13)
at com.github.sarxos.webcam.WebcamUtils.capture(WebcamUtils.java:20)
at com.github.sarxos.webcam.ds.buildin.Test.main(Test.java:26)
Done by implementing two additional methods in
Webcam
class:Code example:
This will store 1280x720 px picture in
test.png
file located in current folder.In case of predefined dimensions (up to 1024x768, which is XGA), there is a very high probability that all of them are supported by most modern webcams, but in case of higher ones, e.g. HD, one have to be aware that used camera has to support this specific resolution.
Rainy day scenario:
Let's assume that your webcam does not support some specific resolution (e.g. my HP HD Webcam does not support SXGA, but it supports HD720). In this case no error will be returned, but lower image resolution will be used instead. User can observe warning in log output:
However, in this case
WebcamDefaultDevice
behavior can be changed to throw exception when requested image size is different than actually received. Please note that not all drivers can handle unsupported resolutions. In case of default driver, Webcam Capture will simply use lower resolution, but in case of other one, some error can be generated, even in native layer.If you don't use non-default driver, and you would like to have exception instead of log warning, you can configure this:
In this case you will get exception: