java处理图片公共方法

 
     //处理图片 处理图片公共方法
    public String posterImg( byte[] img, String type,String tb)
            throws Exception {
        // 创建文件夹对象,参数文件夹地址
        /* File file = new File("/store/image/100/356/", "poster"); */
        Properties p = ConstVar. PROPERS;
        File file = new File(p.getProperty( "IMAGEPATH"), tb);
        if (!file.exists()) { // 判断目录是否存在
            file.mkdirs(); // 创建目录
        }

        BufferedOutputStream bos; // 定义输出流
        // IdGenerator类-随机生成一组ID值
        String idGerns = IdGenerator. createId();
        String imgName = idGerns + type; // 生成图片名称
        String filePath = file.getPath() + File. separator; // 得到filePath
        filePath = filePath.replace( "\\", "/");
        String fileName = filePath + imgName; // 得到fileName
        bos = new BufferedOutputStream( new FileOutputStream(fileName)); // 创建输出流
        bos.write(img); // 写到硬盘上
        bos.close(); // 关闭输出流

        // 处理图片
        String littleName = filePath + idGerns + "_little" + type; // 生成商品小小图
        String smallName = filePath + idGerns + "_small" + type; // 生成小图
        String mediumName = filePath + idGerns + "_medium" + type; // 生成中图
        String bigName = filePath + idGerns + "_big" + type; // 生成大图
        ScaleImage is = new ScaleImage(); // 创建一个缩略图对象
        String savePath = idGerns+type;
        // 保存小图
        try {
            Thumbnails. of(fileName).size(60, 60).toFile(littleName);
            Thumbnails. of(fileName).size(100, 100).toFile(smallName);
            Thumbnails. of(fileName).size(150, 150).toFile(mediumName);
            Thumbnails. of(fileName).size(350, 350).toFile(bigName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return savePath;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值