Java缩略图类

import java.io.*;

import java.awt.*;
import java.awt.image.*;

import com.sun.image.codec.jpeg.*;
/**
 *
 * <p>Title: Thumbnail</p>
 *
 * <p>Description: Picture Thumbnail</p>
 *
 * <p>Copyright: Copyright (c) 54powerman@163.com 2005</p>
 *
 * <p>Company: http://blog.sina.com.cn/u1055000490</p>
 *
 * @author 54powerman
 * @version 1.0
 */
public class Thumbnail {
  private String srcFile;
  private String destFile;
  private int width;
  private int height;
  private Image img;

  public static void main(String[] args) throws Exception {
    Thumbnail thum = new Thumbnail("Winter.png");
    thum.resizeFix(500, 300);
  }

  /**
   * 构造函数
   * @param fileName String
   * @throws IOException
   */
  public Thumbnail(String fileName) throws IOException {
    File _file = new File(fileName); //读入文件
    this.srcFile = _file.getName();
    this.destFile = this.srcFile.substring(0, this.srcFile.lastIndexOf(".")) +
        "_s.jpg";
    img = javax.imageio.ImageIO.read(_file); //构造Image对象
    width = img.getWidth(null); //得到源图宽
    height = img.getHeight(null); //得到源图长
  }

  /**
   * 强制压缩/放大图片到固定的大小
   * @param w int 新宽度
   * @param h int 新高度
   * @throws IOException
   */
  public void resize(int w, int h) throws IOException {
    BufferedImage _image = new BufferedImage(w, h,
                                             BufferedImage.TYPE_INT_RGB);
    _image.getGraphics().drawImage(img, 0, 0, w, h, null); //绘制缩小后的图
    FileOutputStream out = new FileOutputStream(destFile); //输出到文件流
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    encoder.encode(_image); //近JPEG编码
    out.close();
  }

  /**
   * 按照固定的比例缩放图片
   * @param t double 比例
   * @throws IOException
   */
  public void resize(double t) throws IOException {
    int w = (int) (width * t);
    int h = (int) (height * t);
    resize(w, h);
  }

  /**
   * 以宽度为基准,等比例放缩图片
   * @param w int 新宽度
   * @throws IOException
   */
  public void resizeByWidth(int w) throws IOException {
    int h = (int) (height * w / width);
    resize(w, h);
  }

  /**
   * 以高度为基准,等比例缩放图片
   * @param h int 新高度
   * @throws IOException
   */
  public void resizeByHeight(int h) throws IOException {
    int w = (int) (width * h / height);
    resize(w, h);
  }

  /**
   * 按照最大高度限制,生成最大的等比例缩略图
   * @param w int 最大宽度
   * @param h int 最大高度
   * @throws IOException
   */
  public void resizeFix(int w, int h) throws IOException {
    if (width / height > w / h) {
      resizeByWidth(w);
    }
    else {
      resizeByHeight(h);
    }
  }

  /**
   * 设置目标文件名
   * setDestFile
   * @param fileName String 文件名字符串
   */
  public void setDestFile(String fileName) throws Exception {
    if (!fileName.endsWith(".jpg")) {
      throw new Exception("Dest File Must end with /".jpg/".");
    }
    destFile = fileName;
  }

  /**
   * 获取目标文件名
   * getDestFile
   */
  public String getDestFile() {
    return destFile;
  }

  /**
   * 获取图片原始宽度
   * getSrcWidth
   */
  public int getSrcWidth() {
    return width;
  }
  /**
   * 获取图片原始高度
   * getSrcHeight
   */
  public int getSrcHeight() {
    return height;
  }
}

时间: 2024-07-28 18:55:37

Java缩略图类的相关文章

java中有类的子类一说,那有对象的子对象一说吗?

问题描述 java中有类的子类一说,那有对象的子对象一说吗? java中有类的子类一说,那有对象的子对象一说吗?新手提问莫见笑. 解决方案 类的子类专业点说叫继承,对象的子对象你可以理解成方法的重写或重载等. 解决方案二: 那是不是类中有内部类就可以呢?

漫谈Java实例化类

  Java 中实例化类的动作,你是否还是一成不变 new 对应对象呢?     经手的项目多了,代码编写量自然会增加,渐渐的会对设计模式产生感觉.     怎样使书写出来的类实例化动作,高内聚,低耦合,又兼具一定的扩展能力呢?     本文试图从几段鲜活的代码入手,给大家呈现不一样的 Java 实例化类.     下面代码取自 com.google.zxing 源码实现: public BitMatrix encode(String contents, BarcodeFormat format

创建java只读类

完全可以创建自己的只读类,下面是个简单的例子:   //: Immutable1.java // Objects that cannot be modified // are immune to aliasing. public class Immutable1 { private int data; public Immutable1(int initVal) { data = initVal; } public int read() { return data; } public boole

对Java嵌套类的讨论

摘要:与字段和方法类似,Java允许类是其它类的成员.在这里,我们将嵌套类分为4种--嵌套顶级类(nested top-level classes),成员内部类(instance inner classes),本地内部类(local inner classes)和匿名内部类(anonymous inner classes). 在教授Java时,我经常发现学生尝试在方法中声明其它的方法.不过,与Pascal语言不同--Pascal允许嵌套声明过程procedures(与方法类似),而Java是不允

在Java中用类装载框架控制类加载

摘要 通过构建一个能够把Java类装载隔离到一个指定的jar文件中的类装载组件容器框架,你可以确保运行时刻会装载你期望的组件版本. Java的类装载框架强有力且具有灵活性.它允许应用程序存取类库而不必链接到静态的"include"文件.代之的是,它能够从指定位置装载包含库类和资源的档案文件,例如由CLASSPATH环境变量所定义的目录和网络位置.由系统来动态地解析对类和资源的运行时刻参考,从而简化了更新和版本发行.然而,每一个库都有其自己的依赖性集合-并且由开发者和发布人员来保证他们的

Java枚举类用法实例

  本文实例讲述了Java枚举类用法.分享给大家供大家参考.具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 package com.school.stereotype; /** * 活动枚举类型 * @

基于GD2图形库的PHP生成图片缩略图类代码分享

 这篇文章主要介绍了基于GD2图形库的PHP生成图片缩略图类代码分享,本文直接给出实现代码和使用方法,需要的朋友可以参考下     要使用PHP生成图片缩略图,要保证你的PHP服务器安装了GD2图形库 使用一个类生成图片的缩略图 1.使用方法 ? 1 2 $resizeimage = new resizeimage("图片源文件地址", "200", "100", "0","缩略图地址"); //就只用上面

支持png透明图片的php生成缩略图类分享

 这篇文章主要介绍了支持png透明图片的php生成缩略图类分享,本文代码基于GD2图形库,实现支持png透明图片生成缩略图,需要的朋友可以参考下     注:此功能依赖GD2图形库 最近要用php生成缩略图,在网上找了一下,发现了这篇文章:PHP生成图片缩略图 试用了一下后,发现有这样几个问题: 1.png图片生成的缩略图是jpg格式的 2.png图片生成的缩略图没有了透明(半透明)效果(填充了黑色背景) 3.代码语法比较老 因此,在这个版本的基础上简单修改优化了一下. PHP生成缩略图类 ?

Java File类的常用方法总结

 这篇文章主要介绍了Java File类的常用方法总结,本文讲解了File类的常用方法,并对一些方法给出了代码示例,需要的朋友可以参考下     Java File类的功能非常强大,利用Java基本上可以对文件进行所有的操作.本文将对Java File文件操作类进行详细地分析,并将File类中的常用方法进行简单介绍,有需要的Java开发者可以看一下. 构造函数 代码如下: public class FileDemo { public static void main(String[] args)