gpt4 book ai didi

java - 我无法将图像(中间混合)保存到文件?

转载 作者:太空宇宙 更新时间:2023-11-04 13:57:29 29 4
gpt4 key购买 nike

我尝试查看 Oracle 关于写入/保存图像的建议,但我所做的一切都不起作用。我有一个开始按钮,可以开始将两张图像从一张图像混合到另一张图像。然后是一个停止按钮来中途停止混合(或任何你想要的地方),然后我有一个 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,它们根本不包含太多代码。有人可以帮忙吗?

最佳答案

要使用 JFileChooser 并最终获得用户友好的文件选择器,您可以这样做:

 private static Frame workFrame;

public static Frame getWorkFrame() {
return workFrame;
}

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

然后按以下方式修改保存方法(签名更改,并且我还注释了使用 Scanner 的代码并将其替换为 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);
}

在我的测试中它运行良好。测试一下并告诉我是否有效

关于java - 我无法将图像(中间混合)保存到文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29678710/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com