package org.zfz.model;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.zfz.view.MainFrame;
//模型
public class Photo {
BufferedImage bi;
private MainFrame win;
private float[][] data = {
// Roberts算子
{ 0f, 1f, -1f, 0f },
// Prewitt算子
{ 1f, 0f, -1f, 1f, 0f, -1f, 1f, 0f, -1f },
// Sobel算子
{ 1f, 0f, -1f, 2f, 0f, -2f, 1f, 0f, -1f },
// Laplacian算子
{ 0.0f, -1.0f, 0.0f, -1.0f, 4.0f, -1.0f, 0.0f, -1.0f, 0.0f },
// Log(Laplacian-Gauss)算子
{ 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f, -2.0f, -1.0f, 0.0f,
-1.0f, -2.0f, 16.0f, -2.0f, -1.0f, 0.0f, -1.0f, -2.0f, -1.0f, 0.0f,
0.0f, 0.0f, -1.0f, 0.0f, 0.0f } };
private ImageIcon ii;//图片
private JFileChooser jfc = new JFileChooser("c:\\");//默认路径
public Photo() {
super();
}
public Photo(MainFrame win) {
super();
this.win = win;
}
//处理图像公共类
public void handleImage(int i){
Kernel kernel = null;
try {
// 获取处理图像的高度和宽度
int width = ii.getImage().getWidth(null);
int height = ii.getImage().getHeight(null);
// sourceBf的存在是为了要把ii转换成BufferedImage
BufferedImage sourceBf = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = sourceBf.getGraphics();
// 将待处理图像绘制到源BufferedImage对像图像中
g.drawImage(ii.getImage(), 0, 0, null);
// 存处理后的BufferedImage(处理后的图像)
BufferedImage targetBf = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
//根据传来的i来调用不同的矩阵
if(i==0){
kernel = new Kernel(2, 2, data[i]); // 根据data建个2X2的矩阵
}else if(i==4){
kernel = new Kernel(5, 5, data[i]); // 根据data建个5X5的矩阵
}else{
kernel = new Kernel(3, 3, data[i]); // 根据data建个3X3的矩阵
}
// ConvolveOp 专门用于图像的卷积
ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
cop.filter(sourceBf, targetBf); // 过滤后生成新的BufferedImage
bi = targetBf;
// 获取处理后的图像并设置到目标标签中
win.getJlr().setIcon(new ImageIcon(targetBf));
} catch (Exception a) {
JOptionPane.showMessageDialog(null, "请加载图片");
}
}
// 加载图片
public void loadFile() {
//过滤文件,只显示 .jpg 和 .gif , .png 图像:
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG,JPEG,GIF & PNG Images", "jpg", "gif", "png","jpeg");
jfc.setFileFilter(filter);
jfc.showOpenDialog(win); //弹出一个 "Open File" 文件选择器对话框。
String dir = (jfc.getSelectedFile() != null) ? (jfc.getSelectedFile() //获取文件路径
.getPath()) : null;
//System.out.println(dir);
if (dir != null && !dir.equals("")) {
ii = new ImageIcon(dir); //根据指定的 文件名 或 文件路径 创建一个 ImageIcon
}else {
//JOptionPane.showMessageDialog(null, "请选择图片!", "警告", JOptionPane.YES_OPTION);
//JOptionPane.showInternalMessageDialog(null, "请选择图片!", "information", JOptionPane.INFORMATION_MESSAGE);
ii = null;
}
win.getJll().setIcon(ii); //将加载的图片显示在 左边窗口中
}
public void saveFile(){
// jfc.setFileSelectionMode(JFileChooser.SAVE_DIALOG);//只能打开文件夹
jfc.showSaveDialog(win); //弹出一个 "Save File" 文件选择器对话框。
File file = new File(jfc.getSelectedFile().getAbsolutePath());
if(bi==null)
JOptionPane.showMessageDialog(null, "保存文件失败,请检查新的图片是否生成!", "警告", JOptionPane.WARNING_MESSAGE);
else{
if(file.exists()){
int i= JOptionPane.showConfirmDialog(null, "已存在相同的文件名,是否替换?", "确认框", JOptionPane.YES_NO_OPTION);
//System.out.println(i);
if(i==0){
try {
ImageIO.write(bi,"jpg" ,file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else{
try {
ImageIO.write(bi,"jpg" ,file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//if(!file.exists())
//把image写入output中
//Image image = null;
// //image=win.getJlr();
// if(image==null){
// System.out.println("image为空!!");
// }
// int w = image.getWidth(null);
// int h = image.getHeight(null);
// //首先创建一个BufferedImage变量,因为ImageIO写图片用到了BufferedImage变量。
// BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
//
// //再创建一个Graphics变量,用来画出来要保持的图片,及上面传递过来的Image变量
// Graphics g = bi.getGraphics();
// try {
// g.drawImage(image, 0, 0, null);
// //将BufferedImage变量写入文件中。
// ImageIO.write(bi,"jpg",new File("d:/gray11.jpg"));
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
public ImageIcon getIi() {
return ii;
}
public void setIi(ImageIcon ii) {
this.ii = ii;
}
public float[][] getData() {
return data;
}
public void setData(float[][] data) {
this.data = data;
}
}