• 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 "指定路径下生成文件成功!";
        }
  • 相关阅读:
    取近似值
    eclipse 自己主动为getter和setter加入中文凝视
    Linux对外连接port数限制
    C++链表冒泡,归并,插入排序(纯指针)
    Android之实现ViewPager+Fragment左右滑动
    获得鼠标离开消息。
    CMFCPropertyGridProperty的使用
    阅读书单
    Docker 容器管理
    docker rmi 详解
  • 原文地址:https://www.cnblogs.com/htyj/p/12096696.html
Copyright © 2020-2023  润新知