【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“MyImageDialog.java”一文,供大家参考学习!
package com.dialog;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import javax.swing.*;
import com.main.FirstCapture;
import com.sun.image.codec.jpeg.*;
import com.util.ImagePanel;
/*
* 保存图片方法,用于获取获取的静态图片,并将其经行保存,如果保存的图片名称在指定的路径中存在,用户可确认是否覆盖该图片
* */
public class MyImageDialog extends JFrame implements ActionListener {
private ImagePanel iPanel=null;
public JButton save = null;
FirstCapture fristCaptrue = new FirstCapture("");
private JButton stop = null;
private Image image = null;
private String name = null;
//构造方法,设置对话框的初始布局
public MyImageDialog(String title) {
super(title);
setLayout(null);
iPanel=new ImagePanel();
iPanel.setBounds(0, 0, 290, 310);
add(iPanel);
URL framURL = getClass().getResource("/image/6.jpg");
ImageIcon frameIcon = new ImageIcon(framURL);
ImageIcon frameIcon2 = new ImageIcon(framURL);
JLabel jlabel = new JLabel(frameIcon);
jlabel.setBounds(0, 0, 290, 5);
add(jlabel);
JLabel jlabel2 = new JLabel(frameIcon2);
jlabel2.setBounds(0, 0, 5, 245);
add(jlabel2);
JLabel jlabel4 = new JLabel(frameIcon);
jlabel4.setBounds(290, 0, 5, 245);
add(jlabel4);
JLabel jlabel3 = new JLabel(frameIcon);
jlabel3.setBounds(0, 245, 295, 5);
add(jlabel3);
setBounds(100, 100, 303, 330);
save = new JButton("保存");
save.setBounds(70, 260, 60, 30);
add(save);
stop = new JButton("关闭");
stop.setBounds(170, 260, 60, 30);
add(stop);
image = fristCaptrue.getImage();
iPanel.setImage(image);
save.addActionListener(this);
stop.addActionListener(this);
}
/*public void setImage(Image image){
iPanel.setImage(image);
}*/
public void actionPerformed(ActionEvent e) {
//单击”保存“按钮触发的单击事件
if(e.getSource() == save){
if (image == null) {
JOptionPane.showMessageDialog(this,"无法实现保存图片操作",
"消息对话框",JOptionPane.WARNING_MESSAGE);
return;
}
try {
MyFileChooser myfile = new MyFileChooser();
JFileChooser jfc = myfile.getFileChooser();
name = jfc.getCurrentDirectory() + "\\"
+ jfc.getSelectedFile().getName();
String result;
result = ".jpg";
File file1 = new File(name + result);
if(file1.exists() == false){
saveImage(image, name + result);
}
else{
int n = JOptionPane.showConfirmDialog(this,
"文件已存在,确定要覆盖吗?", "确认对话框",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION){
saveImage(image, name + result);
}
else if(n == JOptionPane.NO_OPTION){
return;
}
}
} catch (NullPointerException ee) {
System.out.println("没有保存的图片");
}
}
//单击“退出”按钮触发的方法
if(e.getSource() == stop){
dispose();
}
}
//保存图片方法
public static void saveImage(Image image, String path) {
BufferedImage bi = new BufferedImage(image.getWidth(null), image
.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(image, null, null);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
JPEGImageEncoder jiec = JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParam jpp = jiec.getDefaultJPEGEncodeParam(bi);
jpp.setQuality(0.5f, false);
jiec.setJPEGEncodeParam(jpp);
try {
jiec.encode(bi);
fos.close();
} catch (ImageFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}