zoukankan      html  css  js  c++  java
  • java中图片文件和base64编码的转换

    在线图片转base64编码

      import javax.imageio.ImageIO;
      import java.awt.image.BufferedImage;
      import java.io.ByteArrayOutputStream;
      import java.io.IOException;
      import java.net.URL;
      import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
      /**
         * 下载图片并转换成base64格式
         *
         * @param imageUrl 图片URL
         *
         * @return 图片base64编码
         */
        private String downLoadImageToBase64(String imageUrl) throws Exception{
            logger.info("chainserviceImpl.downLoadImageToBase64,start,imageUrl:{}",imageUrl);
            if(StringUtils.isBlank(imageUrl)){
                throw new JobException("人脸识别,人脸图片url不能为空");
            }
            //下载图片
            BufferedImage image =null;
            URL url = new URL(imageUrl);
            image = ImageIO.read(url);
    
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            String type = StringUtils.substring(imageUrl, imageUrl.lastIndexOf(".") + 1);
            ImageIO.write(image, type, bos);
            byte[] imageBytes = bos.toByteArray();
            String imageString = Base64.encode(imageBytes);
            bos.close();
            logger.info("chainserviceImpl.downLoadImageToBase64,end,imageUrl:{}",imageUrl);
    
            if(StringUtils.isBlank(imageString)){
                throw new JobException("获取人脸图片base64编码失败");
            }
            return imageString;
        }

    本地图片转base64编码

      import java.nio.file.Files;
      import java.nio.file.Paths;  
      import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
      /**
         * 本地图片转base64编码
         * 
         * @param filePath 文件图片所在路径
         *                 
         * @return base64编码
         */
        public String imageToBase64(String filePath) throws Exception{
            if(StringUtils.isBlank(filePath)){
                return null;
            }
            String encode="";
            try{
                byte[] bytes = Files.readAllBytes(Paths.get(filePath));
                encode = Base64.encode(bytes);
            }catch (Exception e){
                throw e;
            }
            return encode;
        }

    base64编码转图片

        import java.nio.file.Files;
        import java.nio.file.Paths;
        import java.nio.file.StandardOpenOption;
    
        /**
         * base64编码转成图片文件
         * 
         * @param base64 图片的base64编码
         * @param filePath 图片文件的保存路径
         *                 
         * @return 
         * @throws Exception
         */
        public static String decryptByBase64(String base64, String filePath) throws Exception{
            if (base64 == null && filePath == null) {
                return "生成文件失败,请给出相应的数据。";
            }
            try {
                Files.write(Paths.get(filePath),Base64.decode(base64), StandardOpenOption.CREATE);
            } catch (IOException e) {
                throw e;
            }
            return "指定路径下生成文件成功!";
        }
  • 相关阅读:
    Filebeat 日志收集器 安装和配置
    纠错式教学法对比鼓励式教学法 -----Lily、贝乐、英孚,乐加乐、剑桥国际、优学汇、北外青少
    硅谷夜谈
    指路Reactive Programming
    Spring WebFlux 要革了谁的命?
    flink部署操作-flink standalone集群安装部署
    Flink的高可用集群环境
    当怪物来敲门 经典台词
    浅析几种线程安全模型
    最详细的排序解析,理解七大排序
  • 原文地址:https://www.cnblogs.com/htyj/p/12096696.html
Copyright © 2011-2022 走看看