java中对图片进行压缩以及放大。

阅读更多

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

import javax.imageio.ImageIO;

public class ImageUtils
{

    /**
     * 对图片进行放大
     *
     * @param originalImage 原始图片
     * @param times 放大倍数
     * @return
     */
    public static BufferedImage zoomInImage(BufferedImage originalImage, Integer times)
    {
        int width = originalImage.getWidth() * times;
        int height = originalImage.getHeight() * times;
        BufferedImage newImage = new BufferedImage(width, height, originalImage.getType());
        Graphics g = newImage.getGraphics();
        g.drawImage(originalImage, 0, 0, width, height, null);
        g.dispose();
        return newImage;
    }

    /**
     * 对图片进行放大
     *
     * @param srcPath 原始图片路径(绝对路径)
     * @param newPath 放大后图片路径(绝对路径)
     * @param times 放大倍数
     * @param format 图片格式
     * @return 是否放大成功
     */
    public static boolean zoomInImage(String srcPath, String newPath, Integer times, String format)
    {
        BufferedImage bufferedImage = null;
        try
        {
            File of = new File(srcPath);
            if (of.canRead())
            {
                bufferedImage = ImageIO.read(of);
            }
        } catch (IOException e)
        {
            // TODO: 打印日志
            return false;
        }
        if (bufferedImage != null)
        {
            bufferedImage = zoomInImage(bufferedImage, times);
            try
            {
                // TODO: 这个保存路径需要配置下子好一点
                ImageIO.write(bufferedImage, format, new File(newPath)); // 保存修改后的图像
            } catch (IOException e)
            {
                // TODO 打印错误信息
                return false;
            }
        }
        return true;
    }

    /**
     * 对图片进行缩小
     *
     * @param originalImage 原始图片
     * @param times 缩小倍数
     * @return 缩小后的Image
     */
    public static BufferedImage zoomOutImage(BufferedImage originalImage, int timeshieght)
    {
        int width = originalImage.getWidth() / timeshieght;
        int height = originalImage.getHeight() / timeshieght;
        BufferedImage newImage = new BufferedImage(width, height, originalImage.getType());
        Graphics g = newImage.getGraphics();
        g.drawImage(originalImage, 0, 0, width, height, null);
        g.dispose();
        return newImage;
    }

    /**
     * 对图片进行缩小
     *
     * @param srcPath 源图片路径(绝对路径)
     * @param newPath 新图片路径(绝对路径)
     * @param times 缩小倍数
     * @param format 图片格式
     * @return 保存是否成功
     */
    public static boolean zoomOutImage(String srcPath, String newPath, int timeshieght,
        String format)
    {
        BufferedImage bufferedImage = null;
        try
        {
            File of = new File(srcPath);
            if (of.canRead())
            {
                bufferedImage = ImageIO.read(of);
            }
        } catch (IOException e)
        {
            // TODO: 打印日志
            return false;
        }
        if (bufferedImage != null)
        {
            bufferedImage = zoomOutImage(bufferedImage, timeshieght);
            try
            {
                // TODO: 这个保存路径需要配置下子好一点
                ImageIO.write(bufferedImage, format, new File(newPath)); // 保存修改后的图像,全部保存为JPG格式
            } catch (IOException e)
            {
                // TODO 打印错误信息
                return false;
            }
        }
        return true;
    }

    public static int getHeight(String path)
    {
        BufferedImage bufferedImage = null;
        int height = 0;
        try
        {
            bufferedImage = ImageIO.read(new File(path));
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        height = bufferedImage.getHeight();
        return height;
    }

    public static void main(String[] args)
    {
        int heighttimes = (new BigDecimal((double) ImageUtils.getHeight("E:/1.bmp") / (double) 260)).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
        int heighttime = (new BigDecimal( (double) 260 / ImageUtils.getHeight("E:/bell_add.png"))).setScale(0, BigDecimal.ROUND_HALF_UP)
            .intValue();
        boolean testIn = zoomInImage("E:/bell_add.png", "E:\\in2.jpg", heighttime, "png");
        if (testIn)
        {
            System.out.println("in ok");
        }
        // boolean testOut = zoomOutImage("E:/11.jpg", "E:\\out.jpg",
        // heighttimes, "jpg");
        // if (testOut)
        // {
        // System.out.println("out ok");
        // }

    }

}

你可能感兴趣的:(java)