日期:2014-05-20  浏览次数:20877 次

利用byte数组 显示gif图片时 内存无法加载图片
import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Huidu extends JFrame{

JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
static Image yourImg = null;
//static 
static boolean isGreyscaleImage(PixelGrabber pg) throws InterruptedException
{
//System.out.println(pg.getPixels() instanceof byte[]);
return pg.getPixels() instanceof byte[];
}

public Huidu() throws InterruptedException, IOException
{
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocation(0, 0);
this.setSize(this.getToolkit().getScreenSize().width, this.getToolkit().getScreenSize().height);
this.setVisible(true);

chuliImage();
//Image img1 = new I
jp1.add(new JLabel(new ImageIcon("Huidu.gif")));
// jp2.add(new JLabel(new ImageIcon(img)));
this.getContentPane().add(jp1, BorderLayout.WEST);
this.getContentPane().add(jp2, BorderLayout.EAST);
//this.pack();
}

public static void chuliImage() throws InterruptedException, IOException
{
int width = 0;
int height = 0;
byte[] dataByte = null;
int [] dataInt = null;
//////////////
byte[] dataByte1 = null;
//////////
byte [] test = {1, 2, 1, 2, 4, 2, 1, 2, 1}; //处理矩阵
Image image = Toolkit.getDefaultToolkit().getImage("huidu.gif");
//System.out.println(image.);
PixelGrabber grabber = new PixelGrabber(image, 0, 0, -1, -1, false);
if(grabber.grabPixels())
{
width = grabber.getWidth();
height = grabber.getHeight();

if(isGreyscaleImage(grabber))
{
dataByte = (byte[]) grabber.getPixels();
}
else
{
dataInt = (int[]) grabber.getPixels();
}
}

byte [] tempData = new byte[height*width];
//
// 保存图像原来的数据。
for(int i = 0; i<height*width; i++)
{

tempData[i] = dataByte[i];
}


int i = 1, j = 1;
for(i = 1; i<height-1; i++)
{
for(j = 1; j<width-1; j++)
{
int res = ((dataByte[i*width+j-width-1]*test[0]+dataByte[i*width+j-width]*test[1]
+dataByte[i*width+j-width+1]*test[2]+dataByte[i*width+j-1]*test[3] 
+dataByte[i*width+j]*test[4]+dataByte[i*width+j+1]*test[5] 
+dataByte[i*width+j+width-1]*test[6]+dataByte[i*width+j+width]*test[7]+dataByte[i*width+j+width+1]*test[8])/16);  

if(res<-128)
res = -128;
if(res>127)
res = 127;


tempData[i*width+j] = (byte) res;
}
}
System.out.println(tempData.length);
for(int temp = 0; temp<3000; temp++)
{
tempData[temp] = dataByte[temp];
}

System.out.println(tempData.length);
System.out.println(dataByte.length);
yourImg = Toolkit.getDefaultToolkit().createImage(dataByte); //ri.data就是个byte数组
JFrame jj = new JFrame();
jj.getContentPane().add(new JLabel(new ImageIcon(yourImg)), BorderLayout.NORTH);
MediaTracker t = new MediaTracker(jj);  
t.addImage(yourImg, 0);  
t.waitForAll(); 
System.out.println(t.isErrorAny());
//System.out.println(t.);
// JFrame jj = new JFrame();
// jj.getContentPane().add(yourImg, );