本文目录导读:

我来提供几个Java实现图片缩放的常用方法案例。
基本图片缩放(Java标准库)
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageScaler {
/**
* 缩放图片到指定尺寸
* @param sourcePath 源图片路径
* @param targetPath 目标图片路径
* @param width 目标宽度
* @param height 目标高度
*/
public static void scaleImage(String sourcePath, String targetPath,
int width, int height) throws IOException {
// 读取源图片
File sourceFile = new File(sourcePath);
BufferedImage sourceImage = ImageIO.read(sourceFile);
// 创建缩放后的图片
BufferedImage scaledImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 绘制缩放后的图片
Graphics2D g2d = scaledImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.drawImage(sourceImage, 0, 0, width, height, null);
g2d.dispose();
// 保存图片
ImageIO.write(scaledImage, "jpg", new File(targetPath));
}
/**
* 等比缩放图片(按比例)
* @param sourcePath 源图片路径
* @param targetPath 目标图片路径
* @param targetWidth 目标宽度(高度会自动计算)
*/
public static void scaleImageByWidth(String sourcePath, String targetPath,
int targetWidth) throws IOException {
File sourceFile = new File(sourcePath);
BufferedImage sourceImage = ImageIO.read(sourceFile);
// 计算等比高度
int originalWidth = sourceImage.getWidth();
int originalHeight = sourceImage.getHeight();
double scale = (double) targetWidth / originalWidth;
int targetHeight = (int) (originalHeight * scale);
scaleImage(sourcePath, targetPath, targetWidth, targetHeight);
}
/**
* 按百分比缩放
* @param sourcePath 源图片路径
* @param targetPath 目标图片路径
* @param percent 缩放百分比(如0.5表示50%)
*/
public static void scaleImageByPercent(String sourcePath, String targetPath,
double percent) throws IOException {
File sourceFile = new File(sourcePath);
BufferedImage sourceImage = ImageIO.read(sourceFile);
int targetWidth = (int) (sourceImage.getWidth() * percent);
int targetHeight = (int) (sourceImage.getHeight() * percent);
scaleImage(sourcePath, targetPath, targetWidth, targetHeight);
}
public static void main(String[] args) {
try {
// 示例1:缩放为固定尺寸
scaleImage("input.jpg", "output_fixed.jpg", 300, 200);
// 示例2:按宽度等比缩放
scaleImageByWidth("input.jpg", "output_width.jpg", 400);
// 示例3:按百分比缩放(缩小50%)
scaleImageByPercent("input.jpg", "output_percent.jpg", 0.5);
System.out.println("图片缩放完成!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用Thumbnailator库(推荐)
import net.coobird.thumbnailator.Thumbnails;
import java.io.IOException;
public class ThumbnailatorExample {
/**
* 基本缩放
*/
public static void basicScale() throws IOException {
// 缩放为指定尺寸
Thumbnails.of("input.jpg")
.size(200, 200)
.toFile("output_basic.jpg");
// 按比例缩放
Thumbnails.of("input.jpg")
.scale(0.5) // 缩小50%
.toFile("output_scale.jpg");
}
/**
* 缩放并保持长宽比
*/
public static void scaleWithAspectRatio() throws IOException {
// 按比例缩放,保持长宽比
Thumbnails.of("input.jpg")
.width(300) // 只设置宽度,高度自动计算
.keepAspectRatio(true)
.toFile("output_width.jpg");
Thumbnails.of("input.jpg")
.height(200) // 只设置高度,宽度自动计算
.keepAspectRatio(true)
.toFile("output_height.jpg");
}
/**
* 带旋转的缩放
*/
public static void scaleWithRotation() throws IOException {
Thumbnails.of("input.jpg")
.size(300, 300)
.rotate(90) // 旋转90度
.toFile("output_rotated.jpg");
}
/**
* 缩放并添加水印
*/
public static void scaleWithWatermark() throws IOException {
Thumbnails.of("input.jpg")
.size(500, 500)
.watermark(Thumbnails.of("watermark.png")
.size(100, 100)
.asBufferedImage(), 0.5f) // 半透明水印
.toFile("output_watermark.jpg");
}
public static void main(String[] args) {
try {
basicScale();
scaleWithAspectRatio();
System.out.println("Thumbnailator缩放完成!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用ImageMagick(通过JMagick)
import magick.ImageInfo;
import magick.MagickImage;
import magick.MagickException;
public class JMagickExample {
/**
* 使用ImageMagick缩放图片
*/
public static void scaleWithImageMagick(String sourcePath, String targetPath,
int width, int height) {
try {
ImageInfo imageInfo = new ImageInfo(sourcePath);
MagickImage image = new MagickImage(imageInfo);
// 缩放图片
MagickImage scaledImage = image.scaleImage(width, height);
// 保存图片
scaledImage.setFileName(targetPath);
scaledImage.writeImage(new ImageInfo());
System.out.println("ImageMagick缩放完成!");
} catch (MagickException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
scaleWithImageMagick("input.jpg", "output_magick.jpg", 300, 200);
}
}
批量图片缩放工具
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class BatchImageScaler {
private static class ScalingTask implements Runnable {
private File sourceFile;
private File targetDir;
private int maxWidth;
private int maxHeight;
public ScalingTask(File sourceFile, File targetDir, int maxWidth, int maxHeight) {
this.sourceFile = sourceFile;
this.targetDir = targetDir;
this.maxWidth = maxWidth;
this.maxHeight = maxHeight;
}
@Override
public void run() {
try {
BufferedImage sourceImage = ImageIO.read(sourceFile);
if (sourceImage == null) {
System.out.println("无法读取图片: " + sourceFile.getName());
return;
}
// 计算缩放比例
int originalWidth = sourceImage.getWidth();
int originalHeight = sourceImage.getHeight();
double scale = Math.min(
(double) maxWidth / originalWidth,
(double) maxHeight / originalHeight
);
if (scale >= 1.0) {
// 原图小于目标尺寸,直接复制
File targetFile = new File(targetDir, sourceFile.getName());
ImageIO.write(sourceImage, getExtension(sourceFile.getName()), targetFile);
return;
}
int targetWidth = (int) (originalWidth * scale);
int targetHeight = (int) (originalHeight * scale);
// 缩放图片
BufferedImage scaledImage = new BufferedImage(targetWidth, targetHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = scaledImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage(sourceImage, 0, 0, targetWidth, targetHeight, null);
g2d.dispose();
// 保存图片
File targetFile = new File(targetDir, sourceFile.getName());
ImageIO.write(scaledImage, "jpg", targetFile);
System.out.println("已缩放: " + sourceFile.getName() +
" (" + originalWidth + "x" + originalHeight +
" → " + targetWidth + "x" + targetHeight + ")");
} catch (IOException e) {
System.err.println("处理失败: " + sourceFile.getName());
e.printStackTrace();
}
}
private String getExtension(String fileName) {
int lastDot = fileName.lastIndexOf('.');
return lastDot > 0 ? fileName.substring(lastDot + 1).toLowerCase() : "jpg";
}
}
/**
* 批量缩放图片
*/
public static void batchScale(String sourceDirPath, String targetDirPath,
int maxWidth, int maxHeight) {
File sourceDir = new File(sourceDirPath);
File targetDir = new File(targetDirPath);
if (!sourceDir.exists() || !sourceDir.isDirectory()) {
System.err.println("源目录不存在: " + sourceDirPath);
return;
}
if (!targetDir.exists()) {
targetDir.mkdirs();
}
// 获取所有图片文件
File[] imageFiles = sourceDir.listFiles((dir, name) -> {
String lowerName = name.toLowerCase();
return lowerName.endsWith(".jpg") ||
lowerName.endsWith(".jpeg") ||
lowerName.endsWith(".png") ||
lowerName.endsWith(".bmp");
});
if (imageFiles == null || imageFiles.length == 0) {
System.out.println("没有找到图片文件");
return;
}
System.out.println("开始批量缩放 " + imageFiles.length + " 个图片...");
// 使用线程池处理
ExecutorService executor = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors()
);
for (File imageFile : imageFiles) {
executor.submit(new ScalingTask(imageFile, targetDir, maxWidth, maxHeight));
}
executor.shutdown();
while (!executor.isTerminated()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
System.out.println("批量缩放完成!");
}
public static void main(String[] args) {
batchScale("source_images", "scaled_images", 800, 600);
}
}
Maven依赖配置
如果使用Thumbnailator库,需要在pom.xml中添加:
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.20</version>
</dependency>
这些示例涵盖了Java中常见的图片缩放场景,可以根据具体需求选择合适的方法。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播