0

我有一个 2D 布尔数组,它是一个分段图像,如下所示:

 000000000000000000
 000000011110000000
 000001110111100000
 000001111110100000
 000000111101100000
 000000111011000000

我想裁剪图片并将其调整为特定大小,如下所示:

 00111100
 11101111
 11111101
 01111011
 01110110

我试图使用 BufferedImage 来保存它并使用 getSubimage() 但我无法正确设置边界......

我也尝试过这种方法,其中我拥有缓冲图像的核心:

            int Width = buffer.getWidth();
            int Height = buffer.getHeight();
            int x1= (int) (core.getX()) - (Width/2);
            int y1= (int)(core.getY()) - (Height/2);
            buffer = buffer.getSubimage(x1, y1, Width, Height); 

如何裁剪并调整大小?

4

1 回答 1

0
    int [][] arr ={ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0},
     {0,0,0,0,0,1,1,1,0,1,1,1,1,0,0,0,0,0},
     {0,0,0,0,0,1,1,1,1,1,1,0,1,0,0,0,0,0},
     {0,0,0,0,0,0,1,1,1,1,0,1,1,0,0,0,0,0},
     {0,0,0,0,0,0,1,1,1,0,1,1,0,0,0,0,0,0} };



    int lowHorizontal =arr[0].length; // give it length of columns "how many columns
    int highHorizontal =0;

    for(int i=0; i<arr.length; i++){
        for(int j=0; j<arr[0].length; j++){

            if(arr[i][j] == 1 ){
                if(lowHorizontal > j) 
                    lowHorizontal = j;
            }
        }
    }

    for(int i=arr.length-1; i>=0; i--){
        for(int j=arr[0].length-1; j>=0; j--){
            if(arr[i][j]==1){
                if(highHorizontal < j)
                    highHorizontal = j;
            }
        }
    }


    for(int i=0; i<arr.length; i++){
        for(int j=lowHorizontal; j<=highHorizontal; j++){
            System.out.print(arr[i][j] +" ");
        }
        System.out.println();
    }

这段代码将计算水平索引,我相信有更好的方法。尝试应用相同的方法来计算垂直索引。

我打印时 j 的索引应该在边界内,这就是为什么我把 j<=highHo... 这是上面代码的输出

0 0 0 0 0 0 0
0 0 1 1 1 1 0
1 1 1 0 1 1 1 
1 1 1 1 1 1 0 
0 1 1 1 1 0 1 
0 1 1 1 0 1 1 
于 2015-04-18T23:36:29.827 回答