base64和图片的相互转换
package czc.superzig.modular.utils; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; import java.io.*;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future; import org.apache.commons.lang3.StringUtils; import com.alibaba.fastjson.JSONObject;
import com.sun.jna.NativeLong; import cn.hutool.core.codec.Base64Decoder;
import czc.superzig.common.operatingtable.base.entity.Result;
import czc.superzig.modular.system.operatingtable.entity.CameraBlock;
import czc.superzig.modular.utils.camera.DemoCapture;
import groovyjarjarantlr.collections.List; public class Base64 {
private static final String separator = "/";
private final static ExecutorService executor = Executors.newCachedThreadPool();//启用多线程 //获取base64字符串
public static String encodeBase64(String filaName,boolean isSafe) {
if(StringUtils.isBlank(filaName)){
throw new NullPointerException();
}
InputStream in = null;
byte[] data = null;
String encodedText=null;
//读取图片字节数组
try {
in = new BufferedInputStream(new FileInputStream(filaName));
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
//对字节数组Base64编码 if(isSafe){
java.util.Base64.Encoder encoder = java.util.Base64.getUrlEncoder();
encodedText = encoder.encodeToString(data);
}else{
BASE64Encoder encoder = new BASE64Encoder();
encodedText=encoder.encode(data);
encodedText=encodedText.replaceAll("[\\s*\t\n\r]", "");
}
return encodedText;
} //解析base64
public static String decodeBase64(String base64,String filePath,String suffix,boolean isSafe){
if(StringUtils.isBlank(base64)||StringUtils.isBlank(filePath)||StringUtils.isBlank(suffix)){
throw new NullPointerException();
}
OutputStream out=null;
String fileName=null;
try {
byte[] b=new byte[2048];
if(isSafe){
java.util.Base64.Decoder decoder = java.util.Base64.getUrlDecoder();
b = decoder.decode(base64);
}else{
BASE64Decoder decoder = new BASE64Decoder();
b = decoder.decodeBuffer(base64.substring(base64.indexOf(",") + 1));
}
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
File file=new File(filePath);
if(!file.exists()){
file.mkdirs();
}
fileName=filePath+System.currentTimeMillis()+"."+suffix;
out = new BufferedOutputStream(new FileOutputStream(fileName));
out.write(b);
out.flush();
}catch (Exception e) { }finally {
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
} }
} return fileName;
}
}
base64和图片的相互转换的更多相关文章
- c# 中base64字符串和图片的相互转换
c#base64字符串转图片用到了bitmap类,封装 GDI+ 位图,此位图由图形图像及其特性的像素数据组成. Bitmap 是用于处理由像素数据定义的图像的对象. 具体bitmap类是什么可以自己 ...
- 页面以base64输出图片
<% //读取文件路径,输出base64 编码 System.IO.FileStream stream = System.IO.File.OpenRead(ViewBag.FilePath); ...
- base64和图片的转换
/// <summary> /// base64转图片 /// </summary> /// <param name="strBase64">& ...
- 通过data:image/png;base64把图片直接写在src里
从网上下了个源文件查看时候发现了引用图片的地址不是在本地上的,而是后面跟了一大串字符data:image/png;base64...查了一下资料分析如下: 关于用base64存储图片 网页上有些图片的 ...
- c# API接收Base64转图片
/// <summary> /// API接收Base64转图片 /// </summary> /// <param name="Img">图片 ...
- base64加密图片处理
场景:下载html中内嵌的base64加密图片 举个例子,博客园的插入图片有两种方式,一是引用图片链接,二是直接粘贴2进制图片文件.以第二种方式的图片则是以base64加密的方式内嵌在html页面中. ...
- 前端上传 base64 编码图片到七牛云存储
参考文档 如何上传base64编码图片到七牛云 调试过程 文档中分别有 java 和 html 的 demo,可以根据文档示例调试. 下面是我调试的过程,可以作为参考,特别注意的是,如果需要给文件起名 ...
- 在HTML中显示base64 img 图片
base64的图片可以直接显示在网页上面 <img src=“data:image/png;base64,******************************************** ...
- 利用base64展示图片
其实很简单,格式如下: <img src="data:image/jpg;base64,具体的编码值" /> 支持的类型有: data:,文本数据 data:text/ ...
随机推荐
- jquer_shijian 增加初始化 年月日 及 结束时间 年月日
增加了插件 在初始化的时候,控制 年月日,和结束 年月日 $(time_createobj).siblings(".xtw_budget_userdatafn_hide").shi ...
- 题解 P1283 【平板涂色】
P1283 平板涂色 数据范围也太小了qwq..适合本萌新暴搜 小小的剪枝: 1.用pre预处理出每块矩形上方的矩形,pre[i][0]记录数目(如果数据范围再开大一点,直接1~n枚举判断可能超时qw ...
- 【转】 WordPress数据库及各表结构分析
默认WordPress一共有以下11个表.这里加上了默认的表前缀 wp_ . wp_commentmeta:存储评论的元数据wp_comments:存储评论wp_links:存储友情链接(Blogro ...
- [QT] QT5.12 HTTPS请求 TLS initialization failed
#前言 接触到了Qt的网络编程 然后尝试对一个http页面请求获取源码 是可以的 但是当对https界面发出请求的时候总是错误 TLC什么的初始化失败 百度也是没有结果 然后网上各种方法 比如说编译O ...
- C语言:计算并输出S=1+(1+2^0.5)+(1+2^0.5+3^0.5)...+(1+2^0.5+3^0.5+...+n^0.5)
//计算并输出S=1+(1+2^0.5)+(1+2^0.5+3^0.5)...+(1+2^0.5+3^0.5+...+n^0.5) #include<math.h> #include< ...
- C语言:判断字符串是否为回文,-函数fun将单向链表结点数据域为偶数的值累加起来。-用函数指针指向要调用的函数,并进行调用。
//函数fun功能:用函数指针指向要调用的函数,并进行调用. #include <stdio.h> double f1(double x) { return x*x; } double f ...
- lsof 查看打开了一个文件的有哪些进程 统计那个进程打开的文件最多
lsof | grep /lib64/libc-2.12.so | wc == 查看打开了一个文件的有哪些进程 lsof | awk '{print $2,$1}' | sort | uniq - ...
- Java工作流引擎关于数据加密流程(MD5数据加密防篡改)
关键字: 驰骋工作流程快速开发平台 工作流程管理系统 工作流引擎 asp.net工作流引擎 java工作流引擎. 开发者表单 拖拽式表单 工作流系统 流程数据加密 md5 数据保密流程数据防篡改 ...
- 201771010135杨蓉庆《面向对象程序设计(java)》第六周学习总结
实验六 继承定义与使用 1.实验目的与要求 (1) 理解继承的定义: (2) 掌握子类的定义要求 (3) 掌握多态性的概念及用法: (4) 掌握抽象类的定义及用途: (5) 掌握类中4个成员访问权限修 ...
- 走过的K8S坑
基本的docker命令: docker 镜像 打包成文件 sudo docker save -o 打包后的文件名 {镜像ID}或者{镜像标签} docker 改名: docker tag ff2816 ...