-
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.
- Loading branch information
Showing
4 changed files
with
244 additions
and
15 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
73 changes: 73 additions & 0 deletions
73
webcam-capture/src/example/java/ImageTransformerRotationExample.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,73 @@ | ||
import java.awt.Dimension; | ||
import java.awt.image.BufferedImage; | ||
import java.awt.image.BufferedImageOp; | ||
|
||
import javax.swing.JFrame; | ||
|
||
import com.github.sarxos.webcam.Webcam; | ||
import com.github.sarxos.webcam.WebcamImageTransformer; | ||
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 use {@link WebcamImageTransformer} to rotate the image from | ||
* camera by using {@link JHFlipFilter} from Jerry Huxtable filters package. | ||
* | ||
* @author Bartosz Firyn (sarxos) | ||
*/ | ||
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>). | ||
*/ | ||
private final BufferedImageOp filter = new JHFlipFilter(JHFlipFilter.FLIP_90CW); | ||
|
||
public ImageTransformerRotationExample() { | ||
|
||
// use VGA resolution | ||
|
||
Dimension size = WebcamResolution.VGA.getSize(); | ||
|
||
// get default webcam and set image transformer to this (transformer will modify image after | ||
// it's received from webcam, in this case it will rotate it) | ||
|
||
Webcam webcam = Webcam.getDefault(); | ||
webcam.setViewSize(size); | ||
webcam.setImageTransformer(this); | ||
webcam.open(); | ||
|
||
// create window | ||
|
||
JFrame window = new JFrame("Test Rotation"); | ||
|
||
// and webcam panel | ||
|
||
WebcamPanel panel = new WebcamPanel(webcam); | ||
panel.setFPSDisplayed(true); | ||
panel.setFitArea(true); | ||
|
||
// add panel to window | ||
|
||
window.add(panel); | ||
window.pack(); | ||
window.setVisible(true); | ||
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
} | ||
|
||
@Override | ||
public BufferedImage transform(BufferedImage image) { | ||
|
||
// this will do rotation on image | ||
|
||
return filter.filter(image, null); | ||
} | ||
|
||
public static void main(String[] args) { | ||
new ImageTransformerRotationExample(); | ||
} | ||
} |
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
154 changes: 154 additions & 0 deletions
154
webcam-capture/src/main/java/com/github/sarxos/webcam/util/jh/JHFlipFilter.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,154 @@ | ||
/* | ||
Copyright 2006 Jerry Huxtable | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package com.github.sarxos.webcam.util.jh; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.awt.image.ColorModel; | ||
|
||
|
||
/** | ||
* A filter which flips images or rotates by multiples of 90 degrees. | ||
*/ | ||
public class JHFlipFilter extends JHFilter { | ||
|
||
/** | ||
* Rotate the image 90 degrees clockwise. | ||
*/ | ||
public static final int FLIP_90CW = 4; | ||
|
||
/** | ||
* Rotate the image 90 degrees counter-clockwise. | ||
*/ | ||
public static final int FLIP_90CCW = 5; | ||
|
||
/** | ||
* Rotate the image 180 degrees. | ||
*/ | ||
public static final int FLIP_180 = 6; | ||
|
||
private int operation; | ||
|
||
/** | ||
* Construct a FlipFilter which flips horizontally and vertically. | ||
*/ | ||
public JHFlipFilter() { | ||
this(FLIP_90CW); | ||
} | ||
|
||
/** | ||
* Construct a FlipFilter. | ||
* | ||
* @param operation the filter operation | ||
*/ | ||
public JHFlipFilter(int operation) { | ||
this.operation = operation; | ||
} | ||
|
||
/** | ||
* Set the filter operation. | ||
* | ||
* @param operation the filter operation | ||
* @see #getOperation | ||
*/ | ||
public void setOperation(int operation) { | ||
this.operation = operation; | ||
} | ||
|
||
/** | ||
* Get the filter operation. | ||
* | ||
* @return the filter operation | ||
* @see #setOperation | ||
*/ | ||
public int getOperation() { | ||
return operation; | ||
} | ||
|
||
@Override | ||
public BufferedImage filter(BufferedImage src, BufferedImage dst) { | ||
int width = src.getWidth(); | ||
int height = src.getHeight(); | ||
|
||
int[] inPixels = getRGB(src, 0, 0, width, height, null); | ||
|
||
int w = width; | ||
int h = height; | ||
|
||
int newW = w; | ||
int newH = h; | ||
switch (operation) { | ||
case FLIP_90CW: | ||
newW = h; | ||
newH = w; | ||
break; | ||
case FLIP_90CCW: | ||
newW = h; | ||
newH = w; | ||
break; | ||
case FLIP_180: | ||
break; | ||
} | ||
|
||
int[] newPixels = new int[newW * newH]; | ||
|
||
for (int row = 0; row < h; row++) { | ||
for (int col = 0; col < w; col++) { | ||
int index = row * width + col; | ||
int newRow = row; | ||
int newCol = col; | ||
switch (operation) { | ||
case FLIP_90CW: | ||
newRow = col; | ||
newCol = h - row - 1; | ||
; | ||
break; | ||
case FLIP_90CCW: | ||
newRow = w - col - 1; | ||
newCol = row; | ||
break; | ||
case FLIP_180: | ||
newRow = h - row - 1; | ||
newCol = w - col - 1; | ||
break; | ||
} | ||
int newIndex = newRow * newW + newCol; | ||
newPixels[newIndex] = inPixels[index]; | ||
} | ||
} | ||
|
||
if (dst == null) { | ||
ColorModel dstCM = src.getColorModel(); | ||
dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(newW, newH), dstCM.isAlphaPremultiplied(), null); | ||
} | ||
setRGB(dst, 0, 0, newW, newH, newPixels); | ||
|
||
return dst; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
switch (operation) { | ||
case FLIP_90CW: | ||
return "Rotate 90"; | ||
case FLIP_90CCW: | ||
return "Rotate -90"; | ||
case FLIP_180: | ||
return "Rotate 180"; | ||
} | ||
return "Flip"; | ||
} | ||
} |