图形的模糊与锐化参考算法精品资料.docx
《图形的模糊与锐化参考算法精品资料.docx》由会员分享,可在线阅读,更多相关《图形的模糊与锐化参考算法精品资料.docx(26页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、您的位置:咨询直通车岗位入门知识IT类JAVA例子模糊与锐化 发布时间:2009-01-25 10:33:36 来源:网络 点击数:683import java.awt.*;import java.awt.event.*;import java.awt.image.*;import javax.swing.*;import java.io.*;/ 图像的模糊与锐化public class BlurAndSharpenDemo extends JFrame implements ActionListener private JPanel jPanel = new JPanel(); /面板jPa
2、nel用于容纳模糊、锐化、还原图像按钮private JButton buttonFile; /打开图像文件按钮private JButton buttonBlur; /模糊图像按钮private JButton buttonSharpen; /锐化图像按钮private JButton buttonReset; /还原图像按钮ImagePanel imagePanel = new ImagePanel(); /创建ImagePanel对象用于绘制图像public BlurAndSharpenDemo() super(图像的模糊与锐化演示); /调用父类构造函数Container conten
3、tPane = getContentPane(); /得到容器buttonFile = new JButton(打开图像文件); /实例化组件buttonFile.addActionListener(this); /增加事件监听buttonBlur = new JButton(模糊图像);buttonBlur.addActionListener(this);buttonSharpen = new JButton(锐化图像);buttonSharpen.addActionListener(this);buttonReset = new JButton(还原图像);buttonReset.addA
4、ctionListener(this);jPanel.add(buttonBlur); /增加组件到面板上jPanel.add(buttonSharpen);jPanel.add(buttonReset);contentPane.add(jPanel, BorderLayout.SOUTH); /增加组件到容器上contentPane.add(buttonFile, BorderLayout.NORTH);contentPane.add(imagePanel, BorderLayout.CENTER);/设置窗口this.setSize(280, 230); /设置窗口大小this.setVi
5、sible(true); /设置窗口可见this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /关闭窗口时退出程序public void actionPerformed(ActionEvent e) JButton button = (JButton)e.getSource(); /获取事件源 /打开图像文件按钮buttonFile事件处理 if(button=this.buttonFile) JFileChooser chooser = new JFileChooser(); /实例化文件选择器chooser.setFileSelectio
6、nMode(JFileChooser.FILES_ONLY); /设置文件打开模式为仅打开文件chooser.setCurrentDirectory(new File(.); /设置文件选择器当前目录/设置图像文件过滤器chooser.setFileFilter(new javax.swing.filechooser.FileFilter() public boolean accept(File file) /可接受的文件类型String name = file.getName().toLowerCase();return name.endsWith(.gif)| name.endsWith(
7、.jpg)| name.endsWith(.jpeg)| file.isDirectory();public String getDescription() /文件描述return 图像文件;);int result = chooser.showOpenDialog(this); /显示文件选择对话框if (result = JFileChooser.APPROVE_OPTION) /得到用户行为String fileName = chooser.getSelectedFile().getAbsolutePath();/得到选择的文件名imagePanel.loadImage(fileName
8、); /截入图像并显示 /模糊图像按钮buttonBlur事件处理else if(button=this.buttonBlur) imagePanel.blur(); /模糊图像buttonReset.setEnabled(true); /设置还原图像按钮可用/锐化图像按钮buttonSharpen事件处理else if(button=this.buttonSharpen) imagePanel.sharpen(); /锐化图像buttonReset.setEnabled(true); /设置还原图像按钮可用/还原图像按钮buttonBlur事件处理else if(button=this.bu
9、ttonReset) imagePanel.reset(); /还原图像buttonReset.setEnabled(false); /设置还原图像按钮不可用public static void main(String args) new BlurAndSharpenDemo();public class ImagePanel extends JPanel Image image; /被处理的图像BufferedImage bufImage; /用于显示的缓冲区图像BufferedImage originalBufImage; /原始缓冲区图像Graphics2D g2D; /图形环境/载入图
10、像public void loadImage(String fileName) image = this.getToolkit().getImage(fileName); /获取图像MediaTracker mt = new MediaTracker(this); /实例化媒体加载器mt.addImage(image, 0); /增加待加载图像到媒体加载器try mt.waitForAll(); /等待所有图像的加载完成 catch (Exception ex) ex.printStackTrace(); /输出出错信息/创建原始缓冲区图像originalBufImage = new Buff
11、eredImage(image.getWidth(this),image.getHeight(this),BufferedImage.TYPE_INT_ARGB);g2D = originalBufImage.createGraphics(); /创建缓冲区图像的图形环境g2D.drawImage(image, 0, 0, this); /传输源图像数据到缓冲区图像中bufImage = originalBufImage;repaint(); /重绘组件 /过滤图像 public void applyFilter(float data) if (bufImage = null)return;
12、/如果bufImage为空则直接返回Kernel kernel = new Kernel(3, 3, data); ConvolveOp imageOp=new ConvolveOp(kernel,ConvolveOp.EDGE_NO_OP, null);/创建卷积变换操作对象BufferedImage filteredBufImage = new BufferedImage(image.getWidth(this),image.getHeight(this),BufferedImage.TYPE_INT_ARGB);/过滤后的缓冲区图像imageOp.filter(bufImage, fil
13、teredBufImage);/过滤图像,目标图像在filteredBufImagebufImage = filteredBufImage; /让用于显示的缓冲区图像指向过滤后的图像repaint(); /重绘组件/模糊图像 public void blur() if (bufImage = null)return;float data = 0.0625f, 0.125f, 0.0625f,0.125f,0.025f, 0.125f,0.0625f, 0.125f, 0.0625f ;applyFilter(data);/锐化图像public void sharpen() if (bufIma
14、ge = null)return;float data = -1.0f, -1.0f, -1.0f, -1.0f, 9.0f, -1.0f, -1.0f, -1.0f, -1.0f ;applyFilter(data);/恢复图像public void reset() if (bufImage = null)return;bufImage = originalBufImage; /g2D.drawImage(image, 0, 0, this);repaint(); /调用paint()方法重绘组件public void paint(Graphics g) super.paintCompone
15、nt(g);/如果bufImage非空,则在组件上绘制它if (bufImage != null) Graphics2D g2 = (Graphics2D) g;g2.drawImage(bufImage,(this.getWidth() - bufImage.getWidth() / 2,(this.getHeight() - bufImage.getHeight() / 2,this);参考算法2:import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Font
16、;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.GridLayout;import java.awt.Image;import java.awt.MediaTracker;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import java.awt.image.ConvolveOp;import java.awt.image.Kernel;
17、import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class SharpenFuzzy extends JFrame private JPanel contentPane;private BorderLayout borderLayout1 = new BorderLayout();/ 添加的组件private JPanel jpanel = new JPanel();private JButton jb1 = new JButton();private JButton j
18、b2 = new JButton();private JButton jb3 = new JButton();private GridLayout gridLayout = new GridLayout();/ 创建ShowPanel类的实例PanelDemo pd = new PanelDemo();public void initial() / 设置各组件的属性contentPane = (JPanel) this.getContentPane();contentPane.setLayout(borderLayout1);this.setSize(new Dimension(380, 55
19、0);this.setTitle(图像的明暗处理演示);jb1.setFont(new Font(Dialog, Font.BOLD, 13);jb1.setText(Sharp Image);jb1.addActionListener(new ActionListener() / 添加事件监听器public void actionPerformed(ActionEvent e) jb1_actionPerformed(e););jb2.setFont(new Font(Dialog, Font.BOLD, 13);jb2.setText(Biur Image);jb2.addActionLi
20、stener(new ActionListener() / 添加事件监听器public void actionPerformed(ActionEvent e) jb2_actionPerformed(e););jb3.setFont(new Font(Dialog, Font.BOLD, 13);jb3.setText(Reset);jb3.addActionListener(new ActionListener() / 添加事件监听器public void actionPerformed(ActionEvent e) jb3_actionPerformed(e););jpanel.setLa
21、yout(gridLayout);jpanel.add(jb1, null);jpanel.add(jb2, null);jpanel.add(jb3, null);contentPane.add(jpanel, BorderLayout.SOUTH);/ 在窗口中部添加ShowPanel面板contentPane.add(pd, BorderLayout.CENTER);this.setVisible(true);public void jb1_actionPerformed(ActionEvent e) pd.sharpImage();/ 调用锐化方法pd.repaint();/ 绘制锐化
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 图形的模糊与锐化参考算法 精品资料 图形 模糊 锐化 参考 算法 精品 资料
限制150内