Base64对图片进行编码解码
图片编码得到字符串:
package com.zhbr.util;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;public class Base64Utils {/*** 图片转化成base64字符串* @param imgPath* @return*/public static String GetImageStr(String imgPath) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理String imgFile = imgPath;// 待处理的图片InputStream in = null;byte[] data = null;String encode = null; // 返回Base64编码过的字节数组字符串// 对字节数组Base64编码BASE64Encoder encoder = new BASE64Encoder();try {// 读取图片字节数组in = new FileInputStream(imgFile);data = new byte[in.available()];in.read(data);encode = encoder.encode(data);} catch (IOException e) {e.printStackTrace();} finally {try {in.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return encode;}/*** base64字符串转化成图片** @param imgData* 图片编码* @param imgFilePath* 存放到本地路径* @return* @throws IOException*/@SuppressWarnings("finally")public static boolean GenerateImage(String imgData, String imgFilePath) throws IOException { // 对字节数组字符串进行Base64解码并生成图片if (imgData == null) // 图像数据为空return false;BASE64Decoder decoder = new BASE64Decoder();OutputStream out = null;try {out = new FileOutputStream(imgFilePath);// Base64解码byte[] b = decoder.decodeBuffer(imgData);for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {// 调整异常数据b[i] += 256;}}out.write(b);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {out.flush();out.close();return true;}}public static void main(String[] args) throws IOException {String imageStr = Base64Utils.GetImageStr("C:\\Users\\yanni\\Pictures\\27.jpg");System.out.println(imageStr);}
}
读取经过编码的字符串,解码成图片:
/*** 读取txt文件的内容* @param file 想要读取的文件对象* @return 返回文件内容*/
public static String txt2String(File file){String result = "";try{BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件String s = null;while((s = br.readLine())!=null){//使用readLine方法,一次读一行result = result + s;}br.close();}catch(Exception e){e.printStackTrace();}return result;
}public static void main(String[] args) throws IOException, SQLException {File file = new File("C:\\Users\\yanni\\Desktop\\1.txt");String s = txt2String(file);Base64Utils.GenerateImage(s, "C:\\Users\\yanni\\Desktop\\29.jpg");
}
Base64对图片进行编码解码相关推荐
- php代码在线base64解码,Base64在线编码解码实现代码 演示与下载
在线演示地址: gb2312版本 http://tools.jb51.net/tools/base64_decode-gb2312.php utf8版本 http://tools.jb51.net/t ...
- Java中使用Base64进行编码解码的工具类-将验证码图片使用Base64编码并返回给前端
场景 前端使用Vue,验证码图片的src属性来自于后台SpringBoot接口. 后台验证码接口生成验证码图片并将其使用Base64进行编码. 前端就可以直接使用 data:image/png;bas ...
- 性能测试初学_loadrunner base64/md5 编码 解码
参考这3篇文章: MD5: http://bbs.51testing.com/forum.php?mod=viewthread&tid=1111323 base64: http://www.c ...
- Python:data:image/png;base64图片编码解码
编码后的内容,可以在浏览器中直接打开,提示:先去掉回车 data类型的Url大致有下面几种形式 data:,<文本数据> data:text/plain,<文本数据> data ...
- C#中Base64之编码,解码方法
编码 public string EncodeBase64(string code_type,string code) { string encode = ""; byte ...
- 图片对服务器性能,关于适用base64对图片进行编码在服务器上性能的相关讨论
C语言基础(6)-char类型 1. char常量.变量 使用单引号''引起来的就是char的常量 'a'是一个char类型的常量 "a"是一个字符串类型的常量 1是一个int型的 ...
- Base64在线编码工具集
Base64在线编码/解码工具 Base64在线编码/解码工具 URL Base64在线编码解码工具 URL Base64在线编码解码工具 等同于java的Base64.getUrlEncoder() ...
- js base64 编码解码
js base64 编码解码 encode decode,可以直接使用 function Base64() {// private property_keyStr = "ABCDEFGHIJ ...
- php base64解码,PHP Base64 中英文编码 JavaScript 解码
最新PHP Base64 中英文编码 JavaScript 解码 以下是三零网为大家整理的最新PHP Base64 中英文编码 JavaScript 解码的文章,希望大家能够喜欢! function ...
- python使用base64编码解码数据
python使用base64编码解码数据 base64模块是用来作base64编码解码,常用于小型数据的传输.编码后的数据是一个字符串,其包括a-z.A-Z.0-9./.+共64个字符,即可用6个字节 ...
最新文章
- Apex 的异常处理
- Python3.7模块之hashlib
- 使用Caffe预测遇到的问题
- sql性能优化之多表联查
- java 有序容器_Java 容器集合框架概览
- word字体放大后只显示一半_太实用了!5个Word冷门技巧!第3个你肯定不知道!...
- JNI介绍及简单实例
- Keras过拟合相关解决办法
- 递归法:走楼梯; 旋转数组的最小数字(递归法和改进二分法)
- 计算机博弈军旗程序,军棋机器人UCT算法及计算机博弈行为研究
- 传奇服务器玩家信息备份,传奇:史上5大漏洞,损失惨重,盛大被迫将服务器回档2天...
- Maven项目管理实战(二)--pom.xml
- 【安全知识分享】重磅|消防控制室培训.ppt(附下载)
- 友盟分享——微信、腾讯微博、新浪微博分享失败原因分析
- 注塑机摆放间距多少合适_注塑模具加工须知(1):必须注意注塑机注塑参数“拉杆间距”...
- Linux中gcc的常用命令
- 千梦网创108计第三十六计:当地人才招聘网,一个年入50W的战友实操案例
- 单双节锂电池6-8.4V升压9V ,12V,24V快充PD升压系统解决方案
- 2008服务器怎么导入备份数据库文件,内江市审计局 *.dmp文件导入Sql Server 2008 R2的方法...
- AD2428W手册解读之系统调试