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;
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 缩略图透明功能可以通过使用 Java 的图像处理库来实现。以下是一种常见的实现方式: 1. 导入所需要的 Java 图像处理库,比如 ImageIO 和 BufferedImage。 2. 使用 ImageIO 类的静态方法读取原始的图片文件,并将其转换成 BufferedImage 对象。 3. 创建一个新的 BufferedImage 对象,用于存储生成的缩略图。 4. 调用 Graphics2D 类的 create 方法,创建一个 Graphics2D 对象,用于在新的 BufferedImage 上绘制图像。 5. 设置 Graphics2D 对象的绘制透明度。 6. 调用 Graphics2D 对象的 drawImage 方法,将原始图片绘制到新的 BufferedImage 上。 7. 调用 Graphics2D 对象的 dispose 方法,释放绘图资源。 8. 使用 ImageIO 类的静态方法将新的 BufferedImage 对象保存为图片文件。 简单的代码示例: ```java import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ThumbnailCreator { public static void main(String[] args) { try { File originalFile = new File("original.jpg"); // 读取原始图片 BufferedImage originalImage = ImageIO.read(originalFile); // 创建缩略图 int thumbnailWidth = 100; int thumbnailHeight = 100; BufferedImage thumbnailImage = new BufferedImage(thumbnailWidth, thumbnailHeight, BufferedImage.TYPE_INT_ARGB); // 绘制透明缩略图 Graphics2D graphics2D = thumbnailImage.createGraphics(); graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); graphics2D.drawImage(originalImage, 0, 0, thumbnailWidth, thumbnailHeight, null); graphics2D.dispose(); // 保存缩略图 File thumbnailFile = new File("thumbnail.png"); ImageIO.write(thumbnailImage, "png", thumbnailFile); } catch (IOException e) { e.printStackTrace(); } } } ``` 上述代码将原始图片 "original.jpg" 缩放为100x100的缩略图,并使用 0.5 的透明度绘制在新的图片上,然后保存为 "thumbnail.png"。可以根据需要调整缩略图的尺寸和透明度值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值