11
package org.example;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
public class ImageTest {
public static void main(String[] args) throws Exception {
readAndWrite(); // 读取并写入图片
readComparison(); // 图片读取速度对比
cropImage("c:/temp/ecnu.jpg", "c:/temp/shida.jpg", 750, 250, 700, 300, "jpg", "jpg");
combineImagesHorizontally("c:/temp/ecnu.jpg", "c:/temp/shida.jpg", "jpg", "c:/temp/combined.jpg");
}
public static void readAndWrite() throws Exception {
// 读取和写入 PNG 图片
BufferedImage image = ImageIO.read(new File("D://桌面//1.png"));
System.out.println("高度: " + image.getHeight()); // 图片高度(像素)
System.out.println("宽度: " + image.getWidth()); // 图片宽度(像素)
ImageIO.write(image, "png", new File("D://桌面//3.png"));
}
public static void readComparison() throws Exception {
System.out.println("===========加载速度测试==============");
// 使用 ImageIO 读取 PNG 图片
long startTime = System.nanoTime();
BufferedImage image = ImageIO.read(new File("D://桌面//1.png"));
System.out.println("高度: " + image.getHeight()); // 图片高度(像素)
System.out.println("宽度: " + image.getWidth()); // 图片宽度(像素)
long endTime = System.nanoTime();
System.out.println("使用 ImageIO 读取时间: " + (endTime - startTime) / 1000000.0 + "毫秒");
// 使用 PNG ImageReader 读取 PNG 图片
startTime = System.nanoTime();
Iterator
readers = ImageIO.getImageReadersByFormatName("png"); ImageReader reader = readers.next(); // 使用正确的格式读取器
System.out.println(reader.getClass().getName());
ImageInputStream iis = ImageIO.createImageInputStream(new File("D://桌面//1.png"));
reader.setInput(iis, true);
System.out.println("使用 ImageReader 获得的高度:" + reader.getHeight(0)); // 从 ImageReader 获取图片高度
System.out.println("使用 ImageReader 获得的宽度:" + reader.getWidth(0)); // 从 ImageReader 获取图片宽度
endTime = System.nanoTime();
System.out.println("使用 ImageReader 读取时间: " + (endTime - startTime) / 1000000.0 + "毫秒");
}
public static void cropImage(String fromPath, String toPath, int x, int y, int width, int height, String readImageFormat, String writeImageFormat) throws Exception {
FileInputStream fis = null;
ImageInputStream iis = null;
try {
// 读取原始图片文件
fis = new FileInputStream(fromPath);
Iterator
it = ImageIO.getImageReadersByFormatName(readImageFormat); ImageReader reader = it.next();
iis = ImageIO.createImageInputStream(fis);
reader.setInput(iis, true);
// 定义一个矩形 并放入切割参数中
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(x, y, width, height);
param.setSourceRegion(rect);
// 从源文件读取一个矩形大小的图像
BufferedImage bi = reader.read(0, param);
// 写入到目标文件
ImageIO.write(bi, writeImageFormat, new File(toPath));
} finally {
fis.close();
iis.close();
}
}
public static void combineImagesHorizontally(String firstPath, String secondPath, String imageFormat, String toPath) {
try {
File first = new File(firstPath);
BufferedImage imageOne = ImageIO.read(first);
int width1 = imageOne.getWidth();
int height1 = imageOne.getHeight();
int[] firstRGB = imageOne.getRGB(0, 0, width1, height1, null, 0, width1);
File second = new File(secondPath);
BufferedImage imageTwo = ImageIO.read(second);
int width2 = imageTwo.getWidth();
int height2 = imageTwo.getHeight();
int[] secondRGB = imageTwo.getRGB(0, 0, width2, height2, null, 0, width2);
// 生成新图片
int height3 = Math.max(height1, height2);
int width3 = width1 + width2;
BufferedImage imageNew = new BufferedImage(width3, height3, BufferedImage.TYPE_INT_RGB);
// 设置左半部分的RGB从 (0, 0) 开始
imageNew.setRGB(0, 0, width1, height1, firstRGB, 0, width1);
// 设置右半部分的RGB从 (width1, 0) 开始
imageNew.setRGB(width1, 0, width2, height2, secondRGB, 0, width2);
// 保存图片
ImageIO.write(imageNew, imageFormat, new File(toPath));
} catch (Exception e) {
e.printStackTrace();
}
}
}