Skip to content

Instantly share code, notes, and snippets.

@He-Pin
Created February 25, 2013 10:05
Show Gist options
  • Save He-Pin/5028876 to your computer and use it in GitHub Desktop.
Save He-Pin/5028876 to your computer and use it in GitHub Desktop.
package us.sosia.media.video;
import java.awt.image.BufferedImage;
public class ImageUtils {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
/**
* Convert a {@link BufferedImage} of any type, to {@link BufferedImage} of a
* specified type. If the source image is the same type as the target type,
* then original image is returned, otherwise new image of the correct type is
* created and the content of the source image is copied into the new image.
*
* @param sourceImage
* the image to be converted
* @param targetType
* the desired BufferedImage type
*
* @return a BufferedImage of the specifed target type.
*
* @see BufferedImage
*/
public static BufferedImage convertToType(BufferedImage sourceImage,
int targetType)
{
BufferedImage image;
// if the source image is already the target type, return the source image
if (sourceImage.getType() == targetType)
image = sourceImage;
// otherwise create a new image of the target type and draw the new
// image
else
{
image = new BufferedImage(sourceImage.getWidth(),
sourceImage.getHeight(), targetType);
image.getGraphics().drawImage(sourceImage, 0, 0, null);
}
return image;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment