我正在 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)
谁能告诉我这种方法有什么问题?