
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.Base64;
// 限制图像大小为4MB
public byte[] limitImageSize(File imageFile, int maxSizeInBytes) throws IOException {
if (imageFile.length() <= maxSizeInBytes) {
// 图像大小已经符合要求,无需压缩
FileInputStream inputStream = new FileInputStream(imageFile);
byte[] imageData = inputStream.readAllBytes();
inputStream.close();
return imageData;
} else {
// 图像大小超过4MB,需要压缩
double compressionRatio = (double) maxSizeInBytes / imageFile.length();
BufferedImage image = ImageIO.read(imageFile);
int newWidth = (int) (image.getWidth() * Math.sqrt(compressionRatio));
int newHeight = (int) (image.getHeight() * Math.sqrt(compressionRatio));
BufferedImage compressedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
compressedImage.getGraphics().drawImage(image, 0, 0, newWidth, newHeight, null);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(compressedImage, "JPEG", outputStream);
return Base64.getEncoder().encodeToString(outputStream.toByteArray());
}
}
这段代码的主要目的是将给定的图像文件限制在指定的大小(4MB)以内,如果图像大小超过了这个限制,则对图像进行压缩,使其满足限制。以下是对这段代码的详细解释:
limitImageSize 函数接受两个参数:imageFile 和 maxSizeInBytes。imageFile 是要限制大小的图像文件,maxSizeInBytes 是要限制的最大图像大小,以字节为单位(例如4MB对应的字节数)。
首先,它检查图像文件的大小是否已经小于或等于指定的最大大小 (maxSizeInBytes)。如果是,就表示图像已经符合要求,无需进行压缩。在这种情况下,它会将图像文件的内容读取到一个 byte 数组 (imageData) 中。
如果图像大小超过了指定大小,它将计算一个压缩比例 (compressionRatio),以便将图像大小压缩到 maxSizeInBytes 以内。这里使用的是等比例压缩,根据压缩比例重新计算图像的宽度和高度。
创建一个新的 BufferedImage 对象 (compressedImage),作为压缩后的图像容器,然后使用 drawImage 方法将原始图像绘制到新的图像中,实现了压缩。
最后,将压缩后的图像数据写入到一个 ByteArrayOutputStream 中,以获取压缩后的图像的字节数组形式。
返回压缩后的图像数据,或者如果图像大小在限制内,则返回原始图像数据。
这段代码的主要目的是确保图像在传递给接口之前不超过指定的大小限制。如果图像已经符合要求,不需要压缩,而如果超过了大小限制,就会进行等比例压缩。
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.Base64;
// 下载远程图像并按照最大大小进行压缩
public String downloadAndCompressImage(String imageUrl, int maxSizeInBytes) throws IOException {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
BufferedImage image = ImageIO.read(inputStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 计算压缩比例
double compressionRatio = (double) maxSizeInBytes / (image.getWidth() * image.getHeight());
if (compressionRatio < 1.0) {
// 图像需要压缩
int newWidth = (int) (image.getWidth() * Math.sqrt(compressionRatio));
int newHeight = (int) (image.getHeight() * Math.sqrt(compressionRatio));
BufferedImage compressedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
compressedImage.getGraphics().drawImage(image, 0, 0, newWidth, newHeight, null);
ImageIO.write(compressedImage, "JPEG", outputStream);
} else {
// 图像不需要压缩,直接写入输出流
ImageIO.write(image, "JPEG", outputStream);
}
inputStream.close();
outputStream.close();
// 将压缩后的图像数据转换为Base64字符串
return Base64.getEncoder().encodeToString(outputStream.toByteArray());
} else {
throw new IOException("Failed to download image from URL: " + imageUrl);
}
}
这段代码的主要功能是从指定的远程URL下载图像,然后根据指定的最大大小(以字节为单位)对图像进行压缩,最后将压缩后的图像数据以Base64字符串的形式返回。以下是对这段代码的详细介绍:
downloadAndCompressImage 方法:这是主要的方法,用于下载、压缩和编码图像。
imageUrl:要下载的远程图像的URL。maxSizeInBytes:要限制的图像最大大小(以字节为单位)。下载图像:
URL 对象和 HttpURLConnection 对象来建立与指定URL的HTTP连接。GET 请求方法获取图像数据。HTTP_OK(状态码200),表示成功连接到URL并可以继续下载。读取图像:
InputStream 从HTTP连接中获取图像数据。ImageIO.read(inputStream) 读取图像数据并将其加载到 BufferedImage 对象中。压缩图像:
compressionRatio),以确保图像不超过指定的最大大小。BufferedImage 对象 (compressedImage) 作为压缩后的图像容器,然后使用 drawImage 方法将原始图像绘制到新的图像中,实现了压缩。ImageIO.write 将压缩后的图像数据写入到 ByteArrayOutputStream 中。处理未压缩的图像:如果图像不需要压缩,直接将原始图像数据写入输出流中。
关闭流:关闭输入流和输出流,释放资源。
Base64 编码:将压缩后的图像数据转换为Base64字符串,以便后续处理和传输。
返回结果:返回Base64编码后的图像数据。
错误处理:如果在连接或下载过程中发生错误,将抛出 IOException 异常,提供错误消息。
这段代码可以确保下载的图像不会超过指定的大小,并以Base64编码的形式返回,以便后续处理或传输。