/**
* @author zyq
* 将网络图片进行Base64位编码
* @param imgUrl
*
*/
public static String encodeWebImageToBase64(URL imgUrl){ //传入图片url,将网络图片编码为base64编码
String webArray;
ByteArrayOutputStream outputStream = null; //字节数组流,可以捕获内存缓冲区的数据,转换成字节数组(拓展:ByteArrayInputStream: 可以将字节数组转化为输入流)
try {
BufferedImage bufferedImage = ImageIO.read(imgUrl); //Image是一个抽象列,BufferedImage是Image的实现,Image和BufferedImage的主要作用就是将一副图片加载到内存 中
outputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage,"jpg",outputStream);//将BufferedImage对象直接写出指定输出流outputStream,图片格式为jpg

}catch (MalformedURLException e){ //URL协议、格式或者路径错误异常
e.printStackTrace();
}catch (IOException e1){ //输入输出异常
e1.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder(); //BASE64Encoder,字符串加密算法
webArray = encoder.encode(outputStream.toByteArray()); //编码为base64编码(方法参数为字节数组),其中outputStream.toByteArray()用于获取内存缓冲中的数据,并转换 为字节数组
return webArray;
}

/**
* @author zyq
* 将本地图片进行Base64位编码
* @param imageFile
*/
public static String encodeLocImageToBase64(File imageFile){
String locArray;
ByteArrayOutputStream outputStream = null;
try {
BufferedImage bufferedImage = ImageIO.read(imageFile);
outputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage,"jpg",outputStream);

}catch (MalformedURLException e1){
e1.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
locArray = encoder.encode(outputStream.toByteArray());
return locArray;
}

/**
* @author zyq
* 将base64编码的图片进行解码显示,并保存在指定路径
* @param base64code
* @param imagePath
* @param imageName
*/
public static void decodeBase64ToImage(String base64code,String imagePath,String imageName){

BASE64Decoder decoder = new BASE64Decoder(); //BASE64Decoder,base64编码解码算法,解码为字节数组
try {
FileOutputStream write = new FileOutputStream(new File(imagePath + imageName)); //文件输出流,是用于将数据写入File或 FileDescriptor的输出流,其中
File(imagePath + imageName)创建文件,参数为文件名(文件路径)
byte[] decoderBytes = decoder.decodeBuffer(base64code); //解码,方法参数为base64加密编码
write.write(decoderBytes); //decoderBytes数组写入文件
write.close(); //关闭此文件输出流并释放与此流有关的所有系统资源
}catch (IOException e){
e.printStackTrace();
}
}

java编写之jpg图片与base64编码之间的转换的更多相关文章

  1. 图片和base64编码字符串 互相转换,图片和byte数组互相转换

    图片和base64编码字符串 互相转换 import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; ...

  2. java实现图片与base64字符串之间的转换

    package cn.com; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOEx ...

  3. 图片的 base64 编码

    图片的 base64 编码就是将一幅图片编码成一串字符串,使用该字符串代替图像地址.我们所看到的网页上的图片,都是需要消耗一个 http 请求下载而来的:(所有才有了 csssprites 技术< ...

  4. Base64编码 图片与base64编码互转

    package com.education.util; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import jav ...

  5. JS 获取图片的base64编码

    获取图片的base64编码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  6. PHP将图片转base64编码以及base64图片转换为图片并保存代码

    图片转base64编码 /*图片转换为 base64格式编码*/ $img = 'uploads/01.png'; $base64_img = base64EncodeImage($img); ech ...

  7. 图片的Base64编码

    Base64编码是一种图片处理格式,通过特定的算法将图片编码成一长串字符串,在页面上显示的时候,可以用该字符串来代替图片的url属性. 我们可以来看一下实际的效果 Base64编码效果 在上图中,我们 ...

  8. H5 Js图片转base64编码

    <!Doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. 字符串与图片的Base64编码转换操作

    //图片 转为 base64编码的文本 private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = ne ...

随机推荐

  1. Springboot集成FreeMarker

    Apache官网对FreeMarker的解释如下: Apache FreeMarker™是一个模板引擎 :一个基于模板和变化的数据来生成文本输出(HTML网页,电子邮件,配置文件,源代码,等等)的Ja ...

  2. Python高级笔记(四) -- 多继承_方法解析顺序表MRO

    1. 多继承以及MRO顺序 1.1 单独调用父类的方法 # -*- encoding=utf-8 -*- class Parent(object): def __init__(self, name): ...

  3. vmware彻底隐藏控制栏白条

    vmware全屏模式都会在屏幕顶端留一条细细的条. 选择查看,里面有个独占模式.选中该模式,就可以达到完全全屏的效果. 但是进入独占模式后,无法再在多个系统间来回切换,使用ctrl+alt可以切换回正 ...

  4. #1 macos和windows下对多Python环境配置的记录

    为啥会发现环节配置的问题 因为scrapy的setting前期走弯路的时候,碰到了修改了Windows下的Python中的scrapy的默认setting,但是我电脑上还有anaconda,而且我是使 ...

  5. python3抓图学习-百度贴吧

    # coding=utf-8 from bs4 import BeautifulSoup import urllib.request import os import time def downlao ...

  6. Exp4 恶意代码分析 20164314

    一.实践目标 1.是监控你自己系统的运行状态,看有没有可疑的程序在运行. 2.是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,systr ...

  7. Sql server 用T-sql读取本地数据文件dbf的数据文件

    第一步启用Ad Hoc Distributed Queries  在SQLserver执行以下的语句: exec sp_configure 'show advanced options',1 reco ...

  8. Python:正则表达式(一):search()、match()、findall() 的区别

    以前一直觉得正则很难,不会用,今天试验了几个方法,整理总结了一下,简洁明了. 简单来讲,正则就是 提取一段特征数据,用(.*?)代替. 自己总结的特点&区别: (.*) 贪婪匹配,会尽可能的往 ...

  9. 关于Android8及以上版本自定义广播无法接收问题

    高版本Android直接发送广播是无法接收的 e.g. sendBraodcast(intent); 需要在前面加上(前一个String为接收广播应用的项目地址,后一个为该广播类的地址) intent ...

  10. 关于tarjan

    关于Tarjan算法 梗概 tarjan算法有两种(我了解的),一种是用来求强连通分量的,另一种是关于割点和桥的问题. 根据机房大佬HL说过,这两种算法是互相独立的,只是代码很像. 强连通分量问题 关 ...