Skip to content

Commit

Permalink
Add possibility to mirror webcam panel image, closes #227
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Jun 21, 2014
1 parent 4434560 commit 2071822
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 7 deletions.
2 changes: 2 additions & 0 deletions webcam-capture/src/example/java/WebcamPanelExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public static void main(String[] args) throws InterruptedException {
WebcamPanel panel = new WebcamPanel(webcam);
panel.setFPSDisplayed(true);
panel.setDisplayDebugInfo(true);
panel.setImageSizeDisplayed(true);
panel.setMirrored(true);

JFrame window = new JFrame("Test webcam panel");
window.add(panel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,21 @@ public static interface Painter {
*/
public class DefaultPainter implements Painter {

/**
* Webcam device name.
*/
private String name = null;

/**
* Lat repaint time, uset for debug purpose.
*/
private long lastRepaintTime = -1;

/**
* Buffered image resized to fit into panel drawing area.
*/
private BufferedImage resizedImage = null;

@Override
public void paintPanel(WebcamPanel owner, Graphics2D g2) {

Expand Down Expand Up @@ -226,10 +238,12 @@ public void paintImage(WebcamPanel owner, BufferedImage image, Graphics2D g2) {
break;
}

BufferedImage resized = null;
if (resizedImage != null) {
resizedImage.flush();
}

if (w == image.getWidth() && h == image.getHeight()) {
resized = image;
if (w == image.getWidth() && h == image.getHeight() && !mirrored) {
resizedImage = image;
} else {

GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
Expand All @@ -238,8 +252,8 @@ public void paintImage(WebcamPanel owner, BufferedImage image, Graphics2D g2) {
Graphics2D gr = null;
try {

resized = gc.createCompatibleImage(pw, ph);
gr = resized.createGraphics();
resizedImage = gc.createCompatibleImage(pw, ph);
gr = resizedImage.createGraphics();
gr.setComposite(AlphaComposite.Src);

for (Map.Entry<RenderingHints.Key, Object> hint : imageRenderingHints.entrySet()) {
Expand All @@ -249,6 +263,12 @@ public void paintImage(WebcamPanel owner, BufferedImage image, Graphics2D g2) {
gr.setBackground(Color.BLACK);
gr.setColor(Color.BLACK);
gr.fillRect(0, 0, pw, ph);

if (mirrored) {
x = x + w;
w = -w;
}

gr.drawImage(image, x, y, w, h, null);

} finally {
Expand All @@ -258,8 +278,7 @@ public void paintImage(WebcamPanel owner, BufferedImage image, Graphics2D g2) {
}
}

g2.drawImage(resized, 0, 0, null);
resized.flush();
g2.drawImage(resizedImage, 0, 0, null);

if (isFPSDisplayed()) {

Expand Down Expand Up @@ -647,6 +666,11 @@ private void update() {
*/
private boolean displayDebugInfo = false;

/**
* Is image mirrored.
*/
private boolean mirrored = false;

/**
* Creates webcam panel and automatically start webcam.
*
Expand Down Expand Up @@ -880,26 +904,61 @@ public void setFPSLimit(double fps) {
this.frequency = fps;
}

/**
* Is displaying of some debug information enabled.
*
* @return True if debug information are enabled, false otherwise
*/
public boolean isDisplayDebugInfo() {
return displayDebugInfo;
}

/**
* Display some debug information on image surface.
*
* @param displayDebugInfo the value to control debug information
*/
public void setDisplayDebugInfo(boolean displayDebugInfo) {
this.displayDebugInfo = displayDebugInfo;
}

/**
* This method return true in case if camera FPS is set to be displayed on
* panel surface. Default value returned is false.
*
* @return True if camera FPS is set to be displayed on panel surface
* @see #setFPSDisplayed(boolean)
*/
public boolean isFPSDisplayed() {
return frequencyDisplayed;
}

/**
* This method is to control if camera FPS should be displayed on the webcam
* panel surface.
*
* @param displayed the value to control if camera FPS should be displayed
*/
public void setFPSDisplayed(boolean displayed) {
this.frequencyDisplayed = displayed;
}

/**
* This method will return true in case when panel is configured to display
* image size. The string will be printed in the right bottom corner of the
* panel surface.
*
* @return True in case if panel is configured to display image size
*/
public boolean isImageSizeDisplayed() {
return imageSizeDisplayed;
}

/**
* Configure panel to display camera image size to be displayed.
*
* @param imageSizeDisplayed
*/
public void setImageSizeDisplayed(boolean imageSizeDisplayed) {
this.imageSizeDisplayed = imageSizeDisplayed;
}
Expand Down Expand Up @@ -1018,4 +1077,23 @@ public void webcamImageObtained(WebcamEvent we) {
// do nothing
}

/**
* This method returns true if image mirroring is enabled. The default value
* is false.
*
* @return True if image is mirrored, false otherwise
*/
public boolean isMirrored() {
return mirrored;
}

/**
* Decide whether or not the image from webcam painted on panel surface will
* be mirrored. The image from camera itself is not modified.
*
* @param mirrored the parameter to control if image should be mirrored
*/
public void setMirrored(boolean mirrored) {
this.mirrored = mirrored;
}
}

0 comments on commit 2071822

Please sign in to comment.