图片和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 ...
随机推荐
- mysql中建立索引的一些原则
1.先存数据,再建索引 有索引的好处是搜索比较快但是在有索引的前提下进行插入.更新操作会很慢 2.不要对规模小的数据表建立索引,数据量超过300的表应该有索引:对于规模小的数据表建立索引 不仅不会提高 ...
- day3_字符串常用方法
s.upper()s.lower()s.capitalize()s.split(',')s.strip('abc')s.lstrip()s.rstrip()s.replace('old','new') ...
- SYSAUX表空间如何清理
############################################################ 操作方案################################### ...
- LeetCode 104 Maximum Depth of Binary Tree 解题报告
题目要求 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- 1、python接口测试requests
import requestsimport jsonr=requests.get('http://www.baidu.com') #get 请求方式r=r ...
- 【PyQt5-Qt Designer】按钮系列
[PyQt5-Qt Designer]按钮系列 复选框(QCheckBox) 效果如下: 参考: https://zhuanlan.zhihu.com/p/30509947 完整代码: from Py ...
- 一步步搭建 Spring Boot maven 框架的工程
摘要:让Spring应用从配置到运行更加快速,演示DIY Spring Boot 框架时,如何配置端口号,如何添加日志. Spring Boot 框架帮助开发者更容易地创建基于Spring的应用程序和 ...
- 安装 Samba服务
参考摘录的是博客园的文章:https://www.cnblogs.com/zhaopengcheng/p/5481048.html ubuntu系统:16.04 1. 首先用管理员权限创建一个新用户, ...
- 安装MongoDB报错
尝试多次,最后找到解决方式: 在安装的最后一步的时候不要勾选左下角的compass即可 命令行mongod --version测试安装是否成功
- Github上Laravel开源排行榜Star数前30名
Github上Laravel开源排行榜前30名,罗列所有 Laravel 开源扩展包,含 Github Star 数量,下载数量和项目简介.默认排序是按Star数量从多到少来排 1.cachethq/ ...