java将⼀个图⽚转换为字符串格式package ylxControlServer.frame.util;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.imageio.ImageIO;
import org.apachemons.logging.Log;
import org.apachemons.logging.LogFactory;
public class ImageToString {
private static Log log = Log(ImageToString.class);
// 根据图⽚地址将图⽚转换为字符串类型的数据
public String imageToString(String picture) {
StringBuffer sb2 = new StringBuffer();
BufferedImage image1 = getImage(picture);
byte[] img = getBytes(image1);
for (int i = 0; i < img.length; i++) {
if (sb2.length() == 0) {
sb2.append(img[i]);
} else {
sb2.append("," + img[i]);
}
}
String();
}
// 将BufferImage 转换为字节数组
private byte[] getBytes(BufferedImage image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(image, "PNG", baos);
} catch (Exception e) {
log.info(e);
}
ByteArray();
}
/
/ 根据图⽚地址得到BufferedImage
public static BufferedImage getImage(String picture) {
try {
BufferedImage bImage = ad(new File(picture));
return bImage;
} catch (Exception ex) {
log.info(ex);
return null;
}
}
}
(上⾯的类将⼀个图⽚转换为字符串)
字符串的格式是:
String a= "1,2,4,4,5,5,5,565";
得到图⽚的字符串后,我们还得将字符串转换为图⽚:
package ylxControlServer.frame.util;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
DateFormat;
SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
public class StringToImage {
public static String fileformat = "png";
public static String fileNameFormat = "yyyy-MM-dd_HH-mm-ss";
// 将字符串格式的图⽚转换为图⽚并保存
public void stringToImage(String string, String saveDir) {
if (ains(",")) {
// 这⾥没有⾃带的那个分割⽅法,原因是分割速度没有这个快,有⼈考证在分割字符长度很⼤的情况下,系统的分割⽅法容易造成内存溢出。            // 还有subString⽅法,不知道最新版本的jdk改了源码了么
String[] imagetemp = split(string, ",");
byte[] image = new byte[imagetemp.length];
for (int i = 0; i < imagetemp.length; i++) {
image[i] = Byte.parseByte(imagetemp[i]);
}
saveImage(image, saveDir);
} else {
// 不能解析格式的字符串
}
}
// 将byte[] 转换为BufferedImage
private BufferedImage readImage(byte[] bytes) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ad(bais);
}
// 保存图⽚
public  String saveImage(byte[] imgages, final String saveDir) {
try {
BufferedImage bis = readImage(imgages);
DateFormat sdf = new SimpleDateFormat(fileNameFormat);
String fileTime = sdf.format(new Date());
final String name = fileTime + "_" + "." + fileformat;
File f = new File(saveDir + name);
boolean istrue = false;
if (f.exists()) {
istrue = ImageIO.write(bis, fileformat, f);
} else {
f.mkdirs();
istrue = ImageIO.write(bis, fileformat, f);
}
if (istrue) {
return name;
}
} catch (Exception e) {
}
return null;
}
// 分割字符串
public String[] split(String s, String token) {
if (s == null)
return null;
if (token == null || s.length() == 0)
return new String[] { s };
int size = 0;
String[] result = new String[4];
while (s.length() > 0) {
int index = s.indexOf(token);
String splitOne = s;
if (index > -1) {
splitOne = s.substring(0, index);
s = s.substring(index + token.length());
} else {
s = "";
}
if (size >= result.length) {
String[] tmp = new String[result.length * 2];
System.arraycopy(result, 0, tmp, 0, result.length);
result = tmp;
}
mkdirs方法
if (splitOne.length() > 0) {
result[size++] = splitOne;
}
}
String[] tmp = result;
result = new String[size];
System.arraycopy(tmp, 0, result, 0, size);
return result;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。