Skip to content

Commit

Permalink
Example for WebcamPanel Painter which rotates image, refs #581
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Nov 13, 2017
1 parent de05c76 commit 9a12ed2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamImageTransformer;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamPanel.DrawMode;
import com.github.sarxos.webcam.WebcamResolution;
import com.github.sarxos.webcam.util.jh.JHFlipFilter;

Expand All @@ -22,8 +23,8 @@ public class ImageTransformerRotationExample implements WebcamImageTransformer {
/**
* This is filter from JH Labs which flips buffered image 90 degrees clockwise. For more details
* please follow to the <a href="http://www.jhlabs.com/ip/filters/index.html">JH Labs Filters<a>
* home page (filters source code can be found <a
* href="https://github.com/axet/jhlabs">here</a>).
* home page (filters source code can be found
* <a href="https://github.com/axet/jhlabs">here</a>).
*/
private final BufferedImageOp filter = new JHFlipFilter(JHFlipFilter.FLIP_90CW);

Expand All @@ -49,7 +50,7 @@ public ImageTransformerRotationExample() {

WebcamPanel panel = new WebcamPanel(webcam);
panel.setFPSDisplayed(true);
panel.setFitArea(true);
panel.setDrawMode(DrawMode.FIT);

// add panel to window

Expand Down
54 changes: 54 additions & 0 deletions webcam-capture/src/example/java/WebcamPanelRotationExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamResolution;
import com.github.sarxos.webcam.util.jh.JHFlipFilter;


/**
* This example demonstrates how to rotate image in {@link WebcamPanel} without rotating original
* image which can be obtained with {@link Webcam#getImage()}.
*
* @author Bartosz Firyn (sarxos)
*
*/
public class WebcamPanelRotationExample {

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

final Webcam webcam = Webcam.getDefault();
webcam.setViewSize(WebcamResolution.QVGA.getSize());

final WebcamPanel panel = new WebcamPanel(webcam);
panel.setFPSDisplayed(true);
panel.setImageSizeDisplayed(true);

// you may wonder what is this below - why new operator is invoked on panel instance, so let
// me clarify this - since DefaultPainter class is an inner non-static class it has to be
// invoked from the panel context - this is because an instance of a non-static inner class
// holds a reference to it's owner object (the instance of the outer class that created it)

final WebcamPanel.Painter painter = panel.new DefaultPainter() {

final JHFlipFilter rotate = new JHFlipFilter(JHFlipFilter.FLIP_90CW);

@Override
public void paintImage(WebcamPanel owner, BufferedImage image, Graphics2D g2) {
super.paintImage(owner, rotate.filter(image, null), g2);
}
};

panel.setPainter(painter);

final JFrame window = new JFrame("Test Rotation");
window.add(panel);
window.setResizable(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
}
}

0 comments on commit 9a12ed2

Please sign in to comment.