java中用thumbnailator依赖写一个压缩图片的类,只要图片大小超过几兆就无限循环下去的详细代码实例?

178 篇文章 7 订阅

下面是使用thumbnailator依赖编写的一个压缩图片类的详细代码示例,该类会对大小超过指定大小(以兆为单位)的图片进行无限循环压缩。

java
Copy code
import net.coobird.thumbnailator.Thumbnails;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageCompressor {
    private static final long MAX_SIZE_MB = 5; // 最大尺寸(以兆为单位)

    public static void main(String[] args) throws IOException {
        String imagePath = "path/to/your/image.jpg";
        compressImage(imagePath);
    }

    public static void compressImage(String imagePath) throws IOException {
        File imageFile = new File(imagePath);

        while (isLargerThanMaxSize(imageFile)) {
            BufferedImage image = ImageIO.read(imageFile);
            int width = image.getWidth();
            int height = image.getHeight();

            Thumbnails.of(imageFile)
                    .size(width / 2, height / 2) // 缩小图片为原始大小的一半
                    .outputQuality(0.9) // 压缩质量为90%
                    .toFile(imageFile);
        }

        System.out.println("Image compressed successfully!");
    }

    private static boolean isLargerThanMaxSize(File file) {
        long fileSizeInBytes = file.length();
        long fileSizeInMb = fileSizeInBytes / (1024 * 1024); // 转换为兆(MB)
        return fileSizeInMb > MAX_SIZE_MB;
    }
}
在上述示例中,我们首先指定了要处理的图像文件路径imagePath,然后调用compressImage()方法来实现无限循环压缩。其中,我们使用ImageIO.read()方法读取图像文件并获取其宽度和高度。

然后,我们使用Thumbnails.of()方法以及链式调用.size(width / 2, height / 2)将图像大小缩小为原始大小的一半,并使用.outputQuality(0.9)设置压缩质量为90%。最后,我们将结果保存回原始图像文件。

在每次循环迭代中,我们使用isLargerThanMaxSize()方法检查图像文件是否仍然超过指定的最大尺寸。如果是,则继续进行下一次循环迭代,直到图像大小小于等于指定的最大尺寸为止。

请确保将thumbnailator库添加到项目的依赖中,并根据实际情况替换示例代码中的图像路径和最大尺寸。

好的,下面是一个简单的图片缩放的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <math.h> #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" int main(int argc, char* argv[]) { if (argc < 4) { printf("Usage: %s input_file output_file scale\n", argv[0]); return 1; } char* input_file = argv[1]; char* output_file = argv[2]; float scale = atof(argv[3]); int width, height, channels; unsigned char* image_data = stbi_load(input_file, &width, &height, &channels, STBI_rgb_alpha); if (!image_data) { printf("Error: Failed to load image file %s\n", input_file); return 1; } int new_width = (int)round(scale * width); int new_height = (int)round(scale * height); unsigned char* new_image_data = (unsigned char*)malloc(new_width * new_height * 4); for (int y = 0; y < new_height; y++) { for (int x = 0; x < new_width; x++) { int src_x = (int)round(x / scale); int src_y = (int)round(y / scale); src_x = src_x >= width ? width - 1 : src_x; src_y = src_y >= height ? height - 1 : src_y; int src_index = (src_y * width + src_x) * 4; int dst_index = (y * new_width + x) * 4; new_image_data[dst_index] = image_data[src_index]; new_image_data[dst_index + 1] = image_data[src_index + 1]; new_image_data[dst_index + 2] = image_data[src_index + 2]; new_image_data[dst_index + 3] = image_data[src_index + 3]; } } stbi_write_png(output_file, new_width, new_height, STBI_rgb_alpha, new_image_data, new_width * 4); stbi_image_free(image_data); free(new_image_data); return 0; } ``` 这个代码依赖于STB的图像处理库,可以使用以下命令安装: ``` sudo apt-get install libstb-dev ``` 然后使用以下命令编译: ``` gcc -o image_scale image_scale.c -lm -lstb_image -lstb_image_write ``` 使用方式如下: ``` ./image_scale input_file output_file scale ``` 其中,input_file为原始图片路径,output_file为缩放后图片路径,scale为缩放比例。例如: ``` ./image_scale input.png output.png 0.5 ``` 会将input.png缩放为原始图片的一半大小,并保存为output.png。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值