image-base64互转
package base64StringToImage;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; public class TestImageBinary { /**
*
*@param filePath 照片绝对路径
*@return String base64码字符串
*
**/
public String changeImagetoBase64String(String filePath){
BASE64Encoder encoder = new BASE64Encoder();
String base64String = "";
File file = new File(filePath);
BufferedImage buffer;
try {
buffer = ImageIO.read(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffer, "jpg", baos); //第二个参数“jpg”不需要修改
byte[] bytes = baos.toByteArray();
base64String = encoder.encodeBuffer(bytes).trim();
} catch (IOException e) {
e.printStackTrace();
}
return base64String;
} /**
*
*@param base64String base64码字符串
*@param filePath 照片绝对路径
*
**/
public void changeBase64StringtoImage(String base64String, String filePath){
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] bytes = decoder.decodeBuffer(base64String);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
BufferedImage buffer =ImageIO.read(bais);
File file = new File(filePath); //jpg,png,gif等格式
ImageIO.write(buffer, "jpg", file); //第二个参数“jpg”不需要修改
} catch (IOException e) {
e.printStackTrace();
}
}
}
在页面可以直接使用base64码显示照片:
<body>
<span><img width=100 height=100 src="data:image/gif;base64,(base64编码后的照片数据)"/></span>
</body>
image-base64互转的更多相关文章
- JS 实现blob与base64互转
/** * base64 to blob二进制 */ function dataURItoBlob(dataURI) { var mimeString = dataURI.split(',')[0]. ...
- PHP 图片base64 互转
<?php /* http://tool.css-js.com/base64.html 透明图片 <img src="data:image/jpg;base64,iVBORw0K ...
- 图片和base64互转
最近项目需要将图片以base64编码,这里记录下相关的一些东西. 需要导入两个类:sun.misc.BASE64Encoder sun.misc.BASE64Decoder 下面是相关java代码: ...
- 图片 和 base64 互转
图片转base64 NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]]; UIImage *img = ...
- php base64互转pdf
/* * base64转pdf */ function base642pdf($formTxt,$toPdf) { $file = file_get_contents($formTxt);//读 $d ...
- java 图片base64互转
public class ImgBase64 { public static void main(String[] args) //测试 { String strImg = GetImageStr() ...
- php 图片与base64互转
header('Content-type:text/html;charset=utf-8'); //读取图片文件,转换成base64编码格式 $image_file = '1.png'; $image ...
- 图片,base64 互转
import sun.misc.BASE64Decoder; import java.io.FileOutputStream; import java.io.OutputStream; /** * @ ...
- C#编程中的Image/Bitmap与base64的转换及 Base-64 字符数组或字符串的长度无效问题 解决
最近用base64编码传图片遇到了点问题,总结下. 首先总结下base64编码的逻辑,来自网络:https://www.cnblogs.com/zhangchengye/p/5432276.html ...
- HTML5之图片转base64编码
之前在群里看到很多小哥哥小姐姐讨论关于图片base64互转的方法,刚好我之前用到的一个方法给大家分享一下. <!Doctype html><html> <head> ...
随机推荐
- HDU——T 3336 Count the string
http://acm.hdu.edu.cn/showproblem.php?pid=3336 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- 通过一个案例彻底读懂10046 trace--字节级深入破解
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/37840583 2014.7.23晚20:30 Oracle support组猫大师分享&l ...
- js29--装饰着模式
//装饰者模式:就是在保证不改变原有对象的基础上,去扩展一些想要的方法或去求 var CarInterface = new BH.Interface('CarInterface' , ['getPri ...
- [NOI.AC#30]candy 贪心
链接 一个直观的想法是,枚举最小的是谁,然后二分找到另外一个序列对应位置更新答案,复杂度 \(O(NlogN)\) 实际上不需要二分,因为每次当最大的变大之后,原来不行的最小值现在也一定不行,指针移动 ...
- linux设置tab键的宽度为4
先cd 到~目录 ~$ cd ~$ vi .vimrc set nu
- THC=TERMINAL HANDLING CHARGE,碼頭操作費
THC=TERMINAL HANDLING CHARGE,碼頭操作費
- Bitmap Image Graphics
Bitmap Image Graphics private void DrawImagePointF(PaintEventArgs e){ // Create image. Image new ...
- ecshop微信接口基础认识
ecshop微信接口基础认识,当你要学习ecshop和微信整合的时候,你就必须研究ecshop的数据结构对接以及微信数据接口的基本知识.我们知道微信其实就是通过有效的消息推送,用JSON格式的数据或者 ...
- [React] Render Text Only Components in React 16
In this session we create a comment component to explore how to create components that only render t ...
- R语言-有负下标里才干有零
1.仅仅有负下标里才干有零 先看一个样例 >a<-c(1,2,3,4) >a[-1:1] > a[-1:1] Error in a[-1:1] : 仅仅有负下标里才干有零 (1 ...