编码为Base64/解码为图片

所需的包

import java.io.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

编码

/**
     * 转换base64
     * @param imgPath 图片路径
     * @return
     */
    public static String getImageStr(String imgPath) {
        InputStream inputStream = null;
        byte[] data = null;
        String base64="";
        try {
            File file=new File(imgPath);
            if (!file.exists()) {
                return "";
            }
            inputStream = new FileInputStream(imgPath);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
            //解码
            BASE64Encoder encoder = new BASE64Encoder();
            base64=encoder.encode(data);
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
        return base64;
    }

解码

 /**
     * base64转换为图片
     * @param imgDate   base64编码
     * @param imgPath   图片放置路径
     * @return
     */
    public static Boolean getBase64Str(String imgDate,String imgPath){
        if (imgDate == null){
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        OutputStream out = null;
        try {
            out = new FileOutputStream(imgPath);
            //解码
            byte[] b = decoder.decodeBuffer(imgDate);
            for (int i = 0; i < b.length; i++){
                if (b[i] < 0){          //调整异常数据
                    b[i] += 256;
                }
            }
            out.write(b);
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

给图片进行压缩

 /**
     * 压缩图片-这里将图片分辨率调小,且小于1MB
     * @param path 文件完整的路径
     * @return
     */
    public static String imageGenerateSmall(String path){
        try {
            log.info("文件完整的路径:"+path);
            File bigFile = new File(path);
            if (bigFile.exists() && bigFile.isFile()){
                log.info("bigFile大小:"+bigFile.length());
                Image image = ImageIO.read(bigFile);
                log.info("OK:"+path);
                int width = image.getWidth(null);
                int height = image.getHeight(null);
                if(bigFile.length() > (1024*100) || width > 960 || height > 960) {
                    int arr [] = imageWH(width,height);
                    int widthSmall = arr[0];
                    int heightSmall = arr[1];
                    BufferedImage buffi = new BufferedImage(widthSmall, heightSmall, BufferedImage.TYPE_INT_RGB);
                    Graphics g = buffi.getGraphics();
                    g.drawImage(image, 0, 0, widthSmall, heightSmall, null);
                    g.dispose();
                    ImageIO.write(buffi, "jpg", new File(path));
                    Thread.sleep(200);
                }
                return getImageStr(path);
            }
        } catch (Exception e) {
            e.printStackTrace();
            String info = getExceptionAllInfo(e);
            log.error("压缩图片异常:"+info);
            return "";
        }
        return "";
    }

    public static int[] imageWH(int w, int h){
        if(w > 960 || h > 960) {
            int wh = imageW(w);
            int he = imageH(h);
            return imageWH(wh, he);
        }else {
            return new int[] {w,h};
        }
    }
    public static int imageW(int w){
        w = (int) (w / 2);
        return w;
    }
    public static int imageH(int h){
        h = (int) (h / 2);
        return h;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值