Java处理图片

经常在网上找处理图片的方法,总是感觉很乱,以下代码的方法不错,总结分享下:

/**
     * @param fromFileStr 源图片路径
     * @param saveToFileStr 目标图片目录
     * @param sysimgfile 目标图片名(不带图片后缀)
     * @param suffix 目标图片后缀
     * @param width 目标图片宽
     * @param height 目标图片高
     * @throws Exception
     * @author 李敏
     */
    public static void createThumbnail(String fromFileStr,
            String saveToFileStr, String sysimgfile, String suffix, int width,
            int height) throws Exception {
        double Ratio = 0.0;
        File F = new File(fromFileStr);
        if (!F.isFile())
            throw new Exception(F
                    + " is not image file error in CreateThumbnail!");
        File ThF = new File(saveToFileStr, sysimgfile + "." + suffix);
        BufferedImage Bi = ImageIO.read(F);
        Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH);
        if ((Bi.getHeight() > width) || (Bi.getWidth() > height)) {
            if (Bi.getHeight() > Bi.getWidth())
                Ratio = (double) width / Bi.getHeight();
            else
                Ratio = (double) height / Bi.getWidth();
        }
        AffineTransformOp op = new AffineTransformOp(AffineTransform
                .getScaleInstance(Ratio, Ratio), null);
        Itemp = op.filter(Bi, null);
        try {
            ImageIO.write((BufferedImage) Itemp, suffix, ThF);
        } catch (Exception ex) {
            throw new Exception(" ImageIo.write error in CreatThum.: "
                    + ex.getMessage());
        }
    }

 

转载于:https://www.cnblogs.com/jdxf/p/image.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值