简体   繁体   English

需要帮助以删除我的Java图形代码中的异常

[英]Need help to remove exception in my java graphics code

I am developing an application in swing which has 5 tabs with following 5 operations on an image :No Operation ,Color Convert ,Affine Transform ,Convolve and Look Up. 我正在开发一个带有5个选项卡的swing应用程序,该选项卡对图像进行以下5个操作:无操作,颜色转换,仿射变换,卷积和查找。

Here is the code : 这是代码:

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)));
}


}

There is some problem in the buildLookUpOpTab as removing this method application works fine. buildLookUpOpTab中存在一些问题,因为删除此方法的应用程序可以正常工作。

Here is the exception which I am getting: 这是我得到的例外:

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)

Can anyone tell me what is the problem in that method? 谁能告诉我该方法有什么问题?

The LookupOp.filter method says that: LookupOp.filter方法表示:

Performs a lookup operation on a BufferedImage. 在BufferedImage上执行查找操作。 If the color model in the source image is not the same as that in the destination image, the pixels will be converted in the destination. 如果源图像中的颜色模型与目标图像中的颜色模型不同,则将在目标中转换像素。 If the destination image is null, a BufferedImage will be created with an appropriate ColorModel. 如果目标图像为空,则将使用适当的ColorModel创建一个BufferedImage。 An IllegalArgumentException might be thrown if the number of arrays in the LookupTable does not meet the restrictions stated in the class comment above, or if the source image has an IndexColorModel. 如果LookupTable中的数组数不满足上面的类注释中所述的限制,或者源图像具有IndexColorModel,则可能引发IllegalArgumentException。

Since you are filtering a BufferedImage created from using ImageIO.read , the color model that the image will have will definitely not be IndexColorModel, since JPEGImageReader (which actually created the BufferdImage from the file) does not support IndexColorModel - in the olden days, JPEGs used the DirectColorModel 由于您正在过滤使用ImageIO.read创建的BufferedImage ,因此图像将具有的颜色模型肯定不是IndexColorModel,因为JPEGImageReader (实际上是根据文件创建了BufferdImage)不支持IndexColorModel在过去,JPEG使用了DirectColorModel

Have a look at the answer on this thread on how to read a JPEG file and use a different color model: Unable to read JPEG image using ImageIO.read(File file) 看一下有关如何读取JPEG文件和使用其他颜色模型的线程上的答案: 无法使用ImageIO.read(File file)读取JPEG图像。

You need to remove alpha channel from your image before using any filter on it. 您需要先从图像中删除Alpha通道,然后再对其使用任何过滤器。 To make your code working, change: 为了使您的代码正常工作,请更改:

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

with this: 有了这个:

 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);}

The above code creates a new buffered image in RGB mode and set the RGB data of original image to new buffered image ignoring the alpha values. 上面的代码在RGB模式下创建了一个新的缓冲图像,并且忽略了alpha值,将原始图像的RGB数据设置为新的缓冲图像。 But in case your original image contains completely transparent spots then it will become black spots in new buffered image. 但是,如果原始图像包含完全透明的斑点,则它将在新的缓冲图像中变成黑点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM