-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example for WebcamPanel Painter which rotates image, refs #581
- Loading branch information
Showing
2 changed files
with
58 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
webcam-capture/src/example/java/WebcamPanelRotationExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |