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

📄 imageutils.java

📁 动态网站管理发布系统
💻 JAVA
字号:
package com.ntsky.news;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Date;

//import org.apache.commons.io.FileUtils;

/**
 * Created by IntelliJ IDEA.
 * User: breezee
 * Date: 2006-11-21
 * Time: 15:03:56
 * Mail: <a href=mailto:yellowemail@gmail.com>Breezee</a>
 * To change this template use File | Settings | File Templates.
 */
public class ImageUtils {
    public static void resizeImage(String filename, String tagfile, float widthSize, float heightSize) {//这里的path是传入你缩略图输出地址
        try {
            FileOutputStream fos = new FileOutputStream(tagfile);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);

            File _file = new File(filename);                    //读入文件
            Image image = javax.imageio.ImageIO.read(_file);                     //构造Image对象
            float width = image.getWidth(null);                                     //得到源图宽
            float height = image.getHeight(null);                                    //得到源图长
            float tagWidthSize = 0;
            float tagHeightSize = 0;
            System.out.println("width=" + width + ", height=" + height);

            if (width > 0 && height > 0) {
                if (width / height >= widthSize / heightSize) {
                    if (width > widthSize) {
                        tagWidthSize = widthSize;
                        tagHeightSize = (height * widthSize) / width;
                    } else {
                        tagWidthSize = width;
                        tagHeightSize = height;
                    }
                } else {
                    if (height > heightSize) {
                        tagHeightSize = heightSize;
                        tagWidthSize = (width * heightSize) / height;
                    } else {
                        tagWidthSize = width;
                        tagHeightSize = height;
                    }
                }
            }
            System.out.println("tagHeightSize = " + tagHeightSize);
            System.out.println("tagWidthSize = " + tagWidthSize);

            int new_w = Math.round(tagWidthSize);
            int new_h = Math.round(tagHeightSize);//计算新图长宽
            System.out.println("new_w=" + new_w + ", new_h=" + new_h);

            BufferedImage tag = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(image, 0, 0, new_w, new_h, null);       //绘制缩小后的图
            encoder.encode(tag);
            bos.close();
        } catch (FileNotFoundException fnfe) {
            System.out.println(fnfe);
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }
  public static void resizeImage(String filename, String tagfile, float tagsize) {//这里的path是传入你缩略图输出地址
        try {
            FileOutputStream fos = new FileOutputStream(tagfile);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);

            File _file = new File(filename);                    //读入文件
            Image image = javax.imageio.ImageIO.read(_file);                     //构造Image对象
            float width = image.getWidth(null);                                     //得到源图宽
            float height = image.getHeight(null);                                    //得到源图长
            float tempdouble = 0;
            System.out.println("width=" + width + ", height=" + height);

            if (width > height) {
                tempdouble = width / tagsize;
            } else {
                tempdouble = height / tagsize;
            }            

            int new_w = Math.round(width / tempdouble);
            int new_h = Math.round(height / tempdouble);//计算新图长宽
            System.out.println("new_w=" + new_w + ", new_h=" + new_h);

            BufferedImage tag = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(image, 0, 0, new_w, new_h, null);       //绘制缩小后的图
            encoder.encode(tag);
            bos.close();
        } catch (FileNotFoundException fnfe) {
            System.out.println(fnfe);
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }


    public static void resizeImage(InputStream is, String smallpath, String middlepath) {//这里的path是传入你缩略图输出地址
        try {
            Image image = javax.imageio.ImageIO.read(is);                   //构造Image对象
            int width = image.getWidth(null);                                     //得到源图宽
            int height = image.getHeight(null);                                    //得到源图长
            float tagsize = 0;

            System.out.println("width=" + width + ", height=" + height);

            int middlesize = 0;
            int smallsize = 0;
            if (width > 1000 && height > 1000) {
                middlesize = 1000;
                smallsize = 100;
            } else {
                middlesize = 100;
                smallsize = 100;
            }
            resizeImage(image, smallpath, smallsize);  //构造小图
            resizeImage(image, middlepath, middlesize);  //构造中图

        } catch (FileNotFoundException fnfe) {
            System.out.println(fnfe);
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }

    private static void resizeImage(Image image, String tagpath, int tagsize) {
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tagpath));
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
            int width = image.getWidth(null);                                     //得到源图宽
            int height = image.getHeight(null);

            float tempdouble;
            if (width > height) {
                tempdouble = width / tagsize;
            } else {
                tempdouble = height / tagsize;
            }
            System.out.println("tempdouble = " + tempdouble);
            int new_w = Math.round(width / tempdouble);
            int new_h = Math.round(height / tempdouble);//计算新图长宽

            System.out.println("new_w=" + new_w + ", new_h=" + new_h);

            BufferedImage tag = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(image, 0, 0, new_w, new_h, null);       //绘制缩小后的图
            encoder.encode(tag);
            bos.close();
        } catch (FileNotFoundException fnfe) {
            System.out.println(fnfe);
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }

    public static void main(String[] args) {
        //PrintUtils.print("fdsafsdafds");

        // resizeImage("E:\\temp\\ddddddd6.jpg","E:\\temp\\s_6.jpg",80,100);

        try {
            String fileName = "E:\\temp\\bb\\aa\\cc";
            File fileDir = new File(fileName);
            System.out.println("fileDir.isDirectory() = " + fileDir.isDirectory());
            if (!fileDir.isDirectory()) {
               // FileUtils.forceMkdir(fileDir);
            }
        } catch (Exception e) {
            System.out.println("ImageUtils" + ".main" + " line " + 142 + " e = " + e.toString() + " >" + new Date(System.currentTimeMillis()));
        }

        //System.out.println(2256/100);
    }
}

⌨️ 快捷键说明

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