2009-11-08 52 views
0
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.*; 
import java.io.*; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class Video implements ActionListener 
{ 
    static int width=480; 
    static int height=368; 
    static JFrame frame = new JFrame(); 
    static JButton button = new JButton("Submit"); 
    static BufferedImage img = new BufferedImage((int) (width), (int) (height), BufferedImage.TYPE_INT_RGB); 
    static BufferedImage img1[] = new BufferedImage[60]; 
    static { 
     for (int i = 0; i < img1.length; i++) { 
     img1[i] = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
     } 
    } 
    public static void main(String[] args) 
    { 
     Video V1 = new Video(); 
     String fileName = args[0]; 
     try { 
      File file = new File(fileName); 
      InputStream is = new FileInputStream(file); 

      long len = file.length(); 
      byte[] bytes = new byte[(int)len]; 
      int offset = 0; 
      int numRead = 0; 
      int ind =0; 
      int[][] pixarray=new int[height+100][width+100]; 
      int[][] red=new int[width*2][height*2]; 
      int[][] green=new int[width*2][height*2]; 
      int[][] blue=new int[width*2][height*2]; 
      while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { 
       offset += numRead; 
      } 
      for(int frames=0;frames<60;frames++) 
      { 
       ind=height*width*frames*3; 
        for(int y = 0; y < height; y++) 
       { 
         for(int x = 0; x < width; x++) 
         { 
          byte a = 0; 
          byte r = bytes[ind]; 
          byte g = bytes[ind+height*width]; 
          byte b = bytes[ind+height*width*2]; 
          int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); 
          pixarray[y][x]=pix; 
          red[y][x] = (pix >>> 16) & 0xff; 
          green[y][x] = (pix >>> 8) & 0xff; 
          blue[y][x] = pix & 0xff; 
          img1[frames].setRGB(x,y,pix); 
          ind++; 
         } 
       } 
      } 

     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 


     JLabel label = new JLabel(new ImageIcon(img1[50])); 

     frame.setLayout(new FlowLayout()); 
     //frame.setSize(200,100); 
     frame.setVisible(true); 
     // Button button = new Button("Submit"); 
     // frame.add(button); 
     frame.getContentPane().add(label, BorderLayout.CENTER); 
     frame.getContentPane().add(button, BorderLayout.CENTER); 
     frame.pack(); 
     frame.setVisible(true); 

     button.addActionListener(V1); 

    } 
    public void actionPerformed(ActionEvent e) { 
     System.out.println("1"); 
     for(int i=0;i<img1.length;i++) 
     { 
      try { 
       Thread.sleep(500); 
      } catch (InterruptedException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

      System.out.println(img1.length); 
     } 


    } 
} 

是我的代碼。我的img1是一組幀。在這種情況下我的視頻有60幀。之後顯示圖像後面板上有時間間隔

我想添加每個幀到我的面板與時間差距。我無法做到這一點,因爲每次我將它添加到面板上時,我的ActionEvent的表演很奇怪。請幫助我。

回答

0

您可以創建一個TimerTask

 
class VideoTask extends TimerTask { 
    private Frame frame; 
    private int frameId; 
    public void run() { 
    frame.drawImage(....); 
    frameId++; 
    } 
} 

而且在動作偵聽器按鈕 - 計劃任務:

 
VideoTask videoTask = new VideoTask(frame); 
videoTask.schedule(..); 
3

使用一個Swing計時器(未一個TimerTask)。定時器觸發時,代碼將在EDT中執行,以便您可以安全地重置JLabel的圖標。所以我會先從BufferedImages創建ImageIcons並將這些圖標存儲在您的數組中。

請參閱How to Use Timers的Swing教程瞭解更多信息。您可能還想查看Concurreny上的部分,瞭解爲什麼在EDT中執行代碼非常重要。