2015-04-16 18 views
1

我已經嘗試了看寫/保存圖像的Oracle建議,但沒有任何工作。我有一個開始按鈕,開始將2張圖像從一個圖像混合到另一個圖像。然後一個停止按鈕來中途停止混合(或任何你想要的),然後我有一個saveImage按鈕,並希望它捕獲圖像的當前狀態並將其保存到文件。但是如何?這裏是從Frame.java我無法將我的圖像(中間混合)保存到文件?

import java.awt.AlphaComposite; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

class Frame extends JPanel implements ActionListener { 

private static final long serialVersionUID = 1L; 
private Image MeSmaller1; 
private Image MeSmaller2; 
protected Timer timer; 
private float alpha; 

JPanel pnlButton = new JPanel(); 
static JButton btnStartBlend = new JButton("Start Blend"); 
static JButton btnStopBlend = new JButton("Stop Blend"); 
static JButton saveImage = new JButton("Save Image To File"); 

public Frame() { 

    loadImages(); 
    initTimer(); 

    pnlButton.add(btnStartBlend); 
    this.add(pnlButton); 
    pnlButton.add(btnStopBlend); 
    this.add(pnlButton); 
    pnlButton.add(saveImage); 
    this.add(pnlButton); 

    addListeners(); 
} 
    // start button actionlistener 
public void addListeners() { 
    btnStartBlend.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      initTimer(); 
      timer.start(); 

      System.out.println("Timer started"); 
     } 
    }); 
    // stop button actionlistener 
    btnStopBlend.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      // initTimer(); 
      timer.stop(); 

      System.out.println("Timer stopped"); 
     } 
    }); 
    // Save button actionlistener 
    saveImage.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 

      System.out.println("Save image clicked"); 
     } 
    }); 
} 

private void loadImages() { 
    MeSmaller1 = new ImageIcon("MeSmaller1.jpg").getImage(); 
    MeSmaller2 = new ImageIcon("MeSmaller2.jpg").getImage(); 
} 

public void initTimer() { 

    timer = new Timer(1000, this); 
    timer.start(); 
    timer.stop(); 
    alpha = 1f; 
} 

private void doDrawing(Graphics g) { 
    Graphics2D g2Dim = (Graphics2D) g; 
    // below sets the size of picture 
    BufferedImage buffImage = new BufferedImage(400, 600, 
      BufferedImage.TYPE_INT_ARGB); 


    Graphics2D gBuffI = buffImage.createGraphics(); 
    AlphaComposite aComp = AlphaComposite.getInstance(
      AlphaComposite.SRC_OVER, alpha); 
    // decides where images are drawn in JFrame 
    gBuffI.drawImage(MeSmaller1, 28, 55, null); 
    gBuffI.setComposite(aComp); 
    gBuffI.drawImage(MeSmaller2, 30, 48, null); 
    g2Dim.drawImage(buffImage, 10, 10, null); 
} 

public static void saveToFile(BufferedImage img) { 


} 
@Override 

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    doDrawing(g); 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    alpha -= 0.1; 
    if (alpha <= 0) { 
     alpha = 0; 
     timer.stop(); 
     System.out.println("Morph Finished please restart."); 
    } 
    repaint(); 
} 

}

代碼誰能幫助這裏它只是不工作。還有另一個類PictureMorph.java

import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class PictureMorph extends JFrame { 

private static final long serialVersionUID = 1L; 

    public PictureMorph() { 
    initUI(); 
} 

private void initUI() { 
    JFrame frame = new JFrame ("Image Morph"); 
    setTitle("Facial Image Manipulation"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    add(new Frame()); 
    // below set Frame Size around image 
    setSize(380, 470); 
    setLocationRelativeTo(null); 

} 

    public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      PictureMorph picMorph = new PictureMorph(); 
      picMorph.setVisible(true); 
     } 
    }); 
} 
    } 

3班(我認爲可能是多餘的現在居然?)叫BtnStartBlendListener,BtnStopBlendListener,SaveImageListener不包含多少代碼在所有。誰能幫忙?

回答

0

要使用一個JFileChooser,並與用戶友好的文件選擇結束了,你可以這樣做:

private static Frame workFrame; 

public static Frame getWorkFrame() { 
     return workFrame; 
} 

public static void setWorkFrame(Frame frame) { 
    workFrame = frame; 
} 

然後修改保存方法通過以下方式(簽名的變化,也是我曾評論使用掃描儀和替換它由JFileChooser中的代碼)

public static void save(BufferedImage img, Frame frame) { 

    // Scanner scan = new Scanner(System.in); 
    // System.out.println("Enter the file name: "); 
    // String fileFullPath = scan.next(); 
     String fileFullPath = getFileToSaveTo(frame); 
     file = new File(fileFullPath); 
     saveToFile(img, file); 

} 

添加以下方法:

public static String getFileToSaveTo(Frame frame) { 
     JFileChooser fc=new JFileChooser(); 
      int returnVal=fc.showOpenDialog(frame); 
      if (returnVal == JFileChooser.APPROVE_OPTION) { 
      return fc.getSelectedFile().getAbsolutePath(); 
      } 
     else { 
      return null; 
      } 
    } 

在你的主要方法有一個像Frame.setWorkFrame(fr);通話中FR被定義爲Frame fr = new Frame();

這應該使它與JFileChooser中工作。例如,下面是我如何調用該程序在我的主要方法

public static void main(String[] args) { 
     JFrame theFrame = new JFrame("Testing catess..."); 
     theFrame.setSize(400, 400); 
     Frame fr = new Frame(); 
     theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     theFrame.add(fr, BorderLayout.CENTER); 
     theFrame.setVisible(true); 
     Frame.setWorkFrame(fr); 
    } 

它在我的測試中正常工作。測試並讓我知道它是否工作

+0

評論是不是擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/75538/discussion-on-answer-by-alainlompo-i-cant-save-my-image-mid-blend-to-文件)。 – Taryn

+1

@bluefeet對不起,感謝你提醒我們這個規則。最好的祝願。 – alainlompo

0

由於您已將saveToFile方法留空,因此我認爲這是您的主要問題。

對於saveToFile方法,您可以使用javax.imageio.ImageIO。我建議你傳遞一個File對象參數給你的方法,以便它知道在哪裏保存圖像。

public static void saveToFile(BufferedImage img, File file) { 
    String filename = file.getName(); 

    String suffix = filename.substring(filename.lastIndexOf('.') + 1); 
    suffix = suffix.toLowerCase(); 

    if (suffix.equals("jpg") || suffix.equals("png")) { 
     try { ImageIO.write(img, suffix, file); } 
     catch (IOException e) { e.printStackTrace(); } 
    } 
    else { 
     System.out.println("Error: filename must end in .jpg or .png"); 
    } 

} 

希望這有助於

[編輯]

文件的構造函數允許您與代表文件路徑字符串實例化File對象。

例子:假設我想在保存它,讓我們說C:\測試\ image1.jpg,我會做這個

File myFile = new File("C:\\Tests\\image1.jpg") 

,其中測試代表現有的文件夾和image1.jpg當然您將在其中保存圖像的新文件的名稱。

所以假設IMG是包含引用您的BufferedImage的一個變量對象,你會調用上面的方法與

Frame.saveToFile(img, myFile); 

其中MYFILE是上述文件對象。

您還可以添加一個靜態方法,例如下面的方法讓用戶指定文件的路徑。我用掃描儀得到它快速,但對於更多的用戶 - 友好的對話框中,使用一個JFileChooser(例子是here

public static void save(BufferedImage img) { 
     Scanner scan = new Scanner(System.in); 
     System.out.println("Enter the file name: "); 
     String fileFullPath = scan.next(); 
     File file = new File(fileFullPath); 
     saveToFile(img, file); 

    } 
+0

嗨,感謝這看起來不錯,我如何通過File對象參數?還有什麼我會放在按鈕saveImage ActionListener的按鈕,以便它按下時啓動保存該文件按鈕連接?如果你不介意我要求更多的幫助...... @alainlompo你的幫助非常感謝。 Caroline – Catess76

+0

@ Catess76,在你的saveImage ActionListener中,添加一個對Frame.saveToFile的調用,傳入你的BufferedImage引用變量和一個類似於我添加的File對象的實例。我希望這有幫助。在這種情況下,如果它解決了你的問題不要忘記投我的答案或接受它 – alainlompo

+0

這是代碼,因爲它現在從主類叫Frame,我仍然試圖讓它saveImage一旦保存圖像按鈕被按下。 {link}(http://pastebin.com/hG6frc73)希望有人可以幫忙:-) – Catess76

0

您遇到的問題大多涉及到的事實,你被聲明爲final的一些變量,特別是BufferedImage和File對象(它們是空的,因爲你讓它們最終它們的值不能改變)

我糾正了這些問題。這裏是整個代碼,除了import語句與您在源代碼鏈接中的代碼相同外。

單擊保存按鈕後,輸入要保存的文件的完整路徑:示例C:\ test1.jpg在控制檯中。

class Frame extends JPanel implements ActionListener { 

    private static final long serialVersionUID = 1L; 
    static BufferedImage bufferedImage = null; // Don't need to make this  final 
    static File file = null; // Don't need to make this final 
    private Image MeSmaller1; 
    private Image MeSmaller2; 
    protected Timer timer; 
    private float alpha; 

    JPanel pnlButton = new JPanel(); 
    static JButton btnStartBlend = new JButton("Start Blend"); 
    static JButton btnStopBlend = new JButton("Stop Blend"); 
    static JButton saveImage = new JButton("Save Image To File"); 

    public Frame() { 

      loadImages(); 
      initTimer(); 

      pnlButton.add(btnStartBlend); 
      this.add(pnlButton); 
      pnlButton.add(btnStopBlend); 
      this.add(pnlButton); 
      pnlButton.add(saveImage); 
      this.add(pnlButton); 

      addListeners(); 
    } 
      // start button action listener 
    public void addListeners() { 
      btnStartBlend.addActionListener(new ActionListener() { 

        public void actionPerformed(ActionEvent e) { 
          initTimer(); 
          timer.start(); 

          System.out.println("Timer started"); 
        } 
      }); 
      // stop button actionlistener 
      btnStopBlend.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
          // initTimer(); 
          timer.stop(); 

          System.out.println("Timer stopped"); 
        } 
      }); 

      // Save button actionlistener 
      saveImage.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent saveImage) { 
          save(bufferedImage); 
          System.out.println("Save image clicked"); 
        } 
      }); 
    } 

    private void loadImages() { 
      // I did my local test using 
      //MeSmaller1 = new ImageIcon("C:\\Tests\\Alain_Lompo.jpg").getImage(); 
      //MeSmaller2 = new ImageIcon("C:\\Tests\\Alain_Lompo.jpg").getImage(); 

      MeSmaller1 = new ImageIcon("MeSmaller1.jpg").getImage(); 
      MeSmaller2 = new ImageIcon("MeSmaller2.jpg").getImage(); 
    } 

    public void initTimer() { 

      timer = new Timer(1000, this); 
      timer.start(); 
      timer.stop(); 
      alpha = 1f; 
    } 

    private void doDrawing(Graphics g) { 
      Graphics2D g2Dim = (Graphics2D) g; 
      // below sets the size of picture 
      bufferedImage = new BufferedImage(400, 600, 
          BufferedImage.TYPE_INT_ARGB); 
      Graphics2D gBuffI = bufferedImage.createGraphics(); 
      AlphaComposite aComp = AlphaComposite.getInstance(
          AlphaComposite.SRC_OVER, alpha); 
      // decides where images are drawn in JFrame 
      gBuffI.drawImage(MeSmaller1, 28, 55, null); 
      gBuffI.setComposite(aComp); 
      gBuffI.drawImage(MeSmaller2, 30, 48, null); 
      g2Dim.drawImage(bufferedImage, 10, 10, null); 
    } 



    public static void save(BufferedImage img) { 
     Scanner scan = new Scanner(System.in); 
     System.out.println("Enter the file name: "); 
     String fileFullPath = scan.next(); 
     file = new File(fileFullPath); 
     saveToFile(img, file); 

    } 

    public static void saveToFile(BufferedImage img, File file) { 
     String filename = file.getName(); 

     String suffix = filename.substring(filename.lastIndexOf('.') + 1); 
     suffix = suffix.toLowerCase(); 

     if (suffix.equals("jpg") || suffix.equals("png")) { 
      try { ImageIO.write(img, suffix, file); } 
      catch (IOException e) { e.printStackTrace(); } 
     } 
     else { 
      System.out.println("Error: filename must end in .jpg or .png"); 
     } 

    } 


    @Override 
    public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      doDrawing(g); 
    } 

    public void actionPerformed(ActionEvent e) { 
      alpha -= 0.1; 
      if (alpha <= 0) { 
        alpha = 0; 
        timer.stop(); 
        System.out.println("Morph Finished please restart."); 
      } 
      repaint(); 
    } 

    public static void main(String[] args) { 
     JFrame theFrame = new JFrame("Testing catess..."); 
     theFrame.setSize(400, 400); 
     Frame fr = new Frame(); 
     theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     theFrame.add(fr, BorderLayout.CENTER); 
     theFrame.setVisible(true); 

    } 
} 
+0

嗨,對不起,我回來了,不得不爲我的兒子找東西!花了很長時間..我現在會嘗試@alainlompo :-)謝謝 – Catess76

+0

嗨,當我點擊保存圖像按鈕時,出現輸入文件名的選項,但是當我輸入文件名時出現錯誤。然而...如果它提出了保存對話框,我很喜歡它,所以我可以保存到我可以選擇的文件。我們現在肯定越來越接近我期待的結果:-)非常感謝你的時間。 @alainlompo – Catess76

+0

當然,我可以在幾分鐘內設置JFileChooser版本 – alainlompo

相關問題