Skip to content

Commit

Permalink
Provide utility methods to capture image in specific format
Browse files Browse the repository at this point in the history
closes #15
  • Loading branch information
sarxos committed Jan 14, 2013
1 parent 958a8ec commit 6849d97
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 30 deletions.
Expand Up @@ -7,8 +7,6 @@
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.github.sarxos.webcam.ds.buildin.WebcamDefaultDevice;


@SuppressWarnings("serial")
public class ManyCamerasAtOnceExample extends JFrame {
Expand All @@ -21,7 +19,7 @@ public ManyCamerasAtOnceExample() {

for (Webcam webcam : Webcam.getWebcams()) {

webcam.setViewSize(WebcamDefaultDevice.SIZE_QVGA);
webcam.setViewSize(WebcamResolution.QVGA.getSize());
WebcamPanel panel = new WebcamPanel(webcam, false);

panels.add(panel);
Expand Down
@@ -1,5 +1,6 @@
package com.github.sarxos.webcam;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.util.concurrent.Executor;
Expand All @@ -9,7 +10,6 @@
import javax.swing.JFrame;
import javax.swing.JTextArea;

import com.github.sarxos.webcam.ds.buildin.WebcamDefaultDevice;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
Expand All @@ -36,15 +36,17 @@ public WebcamQRCodeExample() {
setTitle("Read QR / Bar Code With Webcam");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Dimension size = WebcamResolution.QVGA.getSize();

webcam = Webcam.getWebcams().get(0);
webcam.setViewSize(WebcamDefaultDevice.SIZE_QVGA);
webcam.setViewSize(size);

panel = new WebcamPanel(webcam);
panel.setPreferredSize(WebcamDefaultDevice.SIZE_QVGA);
panel.setPreferredSize(size);

textarea = new JTextArea();
textarea.setEditable(false);
textarea.setPreferredSize(WebcamDefaultDevice.SIZE_QVGA);
textarea.setPreferredSize(size);

add(panel);
add(textarea);
Expand Down
@@ -0,0 +1,54 @@
package com.github.sarxos.webcam;

import java.nio.ByteBuffer;

import com.github.sarxos.webcam.util.ImageUtils;


public class DifferentFileFormatsExample {

public static void main(String[] args) {

Webcam webcam = Webcam.getDefault();

// save files directly to disk

// creates test1.bmp
WebcamUtils.capture(webcam, "test1", ImageUtils.FORMAT_BMP);
// creates test1.gif
WebcamUtils.capture(webcam, "test1", ImageUtils.FORMAT_GIF);
// creates test1.jpg
WebcamUtils.capture(webcam, "test1", ImageUtils.FORMAT_JPG);
// creates test1.png
WebcamUtils.capture(webcam, "test1", ImageUtils.FORMAT_PNG);
// creates test1.wbmp
WebcamUtils.capture(webcam, "test1", ImageUtils.FORMAT_WBMP);

// this is equivalent of this code

// creates test2.bmp
WebcamUtils.capture(webcam, "test2", "bmp");
// creates test2.gif
WebcamUtils.capture(webcam, "test2", "gif");
// creates test2.jpg
WebcamUtils.capture(webcam, "test2", "jpg");
// creates test2.png
WebcamUtils.capture(webcam, "test2", "png");
// creates test2.wbmp
WebcamUtils.capture(webcam, "test2", "wbmp");

// NOTE !!!
// you can use any format you want until there is a ImageIO plugin
// installed which supports it

// get image as bytes array / bytes buffer

// save image in JPG format and return as array of bytes
byte[] bytes = WebcamUtils.getImageBytes(webcam, "jpg");
System.out.println("Bytes length: " + bytes.length);

// save image in JPG format and return as byte buffer
ByteBuffer buffer = WebcamUtils.getImageByteBuffer(webcam, "jpg");
System.out.println("Buffer length: " + buffer.capacity());
}
}
@@ -0,0 +1,31 @@
package com.github.sarxos.webcam;

import java.awt.Dimension;


public enum WebcamResolution {

QQVGA(176, 144),
QVGA(320, 240),
CIF(352, 288),
HVGA(480, 400),
VGA(640, 480),
PAL(768, 576),
SVGA(800, 600),
XGA(1024, 768),
HD720(1280, 720),
WXGA(1280, 768),
SXGA(1280, 1024),
UXGA(1600, 1200),
QXGA(2048, 1536);

private Dimension size = null;

private WebcamResolution(int width, int height) {
this.size = new Dimension(width, height);
}

public Dimension getSize() {
return size;
}
}
Expand Up @@ -2,22 +2,52 @@

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;

import javax.imageio.ImageIO;

import com.github.sarxos.webcam.util.ImageUtils;


public class WebcamUtils {

public static final void capture(Webcam webcam, File file) {
try {
ImageIO.write(webcam.getImage(), "PNG", file);
ImageIO.write(webcam.getImage(), ImageUtils.FORMAT_JPG, file);
} catch (IOException e) {
e.printStackTrace();
throw new WebcamException(e);
}
}

public static final void capture(Webcam webcam, File file, String format) {
try {
ImageIO.write(webcam.getImage(), format, file);
} catch (IOException e) {
throw new WebcamException(e);
}
}

public static final void capture(Webcam webcam, String filename) {
capture(webcam, new File(filename + ".png"));
if (filename.endsWith(".jpg")) {
filename = filename + ".jpg";
}
capture(webcam, new File(filename));
}

public static final void capture(Webcam webcam, String filename, String format) {
String ext = "." + format.toLowerCase();
if (!filename.startsWith(ext)) {
filename = filename + ext;
}
capture(webcam, new File(filename), format);
}

public static final byte[] getImageBytes(Webcam webcam, String format) {
return ImageUtils.toByteArray(webcam.getImage(), format);
}

public static final ByteBuffer getImageByteBuffer(Webcam webcam, String format) {
return ByteBuffer.wrap(getImageBytes(webcam, format));
}

}
Expand Up @@ -17,6 +17,7 @@

import com.github.sarxos.webcam.WebcamDevice;
import com.github.sarxos.webcam.WebcamException;
import com.github.sarxos.webcam.WebcamResolution;
import com.github.sarxos.webcam.ds.buildin.cgt.CloseSessionTask;
import com.github.sarxos.webcam.ds.buildin.cgt.GetImageTask;
import com.github.sarxos.webcam.ds.buildin.cgt.GetSizeTask;
Expand All @@ -33,20 +34,6 @@ public class WebcamDefaultDevice implements WebcamDevice {
}
}

public static final Dimension SIZE_QQVGA = new Dimension(176, 144);
public static final Dimension SIZE_QVGA = new Dimension(320, 240);
public static final Dimension SIZE_CIF = new Dimension(352, 288);
public static final Dimension SIZE_HVGA = new Dimension(480, 400);
public static final Dimension SIZE_VGA = new Dimension(640, 480);
public static final Dimension SIZE_PAL = new Dimension(768, 576);
public static final Dimension SIZE_SVGA = new Dimension(800, 600);
public static final Dimension SIZE_XGA = new Dimension(1024, 768);
public static final Dimension SIZE_HD720 = new Dimension(1280, 720);
public static final Dimension SIZE_WXGA = new Dimension(1280, 768);
public static final Dimension SIZE_SXGA = new Dimension(1280, 1024);
public static final Dimension SIZE_UXGA = new Dimension(1600, 1200);
public static final Dimension SIZE_QXGA = new Dimension(2048, 1536);

/**
* Logger.
*/
Expand All @@ -58,12 +45,12 @@ public class WebcamDefaultDevice implements WebcamDevice {
*/
// @formatter:off
private final static Dimension[] DIMENSIONS = new Dimension[] {
SIZE_QQVGA,
SIZE_QVGA,
SIZE_CIF,
SIZE_HVGA,
SIZE_VGA,
SIZE_XGA,
WebcamResolution.QQVGA.getSize(),
WebcamResolution.QVGA.getSize(),
WebcamResolution.CIF.getSize(),
WebcamResolution.HVGA.getSize(),
WebcamResolution.VGA.getSize(),
WebcamResolution.XGA.getSize(),
};
// @formatter:on

Expand Down
Expand Up @@ -2,10 +2,41 @@

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.github.sarxos.webcam.WebcamException;


public class ImageUtils {

/**
* Graphics Interchange Format.
*/
public static final String FORMAT_GIF = "GIF";

/**
* Portable Network Graphic format.
*/
public static final String FORMAT_PNG = "PNG";

/**
* Joint Photographic Experts Group format.
*/
public static final String FORMAT_JPG = "JPG";

/**
* Bitmap image format.
*/
public static final String FORMAT_BMP = "BMP";

/**
* Wireless Application Protocol Bitmap image format.
*/
public static final String FORMAT_WBMP = "WBMP";

public static BufferedImage premultiple(BufferedImage src) {
BufferedImage pre = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g2 = pre.createGraphics();
Expand All @@ -24,4 +55,31 @@ public static BufferedImage unpremultiple(BufferedImage pre) {
return src;
}

/**
* Convert {@link BufferedImage} to byte array.
*
* @param image the image to be converted
* @param format the output image format
* @return New array of bytes
*/
public static byte[] toByteArray(BufferedImage image, String format) {

byte[] bytes = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();

try {
ImageIO.write(image, format, baos);
bytes = baos.toByteArray();
} catch (IOException e) {
throw new WebcamException(e);
} finally {
try {
baos.close();
} catch (IOException e) {
throw new WebcamException(e);
}
}

return bytes;
}
}

0 comments on commit 6849d97

Please sign in to comment.