imageutil.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 71 行

JAVA
71
字号
package cn.js.fan.util;import java.awt.image.*;import java.io.*;import javax.imageio.*;public class ImageUtil {        public static BufferedImage getScaleImage(BufferedImage source,                                              int width, int height) {                BufferedImage image = new BufferedImage(width, height, source.getType());        image.createGraphics().drawImage(source, 0, 0, width, height, null);        return image;    }        public static BufferedImage getScaleImage(BufferedImage source,                                              double xscale, double yscale) {                return getScaleImage(source,                (int)(source.getWidth() * xscale), (int)(source.getHeight() * yscale));    }        public static boolean scaleImage(File input, File output,                                     int width, int height) throws IOException {        BufferedImage image = ImageIO.read(input);        if (image == null) { return false; }        image = getScaleImage(image, width, height);        String name = output.getName();        String format = name.substring(name.lastIndexOf('.')+1).toLowerCase();        return ImageIO.write(image, format, output);    }        public static boolean scaleImage(File input, File output,                                     double xscale, double yscale) throws IOException {        BufferedImage image = ImageIO.read(input);        if (image == null) { return false; }        image = getScaleImage(image, xscale, yscale);        String name = output.getName();        String format = name.substring(name.lastIndexOf('.')+1).toLowerCase();        return ImageIO.write(image, format, output);    }        public static boolean Image2Thumb(File input, File output,                                     int thumbWidth) throws IOException {       BufferedImage image = ImageIO.read(input);       if (image == null) {           return false;       }       int w = image.getWidth();       int h = image.getHeight();        double dHeight = ((double)thumbWidth)/w * h;        int height = (int)dHeight;        if (w>thumbWidth)            image = getScaleImage(image, thumbWidth, height);        else            image = getScaleImage(image, w, h);        String name = output.getName();        String format = name.substring(name.lastIndexOf('.')+1).toLowerCase();        return ImageIO.write(image, format, output);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?