Skip to content

Commit

Permalink
Add image rotation example
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Jul 20, 2015
1 parent 36e1138 commit d93e386
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ Below are the very pretty basic examples demonstrating of how Webcam Capture API
* [How to display image from IP camera exposing MJPEG stream](https://github.com/sarxos/webcam-capture/blob/master/webcam-capture-drivers/driver-ipcam/src/examples/java/MjpegLignanoBeachExample.java)
* [How to use composite driver to display both, build-in and IP camera images](https://github.com/sarxos/webcam-capture/blob/master/webcam-capture-drivers/driver-ipcam/src/examples/java/DualNativeAndMjpegWebcamExample.java)
* [How to work with Raspberry Pi (camera module or UVC device)](https://github.com/sarxos/webcam-capture/wiki/How-To-Configure-Raspberry-Pi)
* [How to rotate the image from camera with ```WebcamImageTransformer```](https://github.com/sarxos/webcam-capture/blob/master/webcam-capture/src/example/java/ImageTransformerRotationExample.java)

And here are some more advanced examples, few with quite fancy GUI.

Expand Down
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@


/**
* A convenience class which implements those methods of BufferedImageOp which
* are rarely changed.
* A convenience class which implements those methods of BufferedImageOp which are rarely changed.
*/
public abstract class JHFilter implements BufferedImageOp, Cloneable {

@Override
public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
if (dstCM == null)
if (dstCM == null) {
dstCM = src.getColorModel();
}
return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()), dstCM.isAlphaPremultiplied(), null);
}

Expand All @@ -45,8 +45,9 @@ public Rectangle2D getBounds2D(BufferedImage src) {

@Override
public Point2D getPoint2D(Point2D srcPt, Point2D dstPt) {
if (dstPt == null)
if (dstPt == null) {
dstPt = new Point2D.Double();
}
dstPt.setLocation(srcPt.getX(), srcPt.getY());
return dstPt;
}
Expand All @@ -57,10 +58,9 @@ public RenderingHints getRenderingHints() {
}

/**
* A convenience method for getting ARGB pixels from an image. This tries to
* avoid the performance penalty of BufferedImage.getRGB unmanaging the
* image.
*
* A convenience method for getting ARGB pixels from an image. This tries to avoid the
* performance penalty of BufferedImage.getRGB unmanaging the image.
*
* @param image a BufferedImage object
* @param x the left edge of the pixel block
* @param y the right edge of the pixel block
Expand All @@ -72,16 +72,16 @@ public RenderingHints getRenderingHints() {
*/
public int[] getRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) {
int type = image.getType();
if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB)
if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB) {
return (int[]) image.getRaster().getDataElements(x, y, width, height, pixels);
}
return image.getRGB(x, y, width, height, pixels, 0, width);
}

/**
* A convenience method for setting ARGB pixels in an image. This tries to
* avoid the performance penalty of BufferedImage.setRGB unmanaging the
* image.
*
* A convenience method for setting ARGB pixels in an image. This tries to avoid the performance
* penalty of BufferedImage.setRGB unmanaging the image.
*
* @param image a BufferedImage object
* @param x the left edge of the pixel block
* @param y the right edge of the pixel block
Expand All @@ -92,10 +92,11 @@ public int[] getRGB(BufferedImage image, int x, int y, int width, int height, in
*/
public void setRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) {
int type = image.getType();
if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB)
if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB) {
image.getRaster().setDataElements(x, y, width, height, pixels);
else
} else {
image.setRGB(x, y, width, height, pixels, 0, width);
}
}

@Override
Expand Down
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";
}
}

0 comments on commit d93e386

Please sign in to comment.