是什么导致此ArrayIndexOutOfBoundsException?

时间:2018-12-19 20:50:25

标签: java

使用位图文件数据更新字节数组时,出现ArrayIndexOutOfBoundsException错误。

错误:线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:819     在test.BitmapResizer.main(BitmapResizer.java:95)

这是一个简单的练习程序,用于缩放现有的位图图像。问题可能与我的数学有关,但是我被卡住了

public class BitmapResizer {

public static void main(String[] args) {
    File path = new File(File.separator + "test" + File.separator + "bitmap");
    File original = new File(path, "original.bmp");
    File scaled = new File(path, "scaled.bmp");

    int width = 0;
    int length = 0;
    int pixelSize = 0;      

    try {
        BufferedImage bImg = ImageIO.read(original);
        width = bImg.getWidth();
        length = bImg.getHeight();
        pixelSize = bImg.getColorModel().getPixelSize();
    }catch(IOException e) {
        e.printStackTrace();
    }

    Scanner input = new Scanner(System.in);
    System.out.print("Enter scale: ");      
    int scale = input.nextInt();

    int scaledLength = length*scale;
    int scaledWidth = width*scale;

    int bytesPerPixel = pixelSize/8;
    int widthBytes = scaledWidth*bytesPerPixel;

    int originalSize = (28 + (length*width)*bytesPerPixel);
    int scaledSize = (28 + (scaledLength*scaledWidth)*bytesPerPixel);


    try(FileOutputStream fos = new FileOutputStream(scaled)){
        scaled.delete();
        try(FileInputStream oFis = new FileInputStream(original)){
            byte[] scaledArray = new byte[scaledSize];              
            byte[] bArray = Files.readAllBytes(original.toPath());

        fos.write(66);
        fos.write(77);

        fos.write(scaledSize);
        fos.write(0);
        fos.write(0);
        fos.write(0);
        fos.write(0);
        fos.write(0);
        fos.write(0);
        fos.write(0);
        fos.write(26);
        fos.write(0);
        fos.write(0);
        fos.write(0);

        fos.write(12);
        fos.write(0);
        fos.write(0);
        fos.write(0);       
        fos.write(scaledWidth);
        fos.write(0);
        fos.write(scaledLength);
        fos.write(0);
        fos.write(1);
        fos.write(0);
        fos.write(24);
        fos.write(0);       //26

        //o = original
        //s = scaled
        int counter = 1;
        int s = 27;
        for(int o = 27; o < bArray.length-28; o++) {

                scaledArray[s] = bArray[o];
                scaledArray[s+1] = bArray[o+1];
                scaledArray[s+2] = bArray[o+2];

                scaledArray[s+3] = bArray[o];
                scaledArray[s+4] = bArray[o+1];
                scaledArray[s+5] = bArray[o+2];

                scaledArray[s+(widthBytes)] = bArray[o];
                scaledArray[s+(widthBytes)+1] = bArray[o+1];
                scaledArray[s+(widthBytes)+2] = bArray[o+2];

                scaledArray[s+(widthBytes)+3] = bArray[o];
                scaledArray[s+(widthBytes)+4] = bArray[o+1];
                scaledArray[s+(widthBytes)+5] = bArray[o+2];

                if(counter == widthBytes-(bytesPerPixel*2)+1) s+=(widthBytes+6); else {s+=bytesPerPixel*2; counter+=bytesPerPixel*2;}
        }

        fos.write(scaledArray);
        fos.write(0);
        fos.write(0);

        }catch(IOException e) {
            e.printStackTrace();
        }
    }catch(IOException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
    }

}

}

第85行的循环应从“ bArray”的迭代更新“ scaledArray”。这个想法是通过将原始位图的每个像素映射到另一个像素上的多个像素来缩放现有的位图图像。

万一有人想尝试,这里是创建位图的代码:

public class BitmapMaker {
public static void main(String[] args) {
    File dir = new File(File.separator + "test" + File.separator + "bitmap");
    dir.mkdirs();
    File f = new File(dir, "original.bmp");
    File c = new File(dir, "copy.bmp");

    buildBitmap(f);     
}   

public static void buildBitmap(File f) {
    f.delete();
    try(FileOutputStream fos = new FileOutputStream(f)){

        fos.write(66);
        fos.write(77);

        fos.write(220);
        fos.write(0);
        fos.write(0);
        fos.write(0);
        fos.write(0);
        fos.write(0);
        fos.write(0);
        fos.write(0);
        fos.write(26);
        fos.write(0);
        fos.write(0);
        fos.write(0);

        fos.write(12);
        fos.write(0);
        fos.write(0);
        fos.write(0);
        fos.write(8);
        fos.write(0);
        fos.write(8);
        fos.write(0);
        fos.write(1);
        fos.write(0);
        fos.write(24);
        fos.write(0);


        for(int i = 0; i<64; i++) {
            switch(i) {
            case 2: case  3: case  4: case  5: case  9: case  14: case  16: case  19: case  20: case  23: case  24:
                case  26: case  29: case  31: case  32: case  39: case  40: case  42: case  45: case  47: case  49: case  54:
                    case  58: case  59: case  60: case  61:
                        fos.write(0);
                        fos.write(0);
                        fos.write(0);
                        break;
            case 0: case 1: case 6: case 7: case 8: case 15: case 48: case 55: case 56: case 57: case 62: case 63:
                fos.write(255);
                fos.write(255);
                fos.write(255);
                break;
            default:
                fos.write(0);
                fos.write(200);
                fos.write(0);
            }
        }

        //1x2 bytes [ending nulls]
        fos.write(0);
        fos.write(0);

    }catch(IOException e) {
        e.printStackTrace();
    }
}
}

错误:

Enter scale: 2
Original file size: 220
Scaled file size: 796
Scaled Length: 16
Scaled Width: 16
Bits per pixel: 3
bytes per scaled width: 48

counter - o - s - bArray[o]
1 --- 27 --- 27 --- -1
7 --- 28 --- 33 --- -1
13 --- 29 --- 39 --- -1
19 --- 30 --- 45 --- -1
25 --- 31 --- 51 --- -1
31 --- 32 --- 57 --- 0
37 --- 33 --- 63 --- 0
43 --- 34 --- 69 --- 0
43 --- 35 --- 123 --- 0
43 --- 36 --- 177 --- 0
43 --- 37 --- 231 --- 0
43 --- 38 --- 285 --- 0
43 --- 39 --- 339 --- 0
43 --- 40 --- 393 --- 0
43 --- 41 --- 447 --- 0
43 --- 42 --- 501 --- 0
43 --- 43 --- 555 --- 0
43 --- 44 --- 609 --- -1
43 --- 45 --- 663 --- -1
43 --- 46 --- 717 --- -1
43 --- 47 --- 771 --- -1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 819
    at test.BitmapResizer.main(BitmapResizer.java:107)

0 个答案:

没有答案