Skip to content

Latest commit

 

History

History
219 lines (160 loc) · 9.97 KB

README.md

File metadata and controls

219 lines (160 loc) · 9.97 KB

Java Webcam Capture

This library allows you to use your build-in or external webcam directly from Java.

Complete documentation, API, examples, tutorials and many more can be found here:

http://webcam-capture.sarxos.pl/

Build Status

Features

  • Simple and thread-safe API,
  • No additional software required,
  • Supports multiple platforms (Windows, Linux, Mac OS, etc) and various architectures (32-bit, 64-bit, ARM),
  • Stream images from build-in or USB-connected PC webcams,
  • Stream images from IP / network cameras,
  • Detect motion,
  • All required JARs Available in Maven Central,
  • Can re-stream images as MJPEG,
  • Also available as standalone ZIP binaries with all dependencies included,
  • Supports additional video grabbing drivers (such as OpenIMAJ, LTI-CIVIL, JMF, FMJ, OpenCV, VLC, IP Camera),
  • Ready to use Swing component designed to display image from webcam / IP / network camera,

Maven

<dependency>
	<groupId>com.github.sarxos</groupId>
	<artifactId>webcam-capture</artifactId>
	<version>0.3.8</version>
</dependency>

If you are not using Maven, then here you can download ZIP containing all required 3rd-party JARs.

Contribution

If you have spare time, knownledge or some small amount of money you can help developing awesome Webcam Capture API and make it even better! Several kinds of contributions are very welcome:

  • Report bug or feature - If you've found a bug or you've came-up with some fantastic feature which can make Webcam Capture a better API to use, don't hesitate to create new issue where you can describe in details what the problem is, or what would you like to improve.
  • Perform tests - Since Webcam Capture use some part of native code, it's very hard to cover all supported operating systems. I'm always testing it on 64-bit Ubuntu Linux, Windows XP and Vista (both 32-bit), but I have no possibility to test on Raspberry Pi, Mac OS and 32-bit Linux. Please help and test on those systems if you have such possibility.
  • Write code - If you know Java or C++ you can help developing Webcam Capture by forking repository and sending pull requests.
  • Donate - People have expressed a wish to donate a little money. Donating won't get you anything special, other than a warm feeling inside, and possibly urge me to produce more freely available material for Webcam Capture project. You can donate via PayPal by sending money to songo.bercik@interia.pl, or by pressing donate button available here.

Examples

Some pretty basic examples.

Save Webcam Image In File

Code below will capture image from your PC webcam and save it in test.png file:

Webcam webcam = Webcam.getDefault();
BufferedImage image = webcam.getImage();
ImageIO.write(image, "PNG", new File("test.png"));

Display Webcam Image In JFrame

This one will display image from webcam in JFrame window:

JFrame window = new JFrame("Test webcam panel");
window.add(new WebcamPanel(Webcam.getDefault()));
window.pack();
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Take Picture From IP / Network Camera

This is simple example of how to use Webcam Capture with IP / network camera:

String address = "http://88.37.116.138/mjpg/video.mjpg ";
IpCamDevice livecam = new IpCamDevice("Lignano Beach", new URL(address), IpCamMode.PUSH);
IpCamDriver driver = new IpCamDriver();
driver.register(livecam);
Webcam.setDriver(driver);
Image image = Webcam.getDefault().getImage(); // live picture from Lignano beach (Italia)

For more detailed / complex examples of how to use Webcam Capture with IP / network cameras please follow to this subproject.

Detect Motion

This code will print appropriate message whenever motion si detected.

WebcamMotionDetector detector = new WebcamMotionDetector(Webcam.getDefault(), 25, 1000);
detector.setInterval(100);
detector.start();
while (true) {
    if (detector.isMotion()) {
    	System.out.printl("Motion detected!");
    }
    Thread.sleep(200);
}

More detailed motion detector example with some fancy GUI can be found in webcam-capture-motiondetector subproject.

Use Webcam In Applet

Example of how to enable Webcam Capture capabilities in Java Applet can be found in webcam-capture-applet subproject.

Custom Painter for WebcamPanel

You can very easily create your own image painter which can be used by WebcamPanel. Such custom painter can do fantastic things with image from camera, add some effects, filters, etc. Can also perform image analysis and display output in real-time.

To explore example of how to create simple custom painter, please follow to the webcam-capture-painter subproject. I'm sure you can find some fancy stuff there.

Read QR / DataMatrix / Bar Codes

Example presenting how to read QR / DataMatrix / Bar codes using Webcam Capture together with ZXing is available in webcam-capture-qrcode subproject.

BufferedImage image = webcam.getImage();
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

try {
	Result result = new MultiFormatReader().decode(bitmap);
	if (result != null) {
		System.out.println("QR code text: " + result.getText());
	}
} catch (NotFoundException e) {
	System.out.println("No QR code in camera view");
}

Timeout When Webcam Device Busy

Sometimes webcam device can be used by other process (e.g. Skype) or can be unavailable due to various issues. In such a case Java process will not be able to allocate it properly. To avoid application hang one can specify timeout parameter to be used in getWebcams(..) and getDefault(..) methods:

Example below will wait 5 seconds for device to be discovered and throw TimeoutException if it cannot be found in such time interval.

try {
	Webcam webcam = Webcam.getDefault(5, TimeUnit.SECONDS);
} catch (TimeoutException e) {
	System.err.println("Cannot find default webcam due to discovery timeout");
}

Addtional Drivers

Webcam Capture can utilize additional drivers to extend its own functionality. Currently below drivers are stable and available in Maven Central:

Stable but not available in Maven Central (due to 3rd-party dependencies not yet available in Central):

Unstable drivers (experimental stuff, can be dropped in future):

  • OpenCV Driver (JavaCV / OpenCV replacement for build-in webcam driver)
  • VLC Driver (VLC replacement for build-in webcam driver)

If no additional driver has been included in the classpath, then default one will be used instead. It consist of refined part of official OpenIMAJ code from hardware/core-video-capture module enhanced with multi-threading support which makes overral solution thread-safe.

License

Copyright (C) 2012 Bartosz Firyn

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

SarXos