java实现图片与base64字符串之间的转换
2016-04-18 14:08
846 查看
http://blog.sina.com.cn/s/blog_6400e5c50101qtr3.html
package com.zxc.testjava.ant;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class TestChangeImage {
public static void main(String[] args) {
String strImg = GetImageStr();
System.out.println(strImg);
GenerateImage(strImg);
}
// 图片转化成base64字符串
public static String GetImageStr() {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = "C:/Users/Star/Desktop/test.png";// 待处理的图片
InputStream in = null;
byte[] data = null;
// 读取图片字节数组
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);// 返回Base64编码过的字节数组字符串
}
// base64字符串转化成图片
public static boolean GenerateImage(String imgStr) { // 对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) // 图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 生成jpeg图片
String imgFilePath = "C:/Users/Star/Desktop/test22.png";// 新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}
package com.zxc.testjava.ant;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class TestChangeImage {
public static void main(String[] args) {
String strImg = GetImageStr();
System.out.println(strImg);
GenerateImage(strImg);
}
// 图片转化成base64字符串
public static String GetImageStr() {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = "C:/Users/Star/Desktop/test.png";// 待处理的图片
InputStream in = null;
byte[] data = null;
// 读取图片字节数组
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);// 返回Base64编码过的字节数组字符串
}
// base64字符串转化成图片
public static boolean GenerateImage(String imgStr) { // 对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) // 图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 生成jpeg图片
String imgFilePath = "C:/Users/Star/Desktop/test22.png";// 新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}
相关文章推荐
- Java NIO使用及原理分析 (一)(转)
- 线程示例:多线程卖票
- Java NIO系列1:从操作系统的角度剖析I/O
- 利用Eclipse替换项目中的图片
- 创建一个Spring+SpringMVC+MyBatis+MySql+Maven项目需要注意的地方
- String在JAVA里是固定长度的吗?为什么可用“+”连接
- 使用Java 导入Zip文件遇到中文命名乱码问题
- java常用同步工具类
- java常用算法之返回目标数字在有序数组中的位置(假设有序数组中不存在重复数字)
- 深入理解 Java中的 流 (Stream)
- java判断字符串是否为空的方法总结
- Java8之默认方法和静态接口方法
- springMVC中from表单提交对象
- Java命名规范
- java NIO(转)
- JAVA锁机制
- java中四种操作(dom、sax、jdom、dom4j)xml方式详解与比较
- 在eclipse中使用javap
- java编程之SwingUtilities中invokeLater和invokeAndWait的使用
- 总结了一些java基本的一些陷阱