读书人

java.lang.ArrayIndexOutOfBoundsExce

发布时间: 2012-04-12 15:46:35 作者: rapoo

java.lang.ArrayIndexOutOfBoundsException

Java code
package ProImage;import java.awt.Image;import java.awt.image.BufferedImage;import java.awt.image.MemoryImageSource;import java.awt.image.PixelGrabber;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JFrame;/* * 1.PixelGrabber :将图片分解成象素存入int[]数组。alpha,green,red ,blue各占8位(bit) *      24-31 位表示 alpha 分量,16-23 位表示红色分量,8-15 位表示绿色分量,0-7 位表示蓝色分量。          00000000(alpha) 00000000(红) 00000000(绿) 00000000(蓝)          0xff=11111111=00000000 00000000 00000000 11111111        2.MemoryImageSource :将int[]数组合成图片.功能和PixelGrabber相反        ImagemodImg = JFrame.createImage(        new MemoryImageSource(w,h,colors,0,w))   3. * */public class AccordingPixArrayToCreateImage {    private int  imageWidth;    private int imageHeight;    private int pixel[];        private int[] blues;    private int[] greens;    private int[] reds;        private Image image;        private int index=0;    public AccordingPixArrayToCreateImage(){                try {            loadImage();        } catch (Exception e) {                        e.printStackTrace();        }                GrabberPixelToArray();        produceImage(blues);    }    public static void main(String[] args){                new AccordingPixArrayToCreateImage();    }        public void loadImage() throws Exception{//载入图片 同时初始化 同时保存图片宽度 高度         image=ImageIO.read(AccordingPixArrayToCreateImage.class.getResource("MM.jpg"));                imageWidth=image.getWidth(null);        imageHeight=image.getHeight(null);        pixel=new int[imageWidth*imageHeight];        blues=new int[imageWidth*imageHeight];        greens=new int[imageWidth*imageHeight];        reds=new int[imageWidth*imageHeight];        System.out.println(imageWidth);        System.out.println(imageHeight);        System.out.println(imageWidth*imageHeight);                    }    public void GrabberPixelToArray(){//抓取图片的像素点                PixelGrabber grabber=new PixelGrabber(image,0,0,imageWidth,imageHeight,pixel,0,imageWidth);                try {            grabber.grabPixels();//抓取            System.out.println("-----"+pixel.length);        } catch (InterruptedException e) {            System.out.println("抓取图片像素出现异常了!!");            e.printStackTrace();            return;        }                        for(int i=0;i<imageHeight;i++){            for(int j=0;j<imageWidth;i++){                initColorArray(pixel[i*imageWidth+j]);//出现数组越界异常!!!!!                            }        }    }    public void initColorArray(int pixel){                                int alpha=pixel>>24;//将像素值右移24位 即将最左边24位移除 该24位为红绿蓝得值 所以最后得到的结果为 00000000 00000000 00000000 11011011(alpha值)alpha值原先在左边 变成最右边        int red=pixel>>16&0xff;//将像素值右移16位即将绿蓝得数值移除 左边补0  在与0xff进行与运算将alpha的值变成全0        int green=pixel>>8&0xff;        int blue=pixel&0xff;//蓝色像素位于最低位 所以不用移位 直接将其他位变为0                alpha=alpha<<24;//alpha的值移位后 变成在最低位 所以要还原 左移24位        red=red<<16|alpha;//还原 左移16位 绿蓝的值都变为0 在与alpha值或运算 将alpha值 添加到该值的最高位 所以该颜色 最后保留了alpha值 和红色值 绿蓝都变为0了        green=green<<8|alpha;        blue=blue|alpha;                reds[index]=red;        greens[index]=green;        blues[index]=blue;                index++;    }        public void produceImage( int[] pixels){//根据存储像素的数组来初始化图片        JFrame f=new JFrame();        BufferedImage targetImage=(BufferedImage)f.createImage(new MemoryImageSource(imageWidth,imageHeight,pixels,0,imageWidth));                try {            ImageIO.write(targetImage, "jpg", new FileOutputStream("c:/red.jpg"));        } catch (FileNotFoundException e) {                        e.printStackTrace();        } catch (IOException e) {                    e.printStackTrace();        }    }} 



[解决办法]
Java code
for(int i=0;i<imageHeight;i++){            for(int j=0;j<imageWidth;j++){//j++                initColorArray(pixel[i*imageWidth+j]);//出现数组越界异常!!!!!                            }        }
[解决办法]
楼上说的对,第二层循环的变量自加应是j,而不是i~!
[解决办法]
探讨

Java code

for(int i=0;i<imageHeight;i++){
for(int j=0;j<imageWidth;j++){//j++
initColorArray(pixel[i*imageWidth+j]);//出现数组越界异常!!!!!

}
……

[解决办法]
数组越界,这个不难解决、、

读书人网 >J2SE开发

热点推荐