简体   繁体   中英

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.

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.

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:

Performs a lookup operation on a 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. 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.

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

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)

You need to remove alpha channel from your image before using any filter on it. 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. But in case your original image contains completely transparent spots then it will become black spots in new buffered image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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