//byte[] image 转换测试
package image;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Image extends JFrame{
byte[] data = null;
InputStream in = null;
public Image() throws IOException {
in = new FileInputStream(
"C:\\Users\\Administrator\\Desktop\\5590f63f69401b07bdc1dfd4.jpg");//图片路径
byte[] b = new byte[1024];
ByteArrayOutputStream output = new ByteArrayOutputStream();
int numByteRead = 0;
while ((numByteRead = in.read(b)) != -1) {
output.write(b, 0, numByteRead);
}
data = output.toByteArray();
ImageIcon img = new ImageIcon(data);
JLabel label=new JLabel(img);
add(label);
setVisible(true);
}
public static void main(String args[]) throws IOException
{
new Image();
}
}
ImageIcon(byte[] imageData) //imageIcon 的构造函数
效果图如下
溜了,还有一堆code: