旋转位图像素

时间:2012-06-24 06:52:52

标签: java bitmap rotation transformation

我正在尝试旋转位图,其中像素存储在数组int pixels[]中。我得到了以下方法:

public void rotate(double angle) {
    double radians = Math.toRadians(angle);
    double cos, sin;
    cos = Math.cos(radians);
    sin = Math.sin(radians);
    int[] pixels2 = pixels;
    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++) {
            int centerx = this.width / 2, centery = this.height / 2;
            int m = x - centerx;
            int n = y - centery;
            int j = (int) (m * cos + n * sin);
            int k = (int) (n * cos - m * sin);
            j += centerx;
            k += centery;
            if (!((j < 0) || (j > this.width - 1) || (k < 0) || (k > this.height - 1)))

                try {
                    pixels2[(x * this.width + y)] = pixels[(k * this.width + j)];
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
    pixels = pixels2;

}

但它只是给了我疯狂的结果。有谁知道错误在哪里?

2 个答案:

答案 0 :(得分:2)

该行

int[] pixels2 = pixels;

应该复制数组,但您只是复制对它的引用。使用pixels.clone()。实际上,您只需要一个新的空数组,因此new int[pixels.lenght]就足够了。最后,您需要System.arraycopy将新内容复制到旧数组中。

您的代码中还有其他问题 - 您正在混合行和列。一些表达式被编写为好像逐行存储图像,其他表达式就像逐列一样存储。如果逐行(我的假设),那么这没有意义:x*width + y。它应该显示为y*width + x - 您正在向下跳过y行,然后向右移动x列。总而言之,我有这个代码可以正常工作:

import static java.lang.System.arraycopy;

public class Test
{
  private final int width = 5, height = 5;
  private int[] pixels = {0,0,1,0,0,
                          0,0,1,0,0,
                          0,0,1,0,0,
                          0,0,1,0,0,
                          0,0,1,0,0};

  public Test rotate(double angle) {
    final double radians = Math.toRadians(angle),
    cos = Math.cos(radians), sin = Math.sin(radians);
    final int[] pixels2 = new int[pixels.length];
    for (int x = 0; x < width; x++)
      for (int y = 0; y < height; y++) {
        final int
          centerx = this.width / 2, centery = this.height / 2,
          m = x - centerx,
          n = y - centery,
          j = ((int) (m * cos + n * sin)) + centerx,
          k = ((int) (n * cos - m * sin)) + centery;
        if (j >= 0 && j < width && k >= 0 && k < this.height)
          pixels2[(y * width + x)] = pixels[(k * width + j)];
      }
    arraycopy(pixels2, 0, pixels, 0, pixels.length);
    return this;
  }
  public Test print() {
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++)
        System.out.print(pixels[width*y + x]);
      System.out.println();
    }
    System.out.println();
    return this;
  }
  public static void main(String[] args) {
    new Test().print().rotate(-45).print();
  }
}

答案 1 :(得分:0)

public void render(float nx, float ny, float nz, float size, float rotate) {

    int wid = (int) ((width - nz) * size);

    int hgt = (int) ((height - nz) * size);

    if (wid < 0 || hgt < 0) {
        wid = 0;
        hgt = 0;
    }

    for (int x = 0; x < wid; x++) {
        for (int y = 0; y < hgt; y++) {
            double simple = Math.PI;
            int xp = (int) (nx +

            Math.cos(rotate) * ((x / simple) - (wid / simple) / 2) + Math
                    .cos(rotate + Math.PI / 2)
                    * ((y / simple) - (hgt / simple) / 2));
            int yp = (int) (ny + Math.sin(rotate)
                    * ((x / simple) - (wid / simple) / 2) + Math.sin(rotate
                    + Math.PI / 2)
                    * ((y / simple) - (hgt / simple) / 2));

            if (xp + width < 0 || yp + height < 0 || xp >= Main.width
                    || yp >= Main.height) {
                break;
            }
            if (xp < 0
                    || yp < 0
                    || pixels[(width / wid) * x + ((height / hgt) * y)
                            * width] == 0xFFFF00DC) {
                continue;
            }
            Main.pixels[xp + yp * Main.width] = pixels[(width / wid) * x
                    + ((height / hgt) * y) * width];
        }
    }
}

这对我来说只是一个新的旋转,但这个过程就是正常旋转。它仍然需要很多修复 - 它效率低,速度慢。但是在一个小程序中,这段代码可行。我发布这个,这样你就可以接受它,并使它变得更好。 :)