image 工具类

import javax.imageio.ImageTypeSpecifier;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;

public class ImageUtil {
    public static BufferedImage getScaledInstanceClipMaxSide(BufferedImage bufferedImage, int width, int height) {
        BufferedImage bufferedImage1 = clipMaxSide(bufferedImage, width * 1.0 / height);
        return getScaledInstance(bufferedImage1, width, height);
    }

    public static BufferedImage clipMaxSide(BufferedImage bufferedImage, double widthHeightPct) {
        double curPct = bufferedImage.getWidth() * 1.0 / bufferedImage.getHeight();
        int targetWidth;
        int targetHeight;
        int x, y;
        if (curPct > widthHeightPct) {//更宽
            targetWidth = (int) (bufferedImage.getHeight() * widthHeightPct);
            targetHeight = bufferedImage.getHeight();
            x = (bufferedImage.getWidth() - targetWidth) / 2;
            y = 0;
        } else {//更高
            targetWidth = bufferedImage.getWidth();
            targetHeight = (int) (bufferedImage.getWidth() / widthHeightPct);
            x = 0;
            y = (bufferedImage.getHeight() - targetHeight) / 2;
        }
        return bufferedImage.getSubimage(x, y, targetWidth, targetHeight);
    }

    public static BufferedImage getScaledInstance(BufferedImage source, int width, int height) {
        BufferedImage target = ImageTypeSpecifier.createFromRenderedImage(source).createBufferedImage(width, height);
        Graphics g = target.getGraphics();
        g.drawImage(source, 0, 0, width, height, (ImageObserver) null);
        g.dispose();
        return target;
    }

    public static BufferedImage createThumbnail(BufferedImage image, int newWidth, int newHeight) {
        int width = image.getWidth();
        int height = image.getHeight();
        boolean isTranslucent = true;
        if (newWidth < width && newHeight < height) {
            if (newWidth > 0 && newHeight > 0) {
                BufferedImage thumb = image;
                BufferedImage temp = null;
                Graphics2D g2 = null;

                try {
                    int previousWidth = width;
                    int previousHeight = height;

                    do {
                        if (width > newWidth) {
                            width /= 2;
                            if (width < newWidth) {
                                width = newWidth;
                            }
                        }

                        if (height > newHeight) {
                            height /= 2;
                            if (height < newHeight) {
                                height = newHeight;
                            }
                        }

                        if (temp == null || isTranslucent) {
                            if (g2 != null) {
                                g2.dispose();
                            }

                            temp = new BufferedImage(width, height, 6);
                            g2 = temp.createGraphics();
                            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                        }

                        g2.drawImage(thumb, 0, 0, width, height, 0, 0, previousWidth, previousHeight, (ImageObserver) null);
                        previousWidth = width;
                        previousHeight = height;
                        thumb = temp;
                    } while (width != newWidth || height != newHeight);
                } finally {
                    if (g2 != null) {
                        g2.dispose();
                    }

                }

                if (width != thumb.getWidth() || height != thumb.getHeight()) {
                    temp = new BufferedImage(width, height, 6);
                    g2 = temp.createGraphics();

                    try {
                        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                        g2.drawImage(thumb, 0, 0, width, height, 0, 0, width, height, (ImageObserver) null);
                    } finally {
                        g2.dispose();
                    }

                    thumb = temp;
                }

                return thumb;
            } else {
                throw new IllegalArgumentException("newWidth and newHeight must be greater than 0");
            }
        } else {
            throw new IllegalArgumentException("newWidth and newHeight cannot be greater than the image dimensions");
        }
    }
}

 

posted @ 2024-07-26 17:40  知行IT讲堂  阅读(3)  评论(0编辑  收藏  举报