首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

Java 圖片縮略圖生成完全指南:單張/批量、比例/固定尺寸、高質量輸出

     編輯:Java 程式設計入門

核心 API:BufferedImage + ImageIO

Java 標準庫 javax.imageio 與 java.awt.image 提供零依賴的圖片讀寫與縮放能力,適合輕量級項目;大規模處理建議引入 Thumbnailator 或 TwelveMonkeys ImageIO。

單張縮放:兩種模式

/** 縮小到原尺寸的 1/times(times>1) */
public BufferedImage resizeImage(BufferedImage src, float times) {
    int w = src.getWidth(), h = src.getHeight();
    int tw = Math.max(1, (int)(w / times));
    int th = Math.max(1, (int)(h / times));
    return scale(src, tw, th);
}

/** 按倍數縮放(0.5 = 縮小一半,2 = 放大兩倍) */
public BufferedImage zoomImage(BufferedImage src, float factor) {
    int w = src.getWidth(), h = src.getHeight();
    int tw = Math.max(1, (int)(w * factor));
    int th = Math.max(1, (int)(h * factor));
    return scale(src, tw, th);
}

/** 通用高質量縮放 */
private BufferedImage scale(BufferedImage src, int tw, int th) {
    BufferedImage dst = new BufferedImage(tw, th, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = dst.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.drawImage(src.getScaledInstance(tw, th, Image.SCALE_SMOOTH), 0, 0, null);
    g.dispose();
    return dst;
}

高質量 JPEG 輸出(可調壓縮質量)

public boolean writeHighQuality(BufferedImage img, String outPath) {
    try (FileOutputStream fos = new FileOutputStream(outPath)) {
        // 需 JDK 內置 com.sun.image.codec.jpeg 或引入 TwelveMonkeys
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
        JPEGEncodeParam param = JPEGCodec.getDefaultJPEGEncodeParam(img);
        param.setQuality(0.95f, true);  // 0.0~1.0,越大質量越高
        encoder.encode(img, param);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
注意:com.sun.image.codec.jpeg 為 Sun 專有 API,JDK 9+ 已移除。生產環境請改用 ImageIO.write(img, "jpg", out) 搭配 ImageWriteParam 設置質量,或引入 twelvemonkeys-imageio-jpeg。

批量處理:目錄掃描 + 過濾後綴 + 統一輸出

public class BatchThumbnailer {
    private static final String[] IMG_EXTS = {"jpg", "jpeg", "png", "bmp", "gif"};

    public static void main(String[] args) throws IOException {
        File inDir = new File("F:/pic");
        File outDir = new File("F:/picNew");
        if (!outDir.exists()) outDir.mkdirs();

        float factor = 0.25f;  // 縮小到 25%
        BatchThumbnailer t = new BatchThumbnailer();
        for (File f : inDir.listFiles((dir, name) -> {
            String ext = name.substring(name.lastIndexOf('.')+1).toLowerCase();
            return Arrays.asList(IMG_EXTS).contains(ext);
        })) {
            if (f.length() == 0) continue;
            BufferedImage img = ImageIO.read(f);
            BufferedImage thumb = t.zoomImage(img, factor);
            String outName = f.getName().replaceFirst("\\.[^.]+$", "_thumb.jpg");
            t.writeHighQuality(thumb, new File(outDir, outName).getAbsolutePath());
        }
        System.out.println("批量完成!");
    }
    // zoomImage / writeHighQuality 同前
}

最佳實踐清單

場景建議
透明背景 PNG用 BufferedImage.TYPE_INT_ARGB 保留 Alpha 通道
超大圖(>50MP)分塊讀取或用 ImageReader.setInput(..., true) 流式解碼,避免 OOM
並發處理線程池 + CompletableFuture,CPU 核心數為宜
Web 服務引入 Thumbnailator:Thumbnails.of(in).scale(0.25).toFile(out) 一行搞定

常見坑

  • getScaledInstance 預設為 SCALE_FAST,質量差;必須顯式指定 SCALE_SMOOTH 或用 Graphics2D 雙線性插值。
  • JPEG 不支援透明度,輸出 PNG 時勿用 JPEG 編碼器。
  • Windows 路徑反斜桿需轉義或用 File 拼接,避免「路徑不存在」。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved