【发布时间】:2014-09-01 17:01:47
【问题描述】:
我编写了一个程序来分离图像的红蓝和绿色分量,但下面的代码给出了错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:888)
at rgb.Rgb.main(Rgb.java:46):
这里是源代码:
public static void main(String[] args) {
String type = "jpg";
BufferedImage img = null;
try {
img = ImageIO.read(new File("d:\\a.jpg"));
System.out.println(img.getWidth() + " " + img.getHeight());
} catch (IOException ex) {
Logger.getLogger(Rgb.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedImage rp, gp, bp;
rp = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
bp = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
gp = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
for (int i = 1; i <= img.getHeight(); i++) {
for (int j = 1; j <= img.getWidth(); j++) {
int pixel = img.getRGB(i, j);
int alpha = pixel & 0xff000000;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
rp.setRGB(i, j, alpha | (red << 16));
gp.setRGB(i, j, alpha | (green << 8));
bp.setRGB(i, j, alpha | blue);
}
}
try {
ImageIO.write(rp, type, new File("d:\\red.jpg"));
ImageIO.write(gp, type, new File("d:\\green.jpg"));
ImageIO.write(bp, type, new File("d:\\blue.jpg"));
} catch (IOException ex) {
Logger.getLogger(Rgb.class.getName()).log(Level.SEVERE, null, ex);
}
【问题讨论】:
标签: java rgb bufferedimage