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/ ...
随机推荐
- Docker - Dockerfile - 常见命令简介
概述 感觉是个 比较重要的东西 有个疑问 我是先讲 docker build 还是 先讲 Dockerfile 穿插讲 docker build 最基本的东西 原理 -t -f docker file ...
- python 处理form/data文件上传
处理multipart/form-data 的java serverlet请求接口通过python实现 记住不要在头加:"Content-Type":"multipart ...
- Centos6.10-FastDFS-存储器Http配置
Centos610系列配置 1.准备配置 cd /opt/download/fastdfs-master/confcp http.conf /etc/fdfs/http.confcp mime.typ ...
- C++子类虚函数表指针
最近看剑指offer,记录一下 #include <iostream> #include <string> #include <cctype> #include&l ...
- 原生JS实现旋转木马轮播图特效
大概是这个样子: 首先来简单布局一下(emm...随便弄一下吧,反正主要是用js来整的) <!DOCTYPE html> <html lang="en"> ...
- 如何修改mysql 默认引擎为InnoDB?
1.查看 MySQL支持的引擎有哪些? show engines: 结果: 由上图可以看出,只有 InnoDB 是支持事务的 2.查看默认引擎 show variables like “default ...
- nginx解决WordPress 上传到服务器后页面404错误的方法
人啊,要说你傻了吧,真是啥事都能碰到: 因为换了nginx,把新做的上传到服务器配置好后,就主页和后台能打开,其他的所有页面,全是404,果真404和502是我最讨厌的数字啊,这让我很怀疑人生啊,怀疑 ...
- pycharm中可以运行的程序,在命令行中运行提示模块不存在的问题
运行模块(包含main函数的模块),在模块开头添加以下代码,原因是pycharm运行python脚本时,会自动添加以下代码,将当前库加入到系统库目录集合中,在命令行中运行需要手动添加import os ...
- A Simple Problem with Integers(树状数组区间变化和区间求和)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- 如何在应用程序中使用ML.NET?
https://www.cnblogs.com/shanyou/p/9190701.html ML.NET以NuGet包的形式提供,可以轻松安装到新的或现有的.NET应用程序中. 该框架采用了用于其他 ...