java 图片处理工具类(图片简单处理 java原生)

34 篇文章 0 订阅

java 图片处理工具类

方法说明

方法描述
cutCenterImage从中间裁剪图片不进行缩放
zoomImage缩放图片根据最短边
zoomCutCenterImage缩放裁剪图片,先进行缩放,然后进行裁剪

实现代码

public class ImgUtils {
    /**
     * 图片格式
     */
    public static String format = "PNG";
    public static int unit = 1024;

    /**
     * 从中间裁剪图片
     *
     * @param image  图片
     * @param width  裁剪宽
     * @param height 裁剪高
     * @return 图片
     * @throws IOException
     */
    public static BufferedImage cutCenterImage(BufferedImage image, int width, int height) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, format, os);
        InputStream input = new ByteArrayInputStream(os.toByteArray());
        Iterator iterator = ImageIO.getImageReadersByFormatName(format);
        ImageReader reader = (ImageReader) iterator.next();
        ImageInputStream iis = ImageIO.createImageInputStream(input);
        reader.setInput(iis, true);
        ImageReadParam param = reader.getDefaultReadParam();
        int imageIndex = 0;
        Rectangle rect = new Rectangle((reader.getWidth(imageIndex) - width) / 2, (reader.getHeight(imageIndex) - height) / 2, width, height);
        param.setSourceRegion(rect);
        return reader.read(0, param);


    }

    /**
     * 缩放图片
     *
     * @param image  图片
     * @param width  宽
     * @param height 高
     * @return
     * @throws Exception
     */
    public static BufferedImage zoomImage(BufferedImage image, int width, int height) throws Exception {
        double wr = 0, hr = 0;
        //获取缩放比例
        double wRatio = width * 1.0 / image.getWidth();
        double hRatio = height * 1.0 / image.getHeight();
        AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wRatio, hRatio), null);
        return ato.filter(image, null);
    }

    /**
     * 根据图片流的大小
     *
     * @param is   输入
     * @param size 大小 KB
     * @return 图片
     * @throws Exception
     */

    public static BufferedImage zoomImage(InputStream is, Integer size) throws Exception {
        int fileSize = is.available();
        //文件大于size时,才进行缩放。注意:size以K为单位
        if (fileSize < size * unit) {
            return ImageIO.read(is);
        }
        // 获取长*宽(面积)缩放比例
        double sizeRate = (size * 1024 * 0.5) / fileSize;
        // 获取长和宽分别的缩放比例,即面积缩放比例的2次方根
        double sideRate = Math.sqrt(sizeRate);
        BufferedImage bufImg = ImageIO.read(is);
        AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(sideRate, sideRate), null);
        return ato.filter(bufImg, null);

    }

    /**
     * 缩放裁剪图片
     *
     * @param image  图片流
     * @param width  宽
     * @param height 高
     * @return 图片流
     * @throws Exception
     */
    public static BufferedImage zoomCutCenterImage(BufferedImage image, int width, int height) throws Exception {
        // 读文件获取
        int w = image.getWidth();
        int h = image.getHeight();
        double wRatio = 1.0 * width / w;
        double hRatio = 1.0 * height / h;
        double ratio = Math.max(wRatio, hRatio);
        BufferedImage zoomImage = zoomImage(image, (int) (w * ratio), (int) (h * ratio));
        return cutCenterImage(zoomImage, width, height);
    }

    /**
     * 将文件转换成字节数组
     *
     * @param file 文件
     * @return 字节数组
     */
    public static byte[] getByte(File file) {
        byte[] bytes = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            bytes = new byte[fis.available()];
            fis.read(bytes);
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }

    /**
     * 写入图片
     *
     * @param image
     * @param file
     * @throws IOException
     */
    public static void writeFile(BufferedImage image, File file) throws IOException {
        ImageIO.write(image, format, file);
    }

    public static void writeFile(BufferedImage image, String filePath) throws IOException {
        ImageIO.write(image, format, new File(filePath));
    }

    public static BufferedImage getBufferedImage(InputStream is) throws IOException {
        return ImageIO.read(is);
    }

    private static BufferedImage getBufferedImage(File file) throws IOException {
        return ImageIO.read(file);
    }

    /**
     * 获取图片个格式
     *
     * @param file
     * @return
     */
    private static String getImageType(File file) {
        if (file != null && file.exists() && file.isFile()) {
            String fileName = file.getName();
            int index = fileName.lastIndexOf(".");
            if (index != -1 && index < fileName.length() - 1) {
                return fileName.substring(index + 1);
            }
        }
        return null;
    }
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卑微小钟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值