图片和base64互转
最近项目需要将图片以base64编码,这里记录下相关的一些东西。
需要导入两个类:sun.misc.BASE64Encoder
sun.misc.BASE64Decoder
下面是相关java代码:
public class Imagebase64 {
static BASE64Encoder encoder = new sun.misc.BASE64Encoder();
static BASE64Decoder decoder = new sun.misc.BASE64Decoder();
public static void main(String[] args) {
System.out.println(getImageBinary()); // image to base64
base64StringToImage(getImageBinary()); // base64 to image
}
static String getImageBinary() {
File f = new File("d://in.jpg");
try {
BufferedImage bi = ImageIO.read(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
static void base64StringToImage(String base64String) {
try {
byte[] bytes1 = decoder.decodeBuffer(base64String);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
BufferedImage bi1 = ImageIO.read(bais);
File f1 = new File("d://out.jpg");
ImageIO.write(bi1, "jpg", f1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
图片和base64互转的更多相关文章
- 图片 和 base64 互转
图片转base64 NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]]; UIImage *img = ...
- php 图片与base64互转
header('Content-type:text/html;charset=utf-8'); //读取图片文件,转换成base64编码格式 $image_file = '1.png'; $image ...
- Base64编码 图片与base64编码互转
package com.education.util; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import jav ...
- PHP 图片base64 互转
<?php /* http://tool.css-js.com/base64.html 透明图片 <img src="data:image/jpg;base64,iVBORw0K ...
- C#和Python 图片和base64的互转
C#实例代码: /// <summary> /// 图片转base64 /// </summary> /// <param name="bmp"> ...
- java 图片base64互转
public class ImgBase64 { public static void main(String[] args) //测试 { String strImg = GetImageStr() ...
- 用 opencv和numpy进行图片和字符串互转,并保存至 json
用 opencv和numpy进行图片和字符串互转,并保存至 json 转至 https://zhuanlan.zhihu.com/p/27349847 受 用 base64 进行图片和字符串互转,并保 ...
- HTML5之图片转base64编码
之前在群里看到很多小哥哥小姐姐讨论关于图片base64互转的方法,刚好我之前用到的一个方法给大家分享一下. <!Doctype html><html> <head> ...
- .net C# 图片转Base64 Base64转图片
//图片 转为 base64编码的文本 private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = ne ...
随机推荐
- win10 SVN不能显示图标
参考的解决办法有很多(http://blog.csdn.net/lishehe/article/details/8257545),大多数是操作一下注册表. 我就按照他们的办法,svn的注册表顺序根本上 ...
- json和pickle,XML
什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSON 独立于语言:JSON ...
- [potatos][flex][TBC] 语义分析词法分析 flex
FLEX: The Fast Lexical Analyzer https://github.com/westes/flex 这并不是我的人生中第一次遇见flex,好多工程中,我都发现他们用到了fle ...
- LeetCode 897 Increasing Order Search Tree 解题报告
题目要求 Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the r ...
- java之反射的基本介绍
什么是反射 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的以及动态调用对象的方法的功能称为Java的反射 ...
- 20165336 2017-2018-2 《Java程序设计》第8周学习总结
20165336 2017-2018-2 <Java程序设计>第8周学习总结 教材学习内容总结 第十二章 1.程序:一段静态的代码.进程:程序的一次动态执行过程,它对应了从代码加载.执行至 ...
- java Web开发基础(一)工程项目文档结构
2013年毕业后,在深圳工作开始是用.NET ASP.NET MVC做的项目,后来公司用java来做.于是就从.NET转java了.从.NET转java不是那么的难.今天刚好是清明节放假三天,整理了j ...
- caffe中在某一层获得迭代次数的方法以及caffe编译时报错 error: 'to_string' is not a member of 'std'解决方法
https://stackoverflow.com/questions/38369565/how-to-get-learning-rate-or-iteration-times-when-define ...
- 用canvas画三角形的方法
<canvas id="favoriteRectangle" width="30" height="30"></canva ...
- Python3学习之路~8.2 socket简单实例 实现ssh 发送大量数据
实例1: 利用socket模拟客户端和服务器端各自收发一次数据: #Author:Zheng Na # 客户端 import socket # 声明socket类型,同时生成socket连接对象 cl ...