Skip to content

Commit

Permalink
Add possibility to set view size + example
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Jul 8, 2012
1 parent 12ec5f0 commit 126e25c
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 8 deletions.
@@ -0,0 +1,26 @@
package com.github.sarxos.webcam;

import java.awt.Dimension;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


/**
* Set view size before taking picture. It's worth to mention that size has to
* be set before opening camera.
*
* @author Bartosz Firyn (SarXos)
*/
public class TakePictureDifferentSizeExample {

public static void main(String[] args) throws IOException {
Webcam webcam = Webcam.getDefault();
webcam.setViewSize(new Dimension(176, 144));
webcam.open();
ImageIO.write(webcam.getImage(), "PNG", new File("test.png"));
webcam.close();
}

}
Expand Up @@ -7,14 +7,12 @@


/**
* Proof of concept of how to handle webcam video stream from Java
*
* @author Bartosz Firyn (SarXos)
*/
public class TakePictureExample {

public static void main(String[] args) throws IOException {
Webcam webcam = Webcam.getWebcams().get(0);
Webcam webcam = Webcam.getDefault();
webcam.open();
ImageIO.write(webcam.getImage(), "PNG", new File("test.png"));
webcam.close();
Expand Down
@@ -0,0 +1,29 @@
package com.github.sarxos.webcam;

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


/**
* @author Bartosz Firyn (SarXos)
*/
public class TakePictureFromTwoCamsExample {

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

Webcam webcam1 = Webcam.getWebcams().get(0);
Webcam webcam2 = Webcam.getWebcams().get(1);

webcam1.open();
webcam2.open();

ImageIO.write(webcam1.getImage(), "PNG", new File("test1.png"));
ImageIO.write(webcam2.getImage(), "PNG", new File("test2.png"));

webcam1.close();
webcam2.close();
}

}
92 changes: 87 additions & 5 deletions src/main/java/com/github/sarxos/webcam/Webcam.java
Expand Up @@ -136,6 +136,7 @@ public void controllerUpdate(ControllerEvent ce) {

private PlayerStarter starter = null;
private Semaphore semaphore = new Semaphore(0, true);
private Dimension viewSize = null;

/**
* Webcam class.
Expand All @@ -146,6 +147,14 @@ public Webcam(CaptureDeviceInfo device) {
this.device = device;
}

private VideoFormat createFormat(Dimension size) {
if (size == null) {
return getLargestVideoFormat();
} else {
return getSizedVideoFormat(size);
}
}

/**
* Open webcam.
*/
Expand All @@ -159,7 +168,7 @@ public synchronized void open() {
LOG.debug("Opening webcam");

locator = device.getLocator();
format = getVideoFormat(device);
format = createFormat(viewSize);
converter = new BufferToImage(format);

try {
Expand Down Expand Up @@ -254,10 +263,83 @@ private Control getControl(String name) {
* @return Webcam view size (picture size) in pixels.
*/
public Dimension getViewSize() {
if (!isOpen()) {
throw new RuntimeException("Webcam has to be open to get video size");
if (viewSize == null) {
return getLargestVideoFormat().getSize();
}
return viewSize;
}

public Dimension[] getViewSizes() {

List<Dimension> dimensions = new ArrayList<Dimension>();

Format[] formats = device.getFormats();
for (Format format : formats) {
if ("RGB".equalsIgnoreCase(format.getEncoding())) {
VideoFormat rgb = (VideoFormat) format;
dimensions.add(rgb.getSize());
}
}
return format.getSize();
return dimensions.toArray(new Dimension[dimensions.size()]);
}

public void setViewSize(Dimension size) {

// check if dimension is valid one
boolean ok = false;
Dimension[] sizes = getViewSizes();
for (Dimension d : sizes) {
if (d.width == size.width && d.height == size.height) {
ok = true;
break;
}
}

if (!ok) {
StringBuilder sb = new StringBuilder("Incorrect dimension [");
sb.append(size.width).append("x").append(size.height).append("] ");
sb.append("possible ones are ");
for (Dimension d : sizes) {
sb.append("[").append(d.width).append("x").append(d.height).append("] ");
}
throw new IllegalArgumentException(sb.toString());
}

viewSize = size;
}

public static void main(String[] args) {
for (Dimension d : Webcam.getDefault().getViewSizes()) {
System.out.println(d);
}
}

/**
* Get video format for size.
*
* @param device device to get format from
* @param size specific size to search
* @return VideoFormat
*/
private VideoFormat getSizedVideoFormat(Dimension size) {

Format[] formats = device.getFormats();
VideoFormat format = null;

for (Format f : formats) {

if (!"RGB".equalsIgnoreCase(f.getEncoding()) || !(f instanceof VideoFormat)) {
continue;
}

Dimension d = ((VideoFormat) f).getSize();
if (d.width == size.width && d.height == size.height) {
format = (VideoFormat) f;
break;
}
}

return format;
}

/**
Expand All @@ -267,7 +349,7 @@ public Dimension getViewSize() {
* @param device device to get video format for
* @return Suitable video format
*/
protected VideoFormat getVideoFormat(CaptureDeviceInfo device) {
private VideoFormat getLargestVideoFormat() {

Format[] formats = device.getFormats();
VideoFormat format = null;
Expand Down

0 comments on commit 126e25c

Please sign in to comment.