eple27 2013-04-25 20:08 采纳率: 0%
浏览 642
已采纳

对图像进行锐化和模糊操作之后保存到桌面,保存的图片和显示的不一样

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

/*

  • 图片编辑器 */

public class PicEdit extends JFrame {
private int currentPixArray[] = null;// 保存当前操作的像素矩阵
BufferedImage bufImage = null; //用于显示的缓冲区图像
BufferedImage originalBufImage; //原始缓冲区图像
private String lastDir = "";// 图像的路径
private JLabel imageLabel = null;// 用于显示图像的标签
private BufferedImage newImage;// 加载的图像
private int h, w;// 图像的高和宽
private LinkedList imageStack = new LinkedList();// 保存历史操作图像矩阵
private LinkedList tempImageStack = new LinkedList();
public PicEdit() {
super("图片特效工具");

    // 创建菜单
    JToolBar toolBar = new JToolBar();
    this.add(toolBar,BorderLayout.NORTH);
    //skin sk = new skin();
    JButton openbutton      =   new JButton(new ImageIcon("sk.getAddpic()"));
    //JButton exitbutton        =   new JButton(new ImageIcon("sk.getExit()"));
    //JButton graybutton        =   new JButton(new ImageIcon("sk.getGray()"));
    //JButton balancebutton     =   new JButton(new ImageIcon("sk.getBalance()"));
    JButton blurbutton      =   new JButton(new ImageIcon("sk.getBlur()"));
    JButton sharpenbutton   =   new JButton(new ImageIcon("sk.getSharpen()"));
    JButton resetbutton     =   new JButton(new ImageIcon("sk.getReset()"));
    JButton backbutton      =   new JButton(new ImageIcon("sk.getPrevious()"));
    JButton frontbutton     =   new JButton(new ImageIcon("sk.getNext()"));
    //JButton waterbutton       =   new JButton(new ImageIcon("sk.getWater()"));
    //JButton spotlightbutton   =   new JButton(new ImageIcon("sk.getSpotlight()"));
    //JButton bitbltbutton      =   new JButton(new ImageIcon("sk.getBiblt()"));
    //JButton printscbutton         =   new JButton(new ImageIcon("sk.getPrintsc()"));
    JButton savebutton      =   new JButton(new ImageIcon("sk.getSave()"));

    openbutton.setToolTipText("打开文件");
    openbutton.setText("打开文件");
    openbutton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);//让文字置于图标
    openbutton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);//下方
    openbutton.addActionListener(new OpenListener());

    blurbutton.setToolTipText("模糊");
    blurbutton.setText("模糊");
    blurbutton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    blurbutton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    blurbutton.addActionListener(new BlurActionListener());

    sharpenbutton.setToolTipText("锐化");
    sharpenbutton.setText("锐化");
    sharpenbutton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    sharpenbutton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    sharpenbutton.addActionListener(new SharpenActionListener());

    resetbutton.setToolTipText("还原");
    resetbutton.setText("还原");
    resetbutton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    resetbutton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    resetbutton.addActionListener(new ResetMenuItemActionListener());

    backbutton.setToolTipText("后退");
    backbutton.setText("后退");
    backbutton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    backbutton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    backbutton.addActionListener(new BackActionListener());

    frontbutton.setToolTipText("前进");
    frontbutton.setText("前进");
    frontbutton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    frontbutton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    frontbutton.addActionListener(new FrontActionListener());

    savebutton.setToolTipText("保存图片到桌面");
    savebutton.setText("保存");
    savebutton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    savebutton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    savebutton.addActionListener(new SaveListener());

    toolBar.add(openbutton);
    toolBar.add(blurbutton);
    toolBar.add(sharpenbutton);
    toolBar.add(backbutton);
    toolBar.add(frontbutton);
    toolBar.add(resetbutton);
    toolBar.add(savebutton);

    this.setSize(1200,700);
    this.setLocation(20,20);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    imageLabel = new JLabel("");
    JScrollPane pane = new JScrollPane(imageLabel);
    this.add(pane, BorderLayout.CENTER);

    this.setVisible(true);

}

//-----------------菜单监听器---------------//
private class OpenListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
FileDialog d = new FileDialog(PicEdit.this,"Open image file",FileDialog.LOAD);
d.setFile(".jpg");
d.setDirectory(lastDir);
d.setVisible(true);
String file = d.getFile();
lastDir = d.getDirectory();
if(file!=null)
{
try {
newImage = ImageIO.read(new File(lastDir+file));
originalBufImage = newImage;
w = newImage.getWidth();
h = newImage.getHeight();
currentPixArray = getPixArray(newImage, w, h);
imageStack.clear();
tempImageStack.clear();
imageStack.addLast(currentPixArray);
imageLabel.setIcon(new ImageIcon(newImage));

            } catch (IOException ex) {
                System.out.println(ex);
            }
        }
        PicEdit.this.repaint();
    }
}

private class BackActionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        if (imageStack.size() <= 1) {
            JOptionPane.showMessageDialog(null, "此幅图片的处理已经没有后退历史操作了", "提示",
                    JOptionPane.INFORMATION_MESSAGE);
        } else {
            tempImageStack.addLast(imageStack.removeLast());
            currentPixArray = imageStack.getLast();
            showImage(currentPixArray);
            PicEdit.this.setRGB(newImage,0,0,w,h,currentPixArray);//将变换后的结果保存到newImage里
        }
    }

}

private class FrontActionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        if (tempImageStack.size() < 1) {
            JOptionPane.showMessageDialog(null, "此幅图片的处理已经没有前进历史操作了", "提示",
                    JOptionPane.INFORMATION_MESSAGE);
        } else {
            currentPixArray = tempImageStack.removeFirst();
            imageStack.addLast(currentPixArray);
            showImage(currentPixArray);
            PicEdit.this.setRGB(newImage,0,0,w,h,currentPixArray);
        }
    }

}

private class BlurActionListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        PicEdit.this.blur();
    }
}

private class SharpenActionListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        PicEdit.this.sharpen();
    }
}

private class ResetMenuItemActionListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        PicEdit.this.reset();
    }
}

private class SaveListener implements ActionListener{
public void actionPerformed(ActionEvent e){
save();
}
}
//-----------------过滤图像---------------//
public void applyFilter(float[] data) {
if (newImage == null)
return; //如果newImage为空则直接返回
Kernel kernel = new Kernel(3, 3, data);
ConvolveOp imageOp=new ConvolveOp(kernel,ConvolveOp.EDGE_NO_OP, null); //创建卷积变换操作对象
BufferedImage filteredBufImage = new BufferedImage(newImage.getWidth(PicEdit.this),newImage.getHeight(PicEdit.this),BufferedImage.TYPE_INT_ARGB); //过滤后的缓冲区图像
imageOp.filter(newImage, filteredBufImage);//过滤图像,目标图像在filteredBufImage
newImage = filteredBufImage; //让用于显示的缓冲区图像指向过滤后的图像
int[] resultArray = getPixArray(newImage, w, h);
imageStack.addLast(resultArray);
currentPixArray = resultArray;
showImage(resultArray);
tempImageStack.clear();
PicEdit.this.setRGB(newImage,0,0,w,h,resultArray);
}

//-----------------模糊图像---------------//
public void blur() {
if (newImage == 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 (newImage == 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 (newImage == null)
        return;
        int[] resultArray = imageStack.getFirst();
        imageStack.addLast(resultArray);
        currentPixArray = resultArray;
        showImage(resultArray);
        tempImageStack.clear();
        PicEdit.this.setRGB(newImage,0,0,w,h,resultArray);
      }

//-----------------获取图像像素矩阵---------------//
private int[] getPixArray(Image im, int w, int h) {
int[] pix = new int[w * h];
PixelGrabber pg = null;
try {
pg = new PixelGrabber(im, 0, 0, w, h, pix, 0, w);
if (pg.grabPixels() != true)
try {
throw new java.awt.AWTException("pg error" + pg.status());
} catch (Exception eq) {
eq.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();

    }
    return pix;
}

//-----------------显示图片---------------//
private void showImage(int[] srcPixArray) {
Image pic = createImage(new MemoryImageSource(w, h, srcPixArray, 0, w));
ImageIcon ic = new ImageIcon(pic);
imageLabel.setIcon(ic);
imageLabel.repaint();
}

//-----------------将图像数组转变成BufferedImage图像---------------//
public void setRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {
int type = image.getType();
if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
image.getRaster().setDataElements( x, y, width, height, pixels );
else
image.setRGB( x, y, width, height, pixels, 0, width );
}

//-----------------保存图像到桌面---------------//
public void save(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddHHmmss");
String name = sdf.format(new Date());
File path = FileSystemView.getFileSystemView().getHomeDirectory();
String format = "jpg";
File f = new File(path + File.separator + name + "." + format);
try {
ImageIO.write(newImage, format, f);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args){
    new PicEdit();
}

}

  • 写回答

1条回答 默认 最新

  • clxy大叔 2013-04-26 08:29
    关注

    最简单就是改格式
    [quote]String format = "jpg"; [/quote]
    String format = "png";

    ConvolveOp 这个鬼东西,问题多多。
    我是从来没搞清楚过。

    另外,推荐
    http://www.jhlabs.com/ip/index.html
    这个库,用过,还好。
    比JDK自己的东西好。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站