Skip to content

Commit

Permalink
More synchronization changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Feb 5, 2013
1 parent c6c3e55 commit 3cf7798
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.github.sarxos.webcam;

import java.awt.image.BufferedImage;
import java.util.concurrent.atomic.AtomicInteger;

import com.github.sarxos.webcam.log.WebcamLogConfigurator;


public class ConcurrentThreadsExample {

Expand All @@ -17,17 +20,32 @@ public Capture() {

@Override
public void run() {

Webcam webcam = Webcam.getDefault();
webcam.open();

while (true) {
Webcam.getDefault().getImage();
int value = counter.incrementAndGet();
if (value != 0 && value % 10 == 0) {
System.out.println(Thread.currentThread().getName() + ": Frames captured: " + value);

if (!webcam.isOpen()) {
break;
}

BufferedImage image = webcam.getImage();
if (image == null) {
break;
}

int n = counter.incrementAndGet();
if (n != 0 && n % 10 == 0) {
System.out.println(Thread.currentThread().getName() + ": Frames captured: " + n);
}
}
}
}

public static void main(String[] args) {
public static void main(String[] args) throws Throwable {

WebcamLogConfigurator.configure("src/example/resources/logback.xml");

/**
* This example will start several concurrent threads which use single
Expand All @@ -39,5 +57,9 @@ public static void main(String[] args) {
System.out.println("Thread: " + i);
new Capture().start();
}

Thread.sleep(10000);

System.exit(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.sarxos.webcam;

import java.awt.Dimension;
import java.awt.image.BufferedImage;


public class CustomResolutionExample {

public static void main(String[] args) {

Dimension[] nonStandardResolutions = new Dimension[] {
WebcamResolution.PAL.getSize(),
WebcamResolution.HD720.getSize(),
new Dimension(2000, 1000),
new Dimension(1000, 500),
};

Webcam webcam = Webcam.getDefault();
webcam.setCustomViewSizes(nonStandardResolutions);
webcam.open();

BufferedImage image = webcam.getImage();

System.out.println(image.getWidth() + "x" + image.getHeight());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class TakePictureDifferentSizeExample {

public static void main(String[] args) throws IOException {
Webcam webcam = Webcam.getDefault();
webcam.setViewSize(new Dimension(176, 144));
webcam.open();
webcam.setViewSize(new Dimension(1024, 768));
ImageIO.write(webcam.getImage(), "PNG", new File("test.png"));
webcam.close();
}
Expand Down
4 changes: 1 addition & 3 deletions webcam-capture/src/example/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<configuration scan="true" scanPeriod="30 seconds">

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>

<root level="warn">
<root level="trace">
<appender-ref ref="STDOUT" />
</root>
</configuration>
39 changes: 20 additions & 19 deletions webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,22 @@ public class Webcam {
*/
private static final class ShutdownHook extends Thread {

private static int number = 0;

/**
* Webcam instance to be disposed / closed.
*/
private Webcam webcam = null;

public ShutdownHook(Webcam webcam) {
super("shutdown-hook-" + (++number));
this.webcam = webcam;
}

@Override
public void run() {
LOG.info("Automatic {} deallocation", webcam.getName());
super.run();
webcam.dispose();
webcam.close0();
}
}

Expand Down Expand Up @@ -134,6 +135,7 @@ protected Webcam(WebcamDevice device) {
private void ensureSize() {

Dimension size = device.getSize();

if (size == null) {

Dimension[] sizes = device.getSizes();
Expand All @@ -155,12 +157,11 @@ private void ensureSize() {
public synchronized void open() {

if (open) {
LOG.debug("Webcam has already been open {}", getName());
return;
}

if (LOG.isInfoEnabled()) {
LOG.info("Opening webcam " + getName());
}
LOG.info("Opening webcam {}", getName());

ensureSize();

Expand Down Expand Up @@ -226,14 +227,14 @@ private void close0() {
*
* @return true if open, false otherwise
*/
public synchronized boolean isOpen() {
public boolean isOpen() {
return open;
}

/**
* @return Webcam view size (picture size) in pixels.
*/
public Dimension getViewSize() {
public synchronized Dimension getViewSize() {
return device.getSize();
}

Expand All @@ -243,7 +244,7 @@ public Dimension getViewSize() {
*
* @return
*/
public Dimension[] getViewSizes() {
public synchronized Dimension[] getViewSizes() {
return device.getSizes();
}

Expand Down Expand Up @@ -273,7 +274,7 @@ public Dimension[] getCustomViewSizes() {
* @see Webcam#setCustomViewSizes(Dimension[])
* @see Webcam#getViewSizes()
*/
public void setViewSize(Dimension size) {
public synchronized void setViewSize(Dimension size) {

if (size == null) {
throw new IllegalArgumentException("View size cannot be null!");
Expand Down Expand Up @@ -313,9 +314,7 @@ public void setViewSize(Dimension size) {
throw new IllegalArgumentException(sb.toString());
}

if (LOG.isDebugEnabled()) {
LOG.debug("Setting new view size {} x {}", size.width, size.height);
}
LOG.debug("Setting new view size {} x {}", size.width, size.height);

device.setSize(size);
}
Expand All @@ -325,17 +324,15 @@ public void setViewSize(Dimension size) {
*
* @return Captured image
*/
public BufferedImage getImage() {
public synchronized BufferedImage getImage() {

if (disposed) {
LOG.warn("Cannot get image - webcam has been already disposed");
return null;
}

if (!open) {
LOG.debug("Try to get image on closed webcam, opening it automatically");
synchronized (this) {
open();
}
open();
}

return device.getImage();
Expand Down Expand Up @@ -644,8 +641,11 @@ protected void dispose() {
}
}

open = false;
// make sure to dispose first !!
disposed = true;
open = false;

LOG.trace("Disposed flag set, open flag disabled");

WebcamEvent we = new WebcamEvent(this);
for (WebcamListener l : listeners) {
Expand All @@ -658,9 +658,10 @@ protected void dispose() {
}

synchronized (this) {
device.close();
device.dispose();
}

LOG.debug("Webcam disposed {}", getName());
}

/**
Expand Down
Loading

0 comments on commit 3cf7798

Please sign in to comment.