2

我正在 Swing 中开发一个应用程序,它有 5 个选项卡,对图像进行以下 5 次操作:无操作、颜色转换、仿射变换、卷积和查找。

这是代码:

import java.awt.color.ColorSpace;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.awt.image.LookupOp;
import java.awt.image.LookupTable;
import java.awt.image.ShortLookupTable;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;


public class ImageProcessing extends JFrame{
BufferedImage source;
public static void main(String args[])
{
    try {
         UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch (Exception e1){e1.printStackTrace();}
    SwingUtilities.invokeLater(new Runnable(){public void run(){new ImageProcessing();}});
}

public ImageProcessing() {
    // TODO Auto-generated constructor stub
    super("Image Processing");
    setSize(600,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    try
    {
        source=ImageIO.read(new File("src/abc.jpg"));
    }catch(IOException e){System.out.println("Exception Here :"+e);}

    JTabbedPane jtp=new JTabbedPane();
    buildNoOpTab(jtp);
    buildAffineTransformOpTab(jtp);
    buildColorConvertOpTab(jtp);
    buildConvolveOpTab(jtp);
    buildLookUpOpTab(jtp);
    //buildRescaleOpTab(jtp);

    add(jtp);
    setVisible(true);

}
private void buildNoOpTab(JTabbedPane jtp)
{
    jtp.add("No Op",new JLabel(new ImageIcon(source)));
}
private void buildAffineTransformOpTab(JTabbedPane jtp)
{
    BufferedImage dst;
    AffineTransform transform=AffineTransform.getScaleInstance(0.5, 0.5);
    AffineTransformOp op=new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
    dst=op.filter(source, null);
    jtp.add("AffineTransform",new JLabel(new ImageIcon(dst)));
}
private void buildColorConvertOpTab(JTabbedPane jtp)
{
    BufferedImage dst = null;
    ColorSpace clr=ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorConvertOp op=new ColorConvertOp(clr,null);
    dst=op.filter(source,dst);
    jtp.add("Color Convert",new JLabel(new ImageIcon(dst)));
}
 private void buildConvolveOpTab(JTabbedPane jtp) {
        BufferedImage dst = null;
        float sharpen[] = new float[] {
             0.0f, -1.0f,  0.0f,
            -1.0f,  5.0f, -1.0f,
             0.0f, -1.0f,  0.0f
        };
        Kernel kernel = new Kernel(3, 3, sharpen);
        ConvolveOp op = new ConvolveOp(kernel);
        dst = op.filter(source, null);

        jtp.add("Convolve", new JLabel(new ImageIcon(dst)));
    }
 private void buildLookUpOpTab(JTabbedPane jtp)
 {
     BufferedImage dst=null;
     short[] data=new short[256];
     for(int i=0;i<256;i++)
         data[i]=(short)(255-i);
     LookupTable lkp=new ShortLookupTable(0,data);
     LookupOp op=new LookupOp(lkp,null);
     dst=op.filter(source, null);
     jtp.add("Look Up",new JLabel(new ImageIcon(dst)));
}


}

buildLookUpOpTab 中存在一些问题,因为删除此方法应用程序可以正常工作。

这是我得到的例外:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:
  Number of color/alpha components should be 3 but length of bits array is 1
  at java.awt.image.ColorModel.<init>(ColorModel.java:336)
  at java.awt.image.ComponentColorModel.<init>(ComponentColorModel.java:273)
  at java.awt.image.LookupOp.createCompatibleDestImage(LookupOp.java:413)
  at java.awt.image.LookupOp.filter(LookupOp.java:153)
  at ImageProcessing.buildLookUpOpTab(ImageProcessing.java:108)
  at ImageProcessing.<init>(ImageProcessing.java:49)
  at ImageProcessing$1.run(ImageProcessing.java:30)
  at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
  at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701)
  at java.awt.EventQueue.access$000(EventQueue.java:102)
  at java.awt.EventQueue$3.run(EventQueue.java:662)
  at java.awt.EventQueue$3.run(EventQueue.java:660)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
  at java.awt.EventQueue.dispatchEvent(EventQueue.java:671)
  at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
  at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
  at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

谁能告诉我这种方法有什么问题?

4

2 回答 2

5

LookupOp.filter方法说:

对 BufferedImage 执行查找操作。如果源图像中的颜色模型与目标图像中的颜色模型不同,则将在目标图像中转换像素。如果目标图像为空,将使用适当的 ColorModel 创建一个 BufferedImage。如果 LookupTable 中的数组数量不符合上述类注释中规定的限制,或者如果源图像具有 IndexColorModel,则可能会引发 IllegalArgumentException。

由于您正在过滤使用BufferedImage创建ImageIO.read的图像,因此图像将具有的颜色模型肯定不会是 IndexColorModel,因为JPEGImageReader(实际上是从文件创建 BufferdImage)不支持IndexColorModel- 在过去,JPEG 使用DirectColorModel

查看此线程上有关如何读取 JPEG 文件并使用不同颜色模型的答案: 无法使用 ImageIO.read(File file) 读取 JPEG 图像

于 2013-01-01T06:55:44.930 回答
1

在使用任何滤镜之前,您需要从图像中删除 Alpha 通道。要使您的代码正常工作,请更改:

try
    {
        source=ImageIO.read(new File("src/abc.jpg"));
    } catch(IOException e){System.out.println("Exception Here :"+e);}

有了这个:

 try
    {
        BufferedImage src = ImageIO.read(new File("abc.jpg"));
        int w = src.getWidth();
        int h = src.getHeight();
        source = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Raster raster = src.getRaster().createChild(0, 0, w, h, 0, 0, 
            new int[] {0, 1, 2});
        source.setData(raster);
    }catch(IOException e){System.out.println("Exception Here :"+e);}

上面的代码在RGBmode 中创建一个新的缓冲图像,并将RGB原始图像的数据设置为新的缓冲图像,忽略这些alpha值。但是,如果您的原始图像包含完全透明的点,那么它将成为新缓冲图像中的黑点。

于 2013-01-01T07:14:43.943 回答