在 java 中保持组件方向的同时旋转数组

Rotate array while maintaining orientation of components in java

对于我正在进行的项目,我必须读取字体精灵 sheet(font.png,如下所示)并将每个单独的字符转换为索引数组。

读取文件并将其转换为数组时,我使用了流畅的代码:

    BufferedImage img = ImageIO.read(new File("D:\Downloads\font.png"));
    int[][] font = new int[8][760];
    for(int i = 0; i < img.getWidth(); i++) {
        for(int j = 0; j < img.getHeight(); j++) {
            font[j][i] = (img.getRGB(i, j) != - 1) ? 1:0;
        }
    }
    System.out.println(Arrays.deepToString(font).replace("], ", "]\n").replace("[[", "[").replace("]]", "]"));

它给我一个 8x760 int[][],其中每个黑色像素为 1,每个白色像素为 0(图像中的字符为 space、!、"、#)

为了将数组旋转为 760x8 数组,我尝试了以下代码:

    int[][] rFont = new int[760][8];
    for(int a = 0; a < 95; a++) {
        for(int i = 0; i < 8; i++) {
            for(int j = 0; j < 8; j++) {
                rFont[i + 8*a][j] = font[j][i + 8*a];
            }
        }
    }
    System.out.println(Arrays.deepToString(rFont).replace("], ", "]\n").replace("[[", "[").replace("]]", "]"));

除了字体中的每个字母也旋转 90 度外,此代码非常适合旋转数组。如下图所示,很明显“!”也被旋转。

我的代码哪里出错了,没有保持字符的方向?

替换

        for(int j = 0; j < 8; j++) {
            rFont[i + 8*a][j] = font[j][i + 8*a];
        }

                for (int j = 7; j >= 0; j--) {
                    rFont[i + 8 * a][7 - j] = font[j][i + 8 * a];
                }