/**
*
* @说明 :1、读取图片并转成byte[],
* 2、讲byte[]转成图片
*/
@Test
public void imByte(){
//从指定路径读取图片并存到content[]字节组里面,
byte content[] =null;
byte[] temp = new byte[1024];
try{
BufferedInputStream in =new BufferedInputStream(new FileInputStream("c:/test.png"));
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
int size = 0;
while((size = in.read(temp)) != -1){
out.write(temp, 0, size);
}
in.close();
content = out.toByteArray();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("content字节组大小:"+content.length);
/**
* 将字节组里的内容转成图片
*/
//第一种方式
try {
OutputStream out =new FileOutputStream(new File("c:\\test1.png"));
out.write(content);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
//第二种方式
String img = "c:\\test2.png";
try{
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(img));
imageOutput.write(content, 0, content.length);
imageOutput.close();
} catch(Exception ex){
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
*
* @说明 :1、读取图片并转成byte[],
* 2、讲byte[]转成图片
*/
@Test
public void imByte(){
//从指定路径读取图片并存到content[]字节组里面,
byte content[] =null;
byte[] temp = new byte[1024];
try{
BufferedInputStream in =new BufferedInputStream(new FileInputStream("c:/test.png"));
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
int size = 0;
while((size = in.read(temp)) != -1){
out.write(temp, 0, size);
}
in.close();
content = out.toByteArray();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("content字节组大小:"+content.length);
/**
* 将字节组里的内容转成图片
*/
//第一种方式
try {
OutputStream out =new FileOutputStream(new File("c:\\test1.png"));
out.write(content);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
//第二种方式
String img = "c:\\test2.png";
try{
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(img));
imageOutput.write(content, 0, content.length);
imageOutput.close();
} catch(Exception ex){
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}