base64和图片的相互转换

Posted xiatc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了base64和图片的相互转换相关的知识,希望对你有一定的参考价值。

package czc.superzig.modular.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.sun.jna.NativeLong;

import cn.hutool.core.codec.Base64Decoder;
import czc.superzig.common.operatingtable.base.entity.Result;
import czc.superzig.modular.system.operatingtable.entity.CameraBlock;
import czc.superzig.modular.utils.camera.DemoCapture;
import groovyjarjarantlr.collections.List;

public class Base64 {
    private static final String separator = "/";
    private final static ExecutorService executor = Executors.newCachedThreadPool();//启用多线程

    //获取base64字符串
    public static String encodeBase64(String filaName,boolean isSafe) {
        if(StringUtils.isBlank(filaName)){
            throw new NullPointerException();
        }
        InputStream in = null;
        byte[] data = null;
        String encodedText=null;
        //读取图片字节数组
        try {
            in = new BufferedInputStream(new FileInputStream(filaName));
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //对字节数组Base64编码
        
        if(isSafe){
            java.util.Base64.Encoder encoder = java.util.Base64.getUrlEncoder();
            encodedText = encoder.encodeToString(data);
        }else{
            BASE64Encoder encoder = new BASE64Encoder();
            encodedText=encoder.encode(data);
            encodedText=encodedText.replaceAll("[\s*	

]", "");
        }
        return encodedText;
    }
 
    //解析base64
    public static String decodeBase64(String base64,String filePath,String suffix,boolean isSafe){
        if(StringUtils.isBlank(base64)||StringUtils.isBlank(filePath)||StringUtils.isBlank(suffix)){
            throw new NullPointerException();
        }
        OutputStream out=null;
        String fileName=null;
        try {
            byte[] b=new byte[2048];
            if(isSafe){
                java.util.Base64.Decoder decoder = java.util.Base64.getUrlDecoder();
                b = decoder.decode(base64);
            }else{
                BASE64Decoder decoder = new BASE64Decoder();
                b = decoder.decodeBuffer(base64.substring(base64.indexOf(",") + 1));
            }
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            File file=new File(filePath);
            if(!file.exists()){
                file.mkdirs();
            }
            fileName=filePath+System.currentTimeMillis()+"."+suffix;
            out = new BufferedOutputStream(new FileOutputStream(fileName));
            out.write(b);
            out.flush();
        }catch (Exception e) {

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

            }
        }



        return fileName;
    }
}

 

以上是关于base64和图片的相互转换的主要内容,如果未能解决你的问题,请参考以下文章

Java 处理图片 base64 编码的相互转换

java中图片地址base64编码的相互转换

img对象,file对象,base64,canvas对象相互转换以及图片压缩

关于图片与base64相互转换的工具类

Base64与图片的相互转换

oracle中如何实现blob和base64相互转换