Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36440
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2017-04-12 08:20 -0700 |
| References | <871ssy10ub.fsf@Equus.decebal.nl> <ockuu9$oh6$1@dont-email.me> <87wpapzv42.fsf@Equus.decebal.nl> <ocl8os$qc7$1@dont-email.me> <oclfir$p3p$1@dont-email.me> |
| Message-ID | <2c781f24-6e80-4e00-b8db-cb507c2b2993@googlegroups.com> (permalink) |
| Subject | Re: Can you resize with BufferedImage |
| From | Eric Douglas <e.d.programmer@gmail.com> |
On Wednesday, April 12, 2017 at 11:06:08 AM UTC-4, Knute Johnson wrote:
> 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
I've never used an AffineTransformOp.
To change the size of an image I just generate a new image of the width/height/transparency I want. Then I get the Graphics of the new image, cast it to a Graphics2D, create a new AffineTransform, and set the translate/scale/rotate I want. Then I call .setComposite(AlphaComposite.SrcOver) on the new image's Graphics2D, then just .drawImage() passing in the old image, the AffineTransform I just created, and the observer (normally null).
Normally you want to maintain proportions, your x and y for the scale are both Math.min(newWidth / originalImageWidth, newHeight / originalImageHeight).
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Can you resize with BufferedImage Cecil Westerhof <Cecil@decebal.nl> - 2017-04-12 10:34 +0200
Re: Can you resize with BufferedImage Martin Gregorie <martin@address-in-sig.invalid> - 2017-04-12 10:18 +0000
Re: Can you resize with BufferedImage Cecil Westerhof <Cecil@decebal.nl> - 2017-04-12 14:08 +0200
Re: Can you resize with BufferedImage Martin Gregorie <martin@address-in-sig.invalid> - 2017-04-12 13:06 +0000
Re: Can you resize with BufferedImage Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-04-12 10:05 -0500
Re: Can you resize with BufferedImage Eric Douglas <e.d.programmer@gmail.com> - 2017-04-12 08:20 -0700
Re: Can you resize with BufferedImage Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-04-13 11:10 -0500
Re: Can you resize with BufferedImage Martin Gregorie <martin@address-in-sig.invalid> - 2017-04-12 16:13 +0000
csiph-web