在java中向右旋转90度

问题描述 投票:2回答:6

我无法将图像向右旋转90度。我需要能够在java中单独旋转图像。唯一的事情。不幸的是,我需要在特定的点绘制图像,并且没有一个方法可以分别对图像进行旋转,并且2.允许我设置x和y。任何帮助表示赞赏

public class Tumbler extends GraphicsProgram{

public void run() {
    setSize(1000,1000);
    GImage original = new GImage("sunset.jpg");
    add(original, 10, 10);
    int[][] pixels = original.getPixelArray();
    int height = pixels.length;
    int width = pixels[0].length;

    // Your code starts here
    int newheight = width;
    int newwidth = height;
    int[][] newpixels = new int[newheight][newwidth];

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {               
            newpixels[j][height-1-i] = pixels[i][j];            
        }
    }


    GImage image = new GImage(newpixels);
    add(image, width+20, 10);

    // Your code ends here
    }
java image swing rotation graphics2d
6个回答
1
投票

正如讨论here,您可以使用AffineTransformOp通过Math.PI / 2旋转图像;这相当于将图像顺时针旋转90°,如图所示here。另见Handling 90-Degree Rotations


4
投票

如果我们想要具有不错的性能(与直接复制像素相比快~10倍),我们绝对应该使用Graphics2D:

public static BufferedImage rotateClockwise90(BufferedImage src) {
    int width = src.getWidth();
    int height = src.getHeight();

    BufferedImage dest = new BufferedImage(height, width, src.getType());

    Graphics2D graphics2D = dest.createGraphics();
    graphics2D.translate((height - width) / 2, (height - width) / 2);
    graphics2D.rotate(Math.PI / 2, height / 2, width / 2);
    graphics2D.drawRenderedImage(src, null);

    return dest;
}

3
投票

这是我用来顺时针旋转BufferedImage 90度的代码。由于旋转90度是一种特殊情况,我认为任何角度的通用解决方案都不具备最佳性能。同样地,对于执行某种插值(双线性,双三次等)的解决方案,我使用BufferedImage.getRaster()来访问原始字节以提高性能,但是根据图像的结构/布局,这不太可能在所有情况下工作。因人而异。

public static BufferedImage rotateClockwise90(BufferedImage src) {

    int srcWidth = src.getWidth();
    int srcHeight = src.getHeight();
    boolean hasAlphaChannel = src.getAlphaRaster() != null;
    int pixelLength = hasAlphaChannel ? 4 : 3;
    byte[] srcPixels = ((DataBufferByte)src.getRaster().getDataBuffer()).getData();

    // Create the destination buffered image
    BufferedImage dest = new BufferedImage(srcHeight, srcWidth, src.getType());
    byte[] destPixels = ((DataBufferByte)dest.getRaster().getDataBuffer()).getData();
    int destWidth = dest.getWidth();

    int srcPos = 0; // We can just increment this since the data pack order matches our loop traversal: left to right, top to bottom. (Just like reading a book.)   
    for(int srcY = 0; srcY < srcHeight; srcY++) {
        for(int srcX = 0; srcX < srcWidth; srcX++) {

            int destX = ((srcHeight - 1) - srcY);
            int destY = srcX;

            int destPos = (((destY * destWidth) + destX) * pixelLength);

            if(hasAlphaChannel) {
                destPixels[destPos++] = srcPixels[srcPos++];    // alpha
            }
            destPixels[destPos++] = srcPixels[srcPos++];        // blue
            destPixels[destPos++] = srcPixels[srcPos++];        // green
            destPixels[destPos++] = srcPixels[srcPos++];        // red
        }
    }

    return dest;
}

3
投票

Ken答案的简化版本:

public static BufferedImage rotateClockwise90(BufferedImage src) {
    int w = src.getWidth();
    int h = src.getHeight();
    BufferedImage dest = new BufferedImage(h, w, src.getType());
    for (int y = 0; y < h; y++) 
        for (int x = 0; x < w; x++) 
            dest.setRGB(y, w - x - 1, src.getRGB(x, y));
    return dest;
}

0
投票

将图像旋转到90度,180度或270度角

public static BufferedImage rotateImage(BufferedImage src, int rotationAngle) {
    double theta = (Math.PI * 2) / 360 * rotationAngle;
    int width = src.getWidth();
    int height = src.getHeight();
    BufferedImage dest;
    if (rotationAngle == 90 || rotationAngle == 270) {
        dest = new BufferedImage(src.getHeight(), src.getWidth(), src.getType());
    } else {
        dest = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
    }

    Graphics2D graphics2D = dest.createGraphics();

    if (rotationAngle == 90) {
        graphics2D.translate((height - width) / 2, (height - width) / 2);
        graphics2D.rotate(theta, height / 2, width / 2);
    } else if (rotationAngle == 270) {
        graphics2D.translate((width - height) / 2, (width - height) / 2);
        graphics2D.rotate(theta, height / 2, width / 2);
    } else {
        graphics2D.translate(0, 0);
        graphics2D.rotate(theta, width / 2, height / 2);
    }
    graphics2D.drawRenderedImage(src, null);
    return dest;
}

0
投票

这是使用2D数组的代码:

  `private static int[][] rorateImage
            (int[][] imageArr, int rows, int columns, int flag){

    int rotatedImageArr[][] = new int[columns][rows];

    if(flag==1){  //90 degree rotation in right
        for (int i = 0; i < columns; i++) {
            for (int j = 0; j < rows; j++) {
                rotatedImageArr[i][j] = imageArr[rows - 1 - j][i];
            }
        }
    }

    if(flag==0){ //90 degree rotation in left
        for (int i = 0; i < columns; i++) {
            for (int j = 0; j < rows; j++) {
                rotatedImageArr[i][j] = imageArr[j][columns -1 -i];
            }
        }
    }
    return rotatedImageArr;
}`
© www.soinside.com 2019 - 2024. All rights reserved.