⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imageutil.java

📁 spring+acegi编写的网上书城
💻 JAVA
字号:
package net.livebookstore.util;

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;

import org.apache.commons.logging.*;

import net.livebookstore.exception.ApplicationException;

/**
 * Create preview image.
 * 
 * @author Xuefeng
 */
public class ImageUtil {

    private static final int IMAGE_WIDTH = 80;
    private static final int IMAGE_HEIGHT = 100;

    private static Log log = LogFactory.getLog(ImageUtil.class);

    /**
     * Create a preview image from an Image-InputStream, and close the InputStream.
     */
    public static void createPreviewImage(InputStream srcInput, File destFile) {
        if(srcInput==null)
            throw new ApplicationException("无效的文件内容。");
        BufferedImage bis = null;
        try {
            bis = ImageIO.read(srcInput);
            if(bis==null)
                throw new ApplicationException("无法加载图像");
        }
        catch(IOException ioe) {
            throw new ApplicationException("无法加载图像:" + ioe.getMessage());
        }
        try {
            BufferedImage bid = null;
            bid = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bid.createGraphics();
            g.drawImage(bis, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, null);
            g.dispose();
            ImageIO.write(bid, "jpg", destFile);
        }
        catch(Exception e) {
            log.warn("Create preview image failed.", e);
        }
    }

}

⌨️ 快捷键说明

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