java字符串和图片相互转换
2024-09-18 22:56:54
package com.thinkgem.jeesite.modules.api.wechat; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; import java.io.*;
/**
* 字符串和图片相互转换
* @author zhouhe
* @date 2020/7/3 17:23
*/
public class ImgToStr {
/**
* 字符串转图片
* @param imgStr --->图片字符串
* @param filename --->图片名
* @return
*/
public static boolean generateImage(String imgStr, String filename) { if (imgStr == null) {
return false;
}
BASE64Decoder decoder = new BASE64Decoder();
try {
// 解密
byte[] b = decoder.decodeBuffer(imgStr);
// 处理数据
for(int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
OutputStream out = new FileOutputStream("D://"+filename);
out.write(b);
out.flush();
out.close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false; } /**
* 图片转字符串
* @param filePath --->文件路径
* @return
*/
public static String getImageStr(String filePath) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(filePath);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// 加密
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
} /*
* 测试代码
*/
public static void main(String[] args) {
String imageStr = getImageStr("D:\\222.png");
System.out.println(imageStr);
System.out.println(imageStr.length());
boolean generateImage = generateImage(imageStr, "223.png");
System.out.println(generateImage);
}
}
最新文章
- yii模块下面的组件
- Index was outside the bounds of the array. (Microsoft.SqlServer.Smo)
- 打通移动App开发的任督二脉、实现移动互联创业的中国梦
- 关于android的单位dp与px
- 实时获取UITextField内容
- css 默认样式
- [GeekBand] 探讨C++新标准之新语法——C++ 11~14
- hdu 1851 A Simple Game 博弈论
- vue中多个input绑定enter按键事件
- javaWeb学习之Listener监听
- Python Scrapy项目创建(基础普及篇)
- 【.NET Core项目实战-统一认证平台】第八章 授权篇-IdentityServer4源码分析
- Word操作——通配符
- android spf中存list<;string>;
- ElasticSearch6.5.0 【安装IK分词器】
- kafka组件makemirror处理跨机房业务的应用
- 中标麒麟Linux7 如何关闭广播消息
- CefSharp 支持mp4
- mysql多列索引
- 在MFC内竟然不可以解析“Mat”
热门文章
- python实现Excel的表头与索引之间的转换
- Unity组件Toggle详解
- 下载接口时出现:Try to run this command from the system terminal. Make sure that you use the correct version of &#39;pip&#39; installed for your Python interpreter located at &#39;D:\python\demo\venv\Scripts\...的错误
- slitaz中tazpkg更改软件源
- 安装MinGW-C++开发环境2--软件安装
- python打包生成exe报错
- vue获取标签对象的方式
- css 卡片hover效果
- drop table后,约束会被删除吗?
- fpga always赋值