Java converting Image to BufferedImage

From a Java Game Engine: /** * Converts a given Image into a BufferedImage * * @param img The Image to be converted * @return The converted BufferedImage */ public static BufferedImage toBufferedImage(Image img) { if (img instanceof BufferedImage) { return (BufferedImage) img; } // Create a buffered image with transparency BufferedImage bimage = new … Read more

How to setSize of image using RescaleOp

First filter(), as shown here, and then scale using drawImage() or AffineTransformOp, as shown here. Addendum: Alternatively, you can scale the image first (using either approach above) and then use your RescaleOp in drawImage(). As an aside, RescaleOp scales the image’s color bands; it does not change the image’s dimensions. To avoid confusion, dimensional scaling … Read more

How to scale a BufferedImage

AffineTransformOp offers the additional flexibility of choosing the interpolation type. BufferedImage before = getBufferedImage(encoded); int w = before.getWidth(); int h = before.getHeight(); BufferedImage after = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); AffineTransform at = new AffineTransform(); at.scale(2.0, 2.0); AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); after = scaleOp.filter(before, after); The fragment shown illustrates resampling, not cropping; this related … Read more

Java – get pixel array from image

I was just playing around with this same subject, which is the fastest way to access the pixels. I currently know of two ways for doing this: Using BufferedImage’s getRGB() method as described in @tskuzzy’s answer. By accessing the pixels array directly using: byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData(); If you are working with large images … Read more