Java BufferedImage Operation optimizeImage(BufferedImage image)

Here you can find the source of optimizeImage(BufferedImage image)

Description

Optimize an image or create a copy of an image if the image was created by getBufferedImage()

License

Apache License

Declaration

public static BufferedImage optimizeImage(BufferedImage image) 

Method Source Code


//package com.java2s;
/* /*  w w w  .  ja v a  2s  .com*/
 * Copyright 2015 Torridity.
 *
 * 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.
 */

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;

import java.awt.GraphicsEnvironment;

import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

public class Main {
    /**Optimize an image or create a copy of an image if the image was created by getBufferedImage()*/
    public static BufferedImage optimizeImage(BufferedImage image) {
        // obtain the current system graphical settings
        GraphicsConfiguration gfx_config = GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getDefaultScreenDevice().getDefaultConfiguration();

        /*
         * if image is already compatible and optimized for current system
         * settings, simply return it
         */
        if (image.getColorModel().equals(gfx_config.getColorModel())) {
            return image;
        }

        // image is not optimized, so create a new image that is
        BufferedImage new_image = gfx_config.createCompatibleImage(image.getWidth(), image.getHeight(),
                image.getTransparency());

        // get the graphics context of the new image to draw the old image on
        Graphics2D g2d = (Graphics2D) new_image.getGraphics();

        // actually draw the image and dispose of context no longer needed
        g2d.drawRenderedImage(image, AffineTransform.getTranslateInstance(0, 0));
        g2d.dispose();

        // return the new optimized image
        return new_image;
    }
}

Related

  1. open(BufferedImage image)
  2. openAsBufferedImage(String path)
  3. operatedImage(BufferedImage source, BufferedImageOp op)
  4. optimizeForGraphicsHardware(BufferedImage image)
  5. optimizeForGraphicsHardwareIfRequired(BufferedImage image)
  6. otsuTreshold(BufferedImage original)
  7. output(BufferedImage image, OutputStream out)
  8. outPutImage(BufferedImage bufferedimage, String targetPath)
  9. paletteSwapARGB8(Color[] colorSet, Color clearToColorRequested, BufferedImage argbMappedBufferedImage)