自定义的java开发工具类ImageUtils

目录

1.base64格式转

2.根据路径,输出图片文件到前端

3.图片压缩


开发中我们时常需要使用到图片,图片一般有两种较为常见的保存形式:1.图片文件保存到本地,数据库中保存文件路径。2,。将图片转为base64格式,直接将base64字符串保存进数据库。

下面则对一般常见的图片处理类进行记录。

1.base64格式互转

此方法将图片转为base64格式,输入参数为图片的保存路径,输出为base64字符串。

//图片转为base64数据
    public static String ImageToBase64(String imgPath) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgPath);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return "data:image/jpeg;base64,"+encoder.encode(data);
    }

此处需要注意的是

data:image/jpeg;base64,

为base64数据头,不同图片类型的图片头不一样,此处为jpeg/jpg格式图片的格式头。

此方法将base64格式转为图片文件保存,输入参数为base64字符串及保存的文件名称,输出为一个boolean类型标识符。

    public static boolean base64ToImage(String base64Data,String tempFileName){

        String dataPrix = ""; //base64格式前头
        String data = "";//实体部分数据

        if(base64Data==null||"".equals(base64Data)){
            return false;
        }else {
            String [] d = base64Data.split("base64,");//将字符串分成数组
            if(d != null && d.length == 2){
                dataPrix = d[0];
                data = d[1];
            }else {
                return false;
            }
        }

        //图片保存路径
        //String imgFilePath = "F:\\springboot_data\\" + new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"/";
        //若文件件不存在,创建文件夹
        File file = new File(imgFilePath);
        if (!file.exists()) {
            file.mkdirs();
        }

        imgFilePath = imgFilePath + tempFileName;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            //Base64解码
            byte[] b = decoder.decodeBuffer(data);
            for(int i=0;i<b.length;++i) {
                if(b[i]<0) {
                    //调整异常数据
                    b[i]+=256;
                }
            }
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

    }

注意:对应上述图片转base64格式方法,此方法输入的base64字符串需带有data格式头。

2.根据路径,输出图片文件到前端

try{
                
        String filePath = "";    //此处为你的文件路径
        String name = "";    //此处为你的文件名称
        picInput = new FileInputStream(filePath+name);
        OutputStream outputStream = response.getOutputStream();
        byte[] data = new byte[1024*10];
        while(picInput.read(data)!=-1){
            //写入图片流
            outputStream.write(data);
        }
        //输出图片流
        outputStream.flush();

        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(picInput!=null){
                try{
                    picInput.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
         }

以上代码可以将你本地存储的图片文件直接输出到前端。

而文件路径一般我们存储在数据库中,再根据业务需求,使用查询语句获得。

3.图片压缩

有时候图片过大,我们需要根据业务需求进行一定的压缩。

以下方法可以进行图片的压缩,输入的需要的图片的路径,输出为压缩后的图片路径:

public static String  PictureCompression(String filePath){
        try {
            //图片所在路径
            BufferedImage templateImage = ImageIO.read(new File(filePath));
            //原始图片的长度和宽度
            int height = templateImage.getHeight();
            int width = templateImage.getWidth();
            //通过比例压缩
            float scale = 0.8f;
            //通过固定长度压缩
            /*int doWithHeight = 100;
            int dowithWidth = 300;*/
            //压缩之后的长度和宽度
            int doWithHeight = (int) (scale * height);
            int dowithWidth = (int) (scale * width);
            BufferedImage finalImage = new BufferedImage(dowithWidth, doWithHeight, BufferedImage.TYPE_INT_RGB);
            finalImage.getGraphics().drawImage(templateImage.getScaledInstance(dowithWidth, doWithHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
            
            //图片输出路径
            String ouputpath = "";
            //若文件夹不存在,则创建
            File file = new File(ouputpath);
            if (!file.exists()) {
                file.mkdirs();
            }
            //使用uuid命名压缩后的图片
            ouputpath = ouputpath + UUID.randomUUID().toString()+".jpg";
            String formatName = ouputpath.substring(ouputpath.lastIndexOf(".") + 1);
            ImageIO.write(finalImage, formatName , new File(ouputpath) );
            //返回压缩后的图片路径
            return ouputpath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值