/**
* @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. Eclipse MAT 安装及使用

    Eclipse MAT官方网页:https://www.eclipse.org/mat/downloads.php 一.MAT是什么? MAT(Memory Analyzer Tool),一个基于Ec ...

  2. map集合的常用方法

    package test; import java.util.Collection; import java.util.HashMap; import java.util.Map; import ja ...

  3. winform窗体嵌套HTML页面,开发出炫彩桌面程序

    一:CEF全称Chromium Embedded Framework,是一个基于Google Chromium 的开源项目.Google Chromium项目主要是为Google Chrome应用开发 ...

  4. GitHub:本地项目上传与团队协作

    第一部分:我的本次作业成果 我自己个人的github地址是:colintz的个人仓库 我们开发团队小组的github地址是:小组3集中营 第二部分:强烈推荐的github资源 对于和我一样,初次接触g ...

  5. 关于JDK1.7+中HashMap对红黑树场景的思考

    背景 在1.7之前的版本,当数组元素较多(几百.几千,或者更多)的时候,在这种前提扩容,涉及全量元素的遍历和坐标的重新定位,这个耗时会比较长.这是之前存在的一个弊端吧.那么引入红黑树之后就解决了问题, ...

  6. 【尚学堂·Hadoop学习】MapReduce案例1--天气

    案例描述 找出每个月气温最高的2天 数据集 -- :: 34c -- :: 38c -- :: 36c -- :: 32c -- :: 37c -- :: 23c -- :: 41c -- :: 27 ...

  7. E: Unable to locate package clang-7 E: Unable to locate package clang++-7 E: Couldn't find any package by regex 'clang++-7'

    我的系统是Debian 9.8, 然后在装下面这两个包的时候老是提示找不到.然后再github上一问,过了几分钟就有大佬回复了,而且亲测有效 ~$ sudo apt-get -y install cl ...

  8. php curl请求

    multipart/form-data 方式 post的curl库,模拟post提交的时候,默认的方式 multipart/form-data ,这个算是post提交的几个基础的实现方式. $post ...

  9. LeetCode第十九题-链表节点的删除

    Remove Nth Node From End of List 问题简介;给定链表,从链表末尾删除第n个节点并返回其头部 例: 给定链表:1-> 2-> 3-> 4-> 5, ...

  10. 一位6年老Android面经总结

    声明|作者:android进阶者地址:https://www.jianshu.com/p/d77873cbad5f 前言 准备面试其实已经准备了挺久了,当时打算面试准备了差不多以后,跟公司谈谈涨薪的事 ...