如何压缩代码:了解旋转后图像的限制

How to condense code : know the limits of an image after rotation

我是 Java 的初学者。

我有一个图像,我知道它的行数和列数。我想计算旋转后新图像的限制。 这是我的代码:

       BufferedImage myImage = ImageIO.read( new File( "D:\Users...jpg" ) );
        xmaxOrigine = myImage.getWidth() - 1;
        ymaxOrigine = myImage.getHeight() - 1;
        angle = 12;

        angleRadian = Math.toRadians( angle );
        cos = Math.cos( angleRadian );
        sin = Math.sin( angleRadian );

        p1X = (int) ( ( xmaxOrigine * cos ) - ( ymaxOrigine * sin ) );
        p1Y = (int) ( ( xmaxOrigine * sin ) + ( ymaxOrigine * cos ) );

        xmin_f = xmin_f < p1X ? xmin_f : p1X;
        xmax_f = xmax_f < p1X ? p1X : xmax_f;
        ymin_f = ymin_f < p1Y ? ymin_f : p1Y;
        ymax_f = ymax_f < p1Y ? p1Y : ymax_f;

        p2X = (int) ( ( 0  * cos ) - ( ymaxOrigine * sin ) );
        p2Y = (int) ( ( 0 * sin ) + ( ymaxOrigine * cos ) );

        xmin_f = xmin_f < p2X ? xmin_f : p2X;
        xmax_f = xmax_f < p2X ? p2X : xmax_f;
        ymin_f = ymin_f < p2Y ? ymin_f : p2Y;
        ymax_f = ymax_f < p2Y ? p2Y : ymax_f;

        p3X = (int) ( ( xmaxOrigine * cos ) - ( 0 * sin ) );
        p3Y = (int) ( ( xmaxOrigine * sin ) + ( 0 * cos ) );

        xmin_f = xmin_f < p3X ? xmin_f : p3X;
        xmax_f = xmax_f < p3X ? p3X : xmax_f;
        ymin_f = ymin_f < p3Y ? ymin_f : p3Y;
        ymax_f = ymax_f < p3Y ? p3Y : ymax_f;

        p4X = 0;
        p4Y = 0;

        xmin_f = xmin_f < p4X ? xmin_f : p4X;
        xmax_f = xmax_f < p4X ? p4X : xmax_f;
        ymin_f = ymin_f < p4Y ? ymin_f : p4Y;
        ymax_f = ymax_f < p4Y ? p4Y : ymax_f;

        widthFinal = xmax_f - xmin_f;
        heightFinal = ymax_f - ymin_f;

如您所见,我为每个点寻找 xmin、xmax、ymin、ymax。如果可以的话,我想做一次这个操作。

感谢帮助

我做了类似的东西:

public int maxOfAll(int a, int b, int c, int d) {
    //Look for the maximum of 4 variable
    int temp1max = Math.max( a, b );
    int temp2max = Math.max( temp1max, c );
    int temp3 = Math.max( temp2max, d );
    return temp3;
}

public int minOfAll (int a, int b, int c, int d) {
    //Look for the minimum of 4 variable
    int temp1 = Math.min( a, b );
    int temp2 = Math.min( temp1, c );
    int temp3 = Math.min( temp2, d );
    return temp3;
}

你同意吗?

如果我正确理解你的代码和问题,你想在旋转后找到图像的边界。如果这不是一个任务,你应该自己实现它,你应该只使用 AffineTransformOp.getBounds2D(BufferedImage) 方法,它正是为了这个目的而制作的:

// Read the image and set angle (as before)
BufferedImage myImage = ImageIO.read(...));
double angle = 12;

// Create AffineTransformOp to do the calculation
AffineTransform rotation = AffineTransform.getRotateInstance(angle);
AffineTransformOp op = new AffineTransformOp(rotation, null);

Rectangle bounds = op.getBounds2D(myImage).getBounds(); // Correctly rounded bounds

// You can now use bounds.x, bounds.y, bounds.width and bounds.height as you like.
// Example:

widthFinal = bounds.width;
heightFinal = bounds.height;