java 截取图片 生成新图片

java 截取图片 生成新图片

网络代码拼凑 非原创

package com.ruoyi.hfiveinterface.util;
import org.springframework.stereotype.Service;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;


/**
 *图片裁剪工具类
 */
@Service
public class SubImageUtil {


    /**
     * 图片进行裁剪
     * @param fromFileStr
     * @param saveToFileStr
     * @param sysimgfile
     * @param suffix
     * @param width
     * @param height
     * @return
     * @throws Exception
     */
    public static boolean createThumbnail(String fromFileStr, String saveToFileStr, String sysimgfile,String suffix,int width,int height) throws Exception {
        // fileExtNmae是图片的格式 gif JPG 或png
        // String fileExtNmae="";
        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 buffer = ImageIO.read(F);
        /*
         * 核心算法,计算图片的压缩比
         */
        int w= buffer.getWidth();
        int h=buffer.getHeight();
        double ratiox = 1.0d;
        double ratioy = 1.0d;

        ratiox= w * ratiox / width;
        ratioy= h * ratioy / height;

        if( ratiox >= 1){
            if(ratioy < 1){
                ratiox = height * 1.0 / h;
            }else{
                if(ratiox > ratioy){
                    ratiox = height * 1.0 / h;
                }else{
                    ratiox = width * 1.0 / w;
                }
            }
        }else{
            if(ratioy < 1){
                if(ratiox > ratioy){
                    ratiox = height * 1.0 / h;
                }else{
                    ratiox = width * 1.0 / w;
                }
            }else{
                ratiox = width * 1.0 / w;
            }
        }
        /*
         * 对于图片的放大或缩小倍数计算完成,ratiox大于1,则表示放大,否则表示缩小
         */
        AffineTransformOp op = new AffineTransformOp(AffineTransform
                .getScaleInstance(ratiox, ratiox), null);
        buffer = op.filter(buffer, null);
        //从放大的图像中心截图
        buffer = buffer.getSubimage((buffer.getWidth()-width)/2, (buffer.getHeight() - height) / 2, width, height);
        try {
            ImageIO.write(buffer, suffix, ThF);
        } catch (Exception ex) {
            throw new Exception(" ImageIo.write error in CreatThum.: "
                    + ex.getMessage());
        }
        return (true);
    }

    //链接url下载图片
    /**
     * 功能描述:
     * @param:  urlList  图片url地址
     * @param:  path     下载到本地的路径
     */
    private static void downloadPicture(String urlList,String path) {

        try {
            URL url = new URL(urlList);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());
            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    //测试 图片先存到本地 根据图片路径进行截取 并生成新图片 存入本地图片路径
    public static void main(String[] args) {

        boolean ss = false;
        try {
            //通过连接地址下载图片到指定文件家
            String url =
                    "https://k2.autoimg.cn/koubeidfs/g4/M12/E7/36/800x600_1_q87_autohomecar__ChsFVmDcMmuAFEIAAAWSEQyzIJs599.jpg";
            String path="D:\\subImgKouBei0917dowlond\\1.jpg";
            downloadPicture(url,path);


            //图片裁剪后 存入指定文件夹
            ss=createThumbnail("D:\\subImgKouBei0917dowlond\\1.jpg", "D:\\subImgKouBei0917", "ps_low2","jpg",400,600);
            if (ss) {
                System.out.println("Success");
            } else {
                System.out.println("Error");
            }
        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值