Graphics2D图像压缩
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
public class ImageResolutionModifier {
public static void main(String[] args) {
// 输入图片路径和输出图片路径
String inputImagePath = "D:\out1.png";
String outputImagePath = "D:\out1-graphics2d.jpg";
// 目标宽度和高度
int targetWidth = 1024;
int targetHeight = 576;
try {
// 读取输入图片
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);
// 创建缩放后的图像
BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, inputImage.getType());
Graphics2D g2d = outputImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.drawImage(inputImage, 0, 0, targetWidth, targetHeight, null);
g2d.dispose();
// 写入输出图片文件
ImageIO.write(outputImage, "JPEG", new File(outputImagePath));
System.out.println("图片分辨率修改成功,并保存到 " + outputImagePath);
} catch (Exception e) {
System.out.println("图片分辨率修改失败: " + e.getMessage());
}
}
}
Thumbnailator图像压缩
import net.coobird.thumbnailator.Thumbnails;
import java.io.File;
public class ImageResolutionModifier {
public static void main(String[] args) {
// 输入图片路径和输出图片路径
String inputImagePath = "D:\out1.png";
String outputImagePath = "D:\out1-thumbnailator.jpg";
// 目标宽度和高度
int targetWidth = 1024;
int targetHeight = 576;
try {
Thumbnails.of(new File(inputImagePath))
.size(targetWidth, targetHeight)
.toFile(new File(outputImagePath));
System.out.p服务器托管网rintln("图片分辨率修改成功,并保存到 " + outputImagePath);
} catch (Exception e) {
System.out.println("图片分辨率修改失败: " + e.getMessage());
}
}
}
ImageJ图像压缩
import ij.ImagePlus;
import ij.io.FileSaver;
import ij.io.Opener;
import ij.process.ImageProcessor;
public class ImageResolutionModifier {
public static void main(String[] args) {
// 输入图片路径和输出图片路径
String inputImagePath = "D:\out1.png";
String outputImagePath = "D:\out1-imagej.jpg";
// 目标宽度和高度
int targetWidth = 1024;
int targetHeight = 576;
try {
// 打开输入图片
Opener opener = new Opener();
ImagePlus imp = opener.openImage(inputImagePath);
ImageProcessor ip = imp.getProcessor();
// 缩放图片
ip = ip.resize(targetWidth, targetHeight);
// 创建输出图片
ImagePlus outputImage = new ImagePlus("Output Image", ip);
// 保存调整分辨率后的图片到输出文件
FileSaver fileSaver = new FileSaver(outputImage);
fileSaver.saveAsJpeg(outputImagePath);
System.out.println("图片分辨率修改成功,并保存到 " + outputImagePath);
} catch (Exception e) {
System.out.println("图片分辨率修改失败: " + e.getMessage());
}
}
}
JAI图像压缩
import javax.media.jai.Interpolation;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import javax.media.jai.operator.ScaleDescriptor;
public class ImageResolutionModifier {
public static void main(String[] args) {
// 输入图片路径和输出图片路径
String inputImagePath = "D:\out1.png";
String outputImagePath = "D:\out1-jai.jpg";
// 目标宽度和高度
int targetWidth = 1024;
int targetHeight = 576;
try {
// 读取输入图片
RenderedOp inputImage = JAI.create("fileload", inputImagePath);
// 缩放图片
RenderedOp scaledImage = ScaleDescriptor.create(inputImage, targetWidth / (float) inputImage.getWidth(),
targetHeight / (float) inputImage.getHeight(), 0.0F, 0.0F,
Interpolation.getInstance(Interpolation.INTERP_BICUBIC), null);
// 保存调整分辨率后的图片到输出文件
JAI.create("filestore", scaledImage, outputImagePath, "JPEG");
System.out.println("图片分辨率修改成功,并保存到 " + outputImagePath);
} catch (Exception e) {
System.out.println("图片分辨率修改失败: " + e.getMessage());
}
}
}
OpenCV图像压缩
import org.opencv.core.*;
import org.open服务器托管网cv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class ImageResolutionModifier {
public static void main(String[] args) {
// 加载 OpenCV 库
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// 输入图片路径和输出图片路径
String inputImagePath = "D:\out1.png";
String outputImagePath = "D:\out1-opencv.jpg";
// 目标宽度和高度
int targetWidth = 1024;
int targetHeight = 576;
try {
// 读取输入图片
Mat inputImage = Imgcodecs.imread(inputImagePath);
// 创建目标尺寸的图像
Mat resizedImage = new Mat();
Size size = new Size(targetWidth, targetHeight);
Imgproc.resize(inputImage, resizedImage, size, 0, 0, Imgproc.INTER_AREA);
// 保存调整尺寸后的图像
Imgcodecs.imwrite(outputImagePath, resizedImage);
System.out.println("图片分辨率修改成功,并保存到 " + outputImagePath);
} catch (Exception e) {
System.out.println("图片分辨率修改失败: " + e.getMessage());
}
}
}
参考文章:选择最佳图像处理工具OpenCV、JAI、ImageJ、Thumbnailator和Graphics2D
Powered By niaonao
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net