将图像数据保存成文本格式(字符串)
2008-03-25 22:44
302 查看
以下函数是将图片数据(包括jpg、png)转换成字符串格式,使得图象数据可以存在文本文件中,但这种方法要牺牲的存储空间比较大,1.3K的图片转换后会变成7.8K,后来发现有更好的算法,他就是Base64算法,同样可以达到存储在文本中的目的,但文件就小好多,大概1.7K左右,不到原文件的两倍。
public static String byteToString(byte b) {
byte high, low;
byte maskHigh = (byte) 0xf0;
byte maskLow = 0x0f;
high = (byte) ((b & maskHigh) >> 4);
low = (byte) (b & maskLow);
StringBuffer buf = new StringBuffer();
buf.append(findHex(high));
buf.append(findHex(low));
return buf.toString();
}
private static char findHex(byte b) {
int t = new Byte(b).byteValue();
t = t < 0 ? t + 16 : t;
if ((0 <= t) && (t <= 9)) {
return (char) (t + '0');
}
return (char) (t - 10 + 'A');
}
public byte[] stringToByte(String s) {
byte imageData[] = new byte[s.length() / 2];
int j = 0;
for (int i = 0; i < s.length(); i += 2) {
try {
imageData[j] = (byte) Integer.parseInt(s.substring(i, i + 2),
16);
j++;
} catch (NumberFormatException e) {
}
}
return imageData;
}
public static String byteToString(byte b) {
byte high, low;
byte maskHigh = (byte) 0xf0;
byte maskLow = 0x0f;
high = (byte) ((b & maskHigh) >> 4);
low = (byte) (b & maskLow);
StringBuffer buf = new StringBuffer();
buf.append(findHex(high));
buf.append(findHex(low));
return buf.toString();
}
private static char findHex(byte b) {
int t = new Byte(b).byteValue();
t = t < 0 ? t + 16 : t;
if ((0 <= t) && (t <= 9)) {
return (char) (t + '0');
}
return (char) (t - 10 + 'A');
}
public byte[] stringToByte(String s) {
byte imageData[] = new byte[s.length() / 2];
int j = 0;
for (int i = 0; i < s.length(); i += 2) {
try {
imageData[j] = (byte) Integer.parseInt(s.substring(i, i + 2),
16);
j++;
} catch (NumberFormatException e) {
}
}
return imageData;
}
相关文章推荐
- json的作用:按规定格式保存js语言中的一组数据并转换为字符串(json是种特殊的容器,一般保存对象和数组数据(可以嵌套),并把它们装换为字符串在网络中传输?))
- js读取本地磁盘文本文件并保存为JSON数据(有格式的文本)
- js读取本地磁盘文本文件并保存为JSON数据(有格式的文本)
- Linux环境C语言把整数转换成字符串再保存该字符串数据为文本
- C语言读取txt里面的图像数据字符串换成十六进制然后保存为bmp格式的图像文件
- matlab imwrite函数保存jpg格式图像丢失数据或改变图像
- 把32位等任意格式图片灰度化并把图像数据保存为文本文件
- 数字图像处理 CImage类的使用与封装(jpg png gif tif bmp等格式图像的加载、数据读写、保存等功能)
- 利用MVC4将数据保存为CSV格式文件并指定目录
- 把数组格式数据转换成字符串存入数据库
- VC 设置 Excel 的单元格格式 数字形式字符串 文本形式
- 将List<Map<String,String>>中的数据输出保存到TXT文本文件中
- VB6.0用GDI+保存图像为BMP\JPG\PNG\GIF格式终结版。
- 整型数据的二进制格式和文本格式的传输方式
- Pandas 读取文本格式数据
- sscanf() - 从一个字符串中读进与指定格式相符的数据。
- 【Unity】将一串字符串保存到Unity中,保存成任意格式
- DataFormats中定义的数据格式字符串
- NYOJ 663 弟弟的作业 sscanf() - 从一个字符串中读进与指定格式相符的数据.
- python:pandas(7),读写文本格式数据