精华内容
下载资源
问答
  • 用于利用Java程序将图片转换为Base64编码。简单易懂。
  • JAVA 将图片转换为Base64编码 转载于:https://www.cnblogs.com/lwmp/p/7418211.html

    JAVA 将图片转换为Base64编码

    转载于:https://www.cnblogs.com/lwmp/p/7418211.html

    展开全文
  • 1、基本流程 2、图片转化成base64字符串 ... {//将图片文件转化字节数组字符串,并对其进行Base64编码处理 InputStream in = null; byte[] data = null; //读取图片字节数组 try { in = new Fi.

    1、基本流程

    2、图片转化成base64字符串 

    public static String GetImageStr(String imgFile)  
        {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
            InputStream in = null;  
            byte[] data = null;  
            //读取图片字节数组  
            try   
            {  
                in = new FileInputStream(imgFile);          
                data = new byte[in.available()];  
                in.read(data);  
                in.close();  
            }   
            catch (IOException e)   
            {  
                e.printStackTrace();  
            }  
            //对字节数组Base64编码  
            BASE64Encoder encoder = new BASE64Encoder();  
            //返回Base64编码过的字节数组字符串  
            return encoder.encode(data).replace("\r", "").replace("\n", "");
    
        }

    3、base64字符串转化成图片

    public static boolean GenerateImage(String base64str,String savepath)  
        {   //对字节数组字符串进行Base64解码并生成图片  
            if (base64str == null) //图像数据为空  
                return false;  
           // System.out.println("开始解码");
            BASE64Decoder decoder = new BASE64Decoder();  
            try   
            {  
                //Base64解码  
                byte[] b = decoder.decodeBuffer(base64str);  
              //  System.out.println("解码完成");
                for(int i=0;i<b.length;++i)  
                {  
                    if(b[i]<0)  
                    {//调整异常数据  
                        b[i]+=256;  
                    }  
                }
               // System.out.println("开始生成图片");
                //生成jpeg图片  
                OutputStream out = new FileOutputStream(savepath);      
                out.write(b);  
                out.flush();  
                out.close();  
                return true;  
            }   
            catch (Exception e)   
            {  
                return false;  
            }  
        }

     

    展开全文
  • java将图片转换为base64格式

    千次阅读 2018-08-06 17:04:31
    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;...import Decoder.BASE64Decode...
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import Decoder.BASE64Decoder;
    import Decoder.BASE64Encoder;
    
    public class Base64 {
    
    	/*
    	 * 图片转化成base64字符串  
    	 * imagefilePath  图片路径
    	 */
        public static String GetImageStr(String imagefilePath)  
        {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
           
            InputStream in = null;  
            byte[] data = null;  
            //读取图片字节数组  
            try   
            {  
                in = new FileInputStream(imagefilePath);   //该路径只能是本机路径,不能是网络地址       
                data = new byte[in.available()];  
                in.read(data);  
            }   
            catch (IOException e)   
            {  
                e.printStackTrace();  
            } finally{
            	try {
            		if(in != null){
            			in.close();	
            		}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
            }  
           // 对字节数组Base64编码  
            BASE64Encoder encoder = new BASE64Encoder();  
            return "data:image/png;base64,"+encoder.encode(data).replaceAll("\n|\r","");//返回Base64编码过的字节数组字符串
            
           
        }  
        
        /*
    	 *创建文件夹路径
    	 * imagefilePath  图片路径
    	 */
        public static void createMk(String imageMKPath)  
        {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
           
        	File file =new File(imageMKPath);    
        	//如果文件夹不存在则创建    
        	if  (!file .exists()  && !file .isDirectory())      
        	{       
        	    System.out.println("//不存在");  
        	    file .mkdir();    
        	} else   
        	{  
        	    System.out.println("//目录存在");  
        	}  
    
        }  
          
        
        /*
         * base64字符串转化成图片
         * imgStr 图片base64编码
         * newFilePath  生成图片指定路径
         */
        public static boolean GenerateImage(String imgStr, String newFilePath)  
        {   
      
        	//对字节数组字符串进行Base64解码并生成图片  
            if (imgStr == null) //图像数据为空  
                return false;  
            BASE64Decoder decoder = new BASE64Decoder();  
            OutputStream out = null;
            try   
            {  
                //Base64解码  
                byte[] b = decoder.decodeBuffer(imgStr);  
                for(int i=0;i<b.length;++i)  
                {  
                    if(b[i]<0)  
                    {//调整异常数据  
                        b[i]+=256;  
                    }  
                }  
              
                out = new FileOutputStream(newFilePath);      
                out.write(b);  
                out.flush();  
                  
                return true;  
            }   
            catch (Exception e)   
            {  
                return false;  
            }  finally {
            	if(out != null){
            		try {
    					out.close();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
            	}
            }
        }  
    }

     

    展开全文
  • 这里使用的jar包是commons-codec-1.10.jar; import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;imp...

    https://www.cnblogs.com/ywlx/p/4544179.html

    这里使用的jar包是commons-codec-1.10.jar;

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    import org.apache.commons.codec.binary.Base64;


    /**
    * 将图片转换为Base64<br>
    * 将base64编码字符串解码成img图片
    * @创建时间 2015-06-01 15:50
    *
    */
    public class Img2Base64Util {

    public static void main(String[] args) {
    String imgFile = "d:\\3.jpg";//待处理的图片
    String imgbese=getImgStr(imgFile);
    System.out.println(imgbese.length());
    System.out.println(imgbese);
    String imgFilePath = "d:\\332.jpg";//新生成的图片
    generateImage(imgbese,imgFilePath);
    }
    /**
    * 将图片转换成Base64编码
    * @param imgFile 待处理图片
    * @return
    */
    public static String getImgStr(String imgFile){
    //将图片文件转化为字节数组字符串,并对其进行Base64编码处理


    InputStream in = null;
    byte[] data = null;
    //读取图片字节数组
    try
    {
    in = new FileInputStream(imgFile);
    data = new byte[in.available()];
    in.read(data);
    in.close();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    return new String(Base64.encodeBase64(data));
    }

    /**
    * 对字节数组字符串进行Base64解码并生成图片
    * @param imgStr 图片数据
    * @param imgFilePath 保存图片全路径地址
    * @return
    */
    public static boolean generateImage(String imgStr,String imgFilePath){
    //
    if (imgStr == null) //图像数据为空
    return false;

    try
    {
    //Base64解码
    byte[] b = Base64.decodeBase64(imgStr);
    for(int i=0;i<b.length;++i)
    {
    if(b[i]<0)
    {//调整异常数据
    b[i]+=256;
    }
    }
    //生成jpeg图片

    OutputStream out = new FileOutputStream(imgFilePath);
    out.write(b);
    out.flush();
    out.close();
    return true;
    }
    catch (Exception e)
    {
    return false;
    }
    }
    }

     

     







    public static void main(String[] args){
    try {
    base64UpLoad("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCABaAIcDASIAAhEBAxEB/8QAHQAAAgIDAQEBAAAAAAAAAAAABgcEBQADCAkCAf/EAEMQAAEDAgQEAwUDCgQGAwAAAAECAwQFEQAGEiEHEzFBFCJRMmFxgZEII6EVFjNCUmKxweHwNEOi0SRygpKys8LD8f/EABkBAAMBAQEAAAAAAAAAAAAAAAABAgMEBf/EACkRAAICAQMDAgYDAAAAAAAAAAABAhEDEiExEyJBBFEUQlJhkaFigcH/2gAMAwEAAhEDEQA/AHfUqE+phwx6vKakL25qg2QL7XtosSBvb3dsQ6XQ5VLp6Yyq/UJKgpZLrqGNStSyq5s1137WHoB0xHkZvdKNRpUzp3Wxt9HMDdazxU0SojMSkKcDiitwvPoTobTa521Am6htf1wxhCMtyF11p5FcnNsqA5jKVIs6qxFz5bDoi1vQjFrJyXHcdHNemqOxJEt0X+SVAYBKTxObj1dHiIjoQk3V9415f9eC/KvE5rMUeM4/SJUeSptJca50dViSRcWduQetrXHfphgQqdw3g01lY5k511xZcdWqe+dSz1IBWbDYbDbEWfw2jTKrDmtVGpRVRkrFm5rig5qKdlBZUCPL0t6emL/iFnh7KGRq7Xk0CoOIpsZ18n7gpulJIuA5e17dBf3Y81WuL+fcxZnbq0uv1lchLuv7qQtptoX/AFUghKR7rAYaVkOSTo9HTRH4C4qTWqnq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QbX93Q3tt1xplkpYVY227fEY2f5fyxzLCkqi6NOq291Yas0PL8rKlZr89pdJZbChT4bE1L0hxW+nWNJ0i9t/idha65g1BskAr077JxtphKlSQTcb9fhiGW0bHQm+rrbF44OF3K7/AETOSlVKiyzlPSmjJQBs84lBTbqL369ugxAyC25CrT7kdCFjkEL5h3CSDc/y+YxpzQdVLiX3/wCJtv8ABWJ3D8kuVM338F/9qcdKXbQk97Js+nplSW5LgumPfVot7JI63BtiTmXK7QjS67Q5LVRZjhsyQyL3CkgHVYBJIPlVbuQTvfBPkpluQxmBLqEup/JssWWLj9Cv1xF+z4kPZVzU24AtHgZZ0q3G0V4jb4gH5DBS02Q73FoxATPZVLZcIZRZst3BKCB7JFtzsT8PngmyNk5nN+c6BSZjfOivS08wBIuUBKioX+A7YF8vf4iuI/U5WrT2uHUAH42J+pwxeDi1J4jZfIUQeYTcH9xeOfyWuBQ1+gSMs5mqlFec5jsCU5HN/wBYIUU6h9L4L6NJK6E201ZxKH1KPY3UlG3+k4i8aUgcZMzbD/FE/MpBJ+uLakstoypEWlCUrU4bqA3PlHXDrdDvkgqkyGHiHfu2ieo3tiQUIDgUjmLPqR1+v8salHU+AdxfofjiWs2cAGwIP8caEEV1orFktqHv1f1x+tR3dBGkHbYqN8Tkk8u/e2NaVHUNz1whGvlFCbLX5fhtjMSZGxPxxmGM/9k=");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

     

     

    //将前端所有的data:image/jpeg;base64,/9jxxxxxxxx解析
    public static void base64UpLoad(String base64Data) throws Exception{
    String dataPrix = "";
    String data = "";

    if(base64Data == null || "".equals(base64Data)){
    throw new Exception("上传失败,上传图片数据为空");
    }else{
    String [] d = base64Data.split("base64,");
    if(d != null && d.length == 2){
    dataPrix = d[0];
    data = d[1];
    }else{
    throw new Exception("上传失败,数据不合法");
    }
    }
    // FileDtl fileDtl= new FileDtl();
    String suffix = "";
    if("data:image/jpeg;".equalsIgnoreCase(dataPrix)){//data:image/jpeg;base64,base64编码的jpeg图片数据
    suffix = ".jpg";
    } else if("data:image/x-icon;".equalsIgnoreCase(dataPrix)){//data:image/x-icon;base64,base64编码的icon图片数据
    suffix = ".ico";
    } else if("data:image/gif;".equalsIgnoreCase(dataPrix)){//data:image/gif;base64,base64编码的gif图片数据
    suffix = ".gif";
    } else if("data:image/png;".equalsIgnoreCase(dataPrix)){//data:image/png;base64,base64编码的png图片数据
    suffix = ".png";
    }else{
    throw new Exception("上传图片格式不合法");
    }
    String tempFileName = DateUtil.timeStampMS() + suffix;
    try {
    byte[] bs = Base64.decodeBase64(data);
    //处理数据
    for (int i = 0; i < bs.length; ++i) {
    if (bs[i] < 0) {
    bs[i] += 256;
    }
    }
    OutputStream out = new FileOutputStream("d:\\"+tempFileName);
    out.write(bs);
    out.flush();
    out.close();

    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    // fileDtl.setFileName(tempFileName);
    // // byte[] bs = Base64Utils.decodeFromString(data);
    // BASE64Decoder decoder = new BASE64Decoder();
    // byte[] bs = decoder.decodeBuffer(data);
    // //处理数据
    // for (int i = 0; i < bs.length; ++i) {
    // if (bs[i] < 0) {
    // bs[i] += 256;
    // }
    // }
    // int size=bs.length;
    // fileDtl.setFileSize((long)size) ;
    // InputStream is = new ByteArrayInputStream(bs);
    // fileDtl.setIs(is);
    // return fileDtl;
    }

    转载于:https://www.cnblogs.com/wth21-1314/p/10022174.html

    展开全文
  • public classImageUtil {/*** 本地图片转换base64字符串*@paramimgFile* 图片本地路径*@return*/public static String ImageToBase64ByLocal(String imgFile) {//将图片文件转化字节数组字符串,并对其进行Base...
  • 如何把网络图片转换base64,或者把本地图片转换base64,下面就给大家分享用Java实现图片转换base64的两种方法,希望大家共勉,以后尽量避免: 下面先介绍第一种:把网络图片转换Base64字符串 到此,第一种方法...
  • 如果你是一个软件开发,不论前端后端工程师,图片的处理你是肯定要会的,关于图片Base64编码,你可能有点陌生,但是这是一个软件工程师应该要掌握的知识点,现在很多网友把图片base64转换都做成了小工具如:...
  • 摘要:下文讲述java语言中将图片转换为base64编码的方法分享,如下所示;例:/*file为图片文件对象filePath为转换后base64的存储位置*/public static boolean imageToBase64(File file,String filePath) {if (file....
  • 将图片文件转化字节数组字符串,并对其进行Base64编码处理 /** * @Descriptionmap 将图片文件转化字节数组字符串,并对其进行Base64编码处理 * @author temdy * @Date 2015-01-26 * @param path 图片路径 ...
  • 若要使用javascript图像从HTML页面标签转换为数据URI,首先...这返回图像的base64编码数据URI。例如,如果您具有ID为my-image的图像,则可以使用以下命令-示例functiongetDataUrl(img){//创建画布constcanvas=do...
  • public class SysLibrary{// 将图片文件转化字节数组字符串,并对其转化--->Base64编码public String GetImageStr(String imgFilePath) {byte[] data = null;// 读取图片字节数组try {InputStream in = new ...
  • java实现网络图片转换为base64字符串

    千次阅读 2018-01-03 18:49:34
    功能需求:网络图片转换为base64字符串传给前端 lg:String url = "https://www.baidu.com/369270f.jpg"; base64转码之后: url = "/9j/4AAQSkZJRg......G9AFqq6"; 代码实现: package test; import java.io....
  • 将图片服务器地址转化为BASE64位字符串。 import org.apache.commons.codec.binary.Base64; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net....
  • 将图片文件转化字节数组字符串,并对其进行Base64编码处理 /** * @Descriptionmap 将图片文件转化字节数组字符串,并对其进行Base64编码处理 * @author temdy * @Date 2015-01-26 * @param path 图片路径 ...
  • public static String imageToBase64(String path) {// 将图片文件转化字节数组字符串,并对其进行Base64编码处理 byte[] data = null; // 读取图片字节数组 try { InputStream in = new FileInputStream(path); ...
  • 先来说一下什么我们要对图片base64编码base64是当前网络上最为常见的传输8Bit字节代码的编码方式其中之一。base64主要不是加密,它主要的用途是把某些二进制数转成普通字符用于网络传输。由于这些二进制字符在传输...
  • // 将图片文件转化字节数组字符串,并对其转化---&gt;Base64编码  public String GetImageStr(String imgFilePath) {  byte[] data = null;  // 读取图片字节数组  try {  Input...
  • 下面是编程之家 jb51..../*** @Descriptionmap 将图片文件转化字节数组字符串,并对其进行Base64编码处理* @author temdy* @Date 2015-01-26* @param path 图片路径* @return*/public static String imageToBase6...
  • ---------------------------------- BEGIN --------------------------------- 背景: 项目需要,不同服务器之间图片截图互传,JS new ...java 后台 根据图片SRC获取图片Base64编码供前台显示, 前台 JS 操作(裁剪、...
  • 1 需要导入的jar包:sun.misc.BASE64Decoder.jar2 图片转换为base64编码:/*** 将图片转换成Base64编码** @param imgFile* 待处理图片* @return*/public static String getImgStr(String imgFile) {// 将图片文件...
  • 有一个小功能 , 从网络上读取图片 , 然后将图片转换为String类型,发给别人.废话不多说了,直接上代码.这里附上完整代码....有个jar包 , 不是那么好找,已经放在zip包中了....我本地测试的时候,用的是Tomcat服务器.对了...
  • 摘要:下文讲述将base64编码信息转换为图片的方法分享,如下所示;实现思路:public static boolean base64ToImage(String imgbase64, String imgPath){if (imgbase64 == null){return false;}BASE64Decoder decoder ...
  • 最近遇到图片与base64字符串互相转换甚是头疼啊,把资源分享给大家,愿大家用的好用 话不多说直接上代码: 图片转化成base64字符串(不包含 data:image/jpeg;... //将图片文件转化字节数组字符串,并对其进行Bas
  • Java中将base64编码字符串转换为图片

    千次阅读 2020-03-24 10:25:11
    一段时间,在做摄像头拍照上传,摄像头拍的照片为base64编码格式的字符串,需要上传至项目中,则需要使用到base64编码字符串转换为图片 1、base64编码字符串转换为图片的代码如下 ImageUtil.java: package util...
  • Sub Txt2Jpg(inFileFullName As String, outFileFullName As String) '转换过程Dim a() As Byte, b() As Byte'a 原始文件字节数组b 生成的jpg字节数组Dim i As Long, j As LongDim FileNo As Long'文件号FileNo = ...

空空如也

空空如也

1 2 3 4 5 ... 9
收藏数 178
精华内容 71
关键字:

java将图片转换为base64

java 订阅