java-图片修改尺寸(缩小)

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import javax.imageio.ImageIO;

public class ImageChange {

    public static void main(String[] args) throws IOException {

        // 缩小图片,输出base64字符串
        String imgPath = "/Users/pic/003.png";
        ImageChange.scaleImageAndEncodeToBase64(imgPath);

        // 缩小图片,导出新图
        String imagePath = "/Users/pic/002.png";
        String newImagePath = "/Users/pic/002-cp.png";
        ImageChange.FileSmall(imagePath, newImagePath);
        
    }

    public static String scaleImageAndEncodeToBase64(String imagePath) throws IOException {

        File imageFile = new File(imagePath);
        BufferedImage originalImage = ImageIO.read(imageFile);

        // 计算缩放比例
        int width = originalImage.getWidth();
        int height = originalImage.getHeight();
        int targetWidth = width/3; // 设置新的宽度
        int targetHeight = (targetWidth * height) / width; // 计算新的高度

        // 创建新的图像
        BufferedImage scaledImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);

        // 将BufferedImage转换为Base64
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(scaledImage, "png", outputStream); // 假设目标格式为jpg
        byte[] imageBytes = outputStream.toByteArray();
        String base64String = Base64.getEncoder().encodeToString(imageBytes);

        System.out.println("Successfully!");

        return base64String;
    }

    public static void FileSmall(String imgPath, String newImagePath){
        try {
            File file = new File(imgPath);
            BufferedImage image = ImageIO.read(file);

            // 计算缩放比例
            int width = image.getWidth();
            int height = image.getHeight();
            int newWidth = width/3; // 设置新的宽度
            int newHeight = (newWidth * height) / width; // 计算新的高度

            // 创建新的图像
            BufferedImage scaledImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);

            // 获取 Graphics 对象以进行绘制
            Graphics g = scaledImage.createGraphics();
            g.drawImage(image, 0, 0, newWidth, newHeight, null);
            g.dispose();

            ImageIO.write(scaledImage, "png", new File(newImagePath));

            System.out.println("Successfully!");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用 Java 的 ImageIO 类和 BufferedImage 类来更改图片尺寸。下面是一个简单的例子: ```java import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ResizeImage { public static void main(String[] args) throws IOException { File inputFile = new File("input.jpg"); BufferedImage inputImage = ImageIO.read(inputFile); // 指定新的宽度和高度 int newWidth = 100; int newHeight = 100; // 创建一个 BufferedImage 对象,指定新的宽度和高度 BufferedImage outputImage = new BufferedImage(newWidth, newHeight, inputImage.getType()); // 获取 Graphics2D 对象 Image image = inputImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH); outputImage.createGraphics().drawImage(image, 0, 0, null); // 保存新图片到文件 File outputFile = new File("output.jpg"); ImageIO.write(outputImage, "jpg", outputFile); } } ``` 这个例子中,我们首先从文件读取一个 BufferedImage 对象。然后指定新的宽度和高度,并创建一个新的 BufferedImage 对象。接着获取 Graphics2D 对象,并使用 drawImage() 方法将原图缩放到新的尺寸。最后将新的 BufferedImage 对象保存到文件中。 注意,这个例子中使用的是 Image.SCALE_SMOOTH 缩放算法,这个算法会使缩放后的图片质量更好,但也会消耗更多的时间和内存。如果你对时间和内存要求较高,可以使用 Image.SCALE_FAST 缩放算法,但缩放后的图片质量可能会比较差。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值