Knute Johnson
unread,Apr 12, 2017, 11:06:08 AM4/12/17You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
On 4/12/2017 08:06, Martin Gregorie wrote:
If you find a good book, let us all know please.
I wrote a library to do a few common image mods so I don't have to
remember how to do it every time:
package com.knutejohnson.classes;
import java.awt.*;
import java.awt.color.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import javax.imageio.plugins.jpeg.*;
/**
* ImageUtilities is an assortment of static methods to manipulate
* BufferedImages
*
* @author Knute Johnson
*/
public class ImageUtilities {
/**
* Writes a BufferedImage to a JPEG file with the specified quality
*
* @param image BufferedImage to write
* @param quality image quality, a float value between 0.0f and 1.0f
* @param file destination File
*
* @throws IOException if an error occurs writing to the file
* @throws IllegalArgumentException if quality is not in the range
* 0.0f to 1.0f
*/
public static void writeJPEG(RenderedImage image, float quality,
File file)
throws IOException {
if (quality < 0.0f || quality > 1.0f)
throw new IllegalArgumentException("0.0 < Quality < 1.0");
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("JPEG");
if (!iter.hasNext())
throw new IOException("No Writers Available");
writer = (ImageWriter)iter.next();
if (file.exists())
file.delete();
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
writer.setOutput(ios);
JPEGImageWriteParam iwp = new JPEGImageWriteParam(null);
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality);
writer.write(null,new IIOImage(image,null,null),iwp);
ios.flush();
writer.dispose();
ios.close();
}
/**
* Converts a colored image to gray scale
*
* @param image source BufferedImage
* @param hints any rendering hints for the conversion or
{@code null}
*
* @return gray scale BufferedImage
*/
public static BufferedImage convertToGray(BufferedImage image,
RenderingHints hints) {
BufferedImage gray =
createCompatible(image.getWidth(),image.getHeight());
ColorConvertOp op = new ColorConvertOp(
image.getColorModel().getColorSpace(),
ColorSpace.getInstance(ColorSpace.CS_GRAY),hints);
return op.filter(image,gray);
}
/**
* Converts a color image to sepia tone
*
* @param image source image
* @param hints any rendering hints for the conversion or
{@code null}
*
* @return sepia tone image
*/
public static BufferedImage convertToSepia(BufferedImage image,
RenderingHints hints) {
BufferedImage sepia =
createCompatible(image.getWidth(),image.getHeight());
// convert to grayscale and back to compatible RGB
ColorSpace imgCS = image.getColorModel().getColorSpace();
ColorSpace grayCS = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorSpace sepiaCS = sepia.getColorModel().getColorSpace();
ColorConvertOp cop = new ColorConvertOp(imgCS,grayCS,hints);
image = cop.filter(image,null);
cop = new ColorConvertOp(grayCS,sepiaCS,hints);
sepia = cop.filter(image,null);
// slightly enhance the red, slightly reduce the green and
// remove half the blue
float[] factor = new float[] { 1.35f,1f,.7f };
float[] offset = new float[] { 0f,0f,0f };
RescaleOp rop = new RescaleOp(factor,offset,hints);
return rop.filter(sepia,null);
}
/**
* Scales a BufferedImage
*
* @param src source BufferedImage
* @param sx x scale factor
* @param sy y scale factor
* @param interpolationType the interpolation type
*
* @return scaled BufferedImage
*
* @see AffineTransformOp
*/
public static BufferedImage scaleImage(BufferedImage src, double sx,
double sy, int interpolationType) {
AffineTransformOp op = new AffineTransformOp(
AffineTransform.getScaleInstance(sx,sy),interpolationType);
return op.filter(src,null);
}
/**
* Scales a BufferedImage
*
* @param src source BufferedImage
* @param sx x scale factor
* @param sy y scale factor
* @param hints any rendering hints or {@code null}
*
* @return scaled BufferedImage
*
* @see RenderingHints
*/
public static BufferedImage scaleImage(BufferedImage src, double sx,
double sy, RenderingHints hints) {
AffineTransformOp op = new AffineTransformOp(
AffineTransform.getScaleInstance(sx,sy),hints);
return op.filter(src,null);
}
/**
* Creates a compatible BufferedImage with the specified dimensions
*
* @param width width of the image
* @param height height of the image
*
* @return compatible BufferedImage
*/
public static BufferedImage createCompatible(int width, int height) {
GraphicsConfiguration gc = getDefaultConfiguration();
return gc.createCompatibleImage(width,height);
}
/**
* Creates a compatible BufferedImage with the specified dimensions and
* transparency
*
* @param width image width
* @param height image height
* @param transparency transparency of the image
*
* @return compatible BufferedImage
*
* @see BufferedImage
* @see Transparency
*/
public static BufferedImage createCompatible(int width, int height,
int transparency) {
GraphicsConfiguration gc = getDefaultConfiguration();
return gc.createCompatibleImage(width,height,transparency);
}
/**
* Converts an existing image to a compatible ColorModel
*
* @param image source BufferedImage
* @param hints any RenderingHints or {@code null}
*
* @return compatible BufferedImage
*
* @see ColorModel
* @see RenderingHints
*/
public static BufferedImage convertToCompatible(BufferedImage image,
RenderingHints hints) {
if (isCompatible(image))
return image;
ColorConvertOp op = new ColorConvertOp(
image.getColorModel().getColorSpace(),
getDefaultConfiguration().getColorModel().getColorSpace(),hints);
return op.filter(image,null);
}
/**
* Converts an existing BufferedImage to a compatible ColorModel
*
* @param image source BufferedImage
*
* @return compatible BufferedImage
*/
public static BufferedImage convertToCompatible(BufferedImage image) {
return convertToCompatible(image,null);
}
/**
* Checks if a BufferedImage is compatible
*
* @param src source BufferedImage
*
* @return {@code true} if image is compatible
*/
public static boolean isCompatible(BufferedImage src) {
GraphicsConfiguration gc = getDefaultConfiguration();
return src.getColorModel().equals(gc.getColorModel());
}
/**
* Gets the default graphics configuration. Used by several methods in
* this class.
*
* @return default graphics configuration
*/
public static GraphicsConfiguration getDefaultConfiguration() {
return GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().getDefaultConfiguration();
}
}
--
Knute Johnson