请问将图片转换成字符串如何实现

来源:百度知道 编辑:UC知道 时间:2024/05/25 10:52:19
有时觉得图片很大,如果可以转换成字符串就小了很多~~~
如何将图片转换成字符串,也可以把字符串再还原成图片~~~
不知道有没有这样的工具~~

try{

OutputStream o = response.getOutputStream();

// 将图片转换成字符串

File f = new File("f:\\Vista.png");

FileInputStream fis = new FileInputStream( f );

byte[] bytes = new byte[fis.available()];

fis.read(bytes);

fis.close();

// 生成字符串

String imgStr = byte2hex( bytes );

System.out.println( imgStr);

// 将字符串转换成二进制,用于显示图片

// 将上面生成的图片格式字符串 imgStr,还原成图片显示

byte[] imgByte = hex2byte( imgStr );

InputStream in = new ByteArrayInputStream( imgByte );

byte[] b = new byte[1024];

int nRead = 0;

while( ( nRead = in.read(b) ) != -1 ){

o.write( b, 0, nRead );

}

o.flush();

o.close(