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和图片的相互转换的更多相关文章

  1. c# 中base64字符串和图片的相互转换

    c#base64字符串转图片用到了bitmap类,封装 GDI+ 位图,此位图由图形图像及其特性的像素数据组成. Bitmap 是用于处理由像素数据定义的图像的对象. 具体bitmap类是什么可以自己 ...

  2. 页面以base64输出图片

    <% //读取文件路径,输出base64 编码 System.IO.FileStream stream = System.IO.File.OpenRead(ViewBag.FilePath); ...

  3. base64和图片的转换

    /// <summary> /// base64转图片 /// </summary> /// <param name="strBase64">& ...

  4. 通过data:image/png;base64把图片直接写在src里

    从网上下了个源文件查看时候发现了引用图片的地址不是在本地上的,而是后面跟了一大串字符data:image/png;base64...查了一下资料分析如下: 关于用base64存储图片 网页上有些图片的 ...

  5. c# API接收Base64转图片

    /// <summary> /// API接收Base64转图片 /// </summary> /// <param name="Img">图片 ...

  6. base64加密图片处理

    场景:下载html中内嵌的base64加密图片 举个例子,博客园的插入图片有两种方式,一是引用图片链接,二是直接粘贴2进制图片文件.以第二种方式的图片则是以base64加密的方式内嵌在html页面中. ...

  7. 前端上传 base64 编码图片到七牛云存储

    参考文档 如何上传base64编码图片到七牛云 调试过程 文档中分别有 java 和 html 的 demo,可以根据文档示例调试. 下面是我调试的过程,可以作为参考,特别注意的是,如果需要给文件起名 ...

  8. 在HTML中显示base64 img 图片

    base64的图片可以直接显示在网页上面 <img src=“data:image/png;base64,******************************************** ...

  9. 利用base64展示图片

    其实很简单,格式如下: <img src="data:image/jpg;base64,具体的编码值" /> 支持的类型有: data:,文本数据 data:text/ ...

随机推荐

  1. hadoop学习笔记(七):hadoop2.x的高可用HA(high avaliable)和联邦F(Federation)

    Hadoop介绍——HA与联邦 0.1682019.06.04 13:30:55字数 820阅读 138 Hadoop 1.0中HDFS和MapReduce在高可用.扩展性等方面存在问题: –HDFS ...

  2. app内区域截图利用html2Canvals保存到手机 截屏 (html2Canvals2版本1.0.0)

    html2canvas($(], {scale:,logging:false,useCORS:true}).then(function(canvas) { var dataUrl = canvas.t ...

  3. paramiko-tools

    own dev # coding=utf-8 import paramiko import os import logging import json import unittest from sta ...

  4. H5拖动事件复习

    定义和用法 ondrag 事件在元素或者选取的文本被拖动时触发. 拖放是 HTML5 中非常常见的功能. 更多信息可以查看我们 HTML 教程中的 HTML5 拖放. 注意: 为了让元素可拖动,需要使 ...

  5. Vue - 实现双击显示编辑框;自动聚焦点击的显示框;点击编辑框外的地方,隐藏编辑框

    实现这三个功能的踩坑记录. 1. 需求 在Vue中,有一个input, 双击时编辑文本,点击该input节点外的其他地方,则取消编辑. 那么这里有三个要实现的地方 第一是双击显示编辑框. 第二是自动聚 ...

  6. Git的安装和使用(转载)

    转载博客连接:https://www.cnblogs.com/qijunjun/p/7137207.html 实际项目开发中,我们经常会用一些版本控制器来托管自己的代码,今天就来总结下Git的相关用法 ...

  7. css3的一些特效

    前段时间有位同事分享了一个网站,里边是一些css3特效,看着挺好,分享一下: [http://daneden.github.io/animate.css/ ] 所有的特效都集中在一个css层叠样式表中 ...

  8. linux下安装mongo数据库存

    https://www.runoob.com/mongodb/mongodb-linux-install.html 安装完后,要重启一下,否则无法运行./mongod 下载完安装包,并解压 tgz(以 ...

  9. es 的搜索

    kibana语言查询 q指定查询语句 df默认字段,不指定时 sort排序/from和size用于分页 profile可以查看查询是如何倍执行的 查询title的值等于2012 的数据 GET /mo ...

  10. 让 typora和word一样好用

    让 typora和word一样好用  :https://github.com/itcastWsy/typora_copy_images typora是一款支持实时预览的markdown编辑器,作者在使 ...