如何压缩代码:了解轮换后图像的限制

时间:2015-08-19 07:00:50

标签: java rotation bufferedimage trigonometry

我是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。如果可能的话,我想做一次这个操作。

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

我做了类似的事情:

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;
}
你同意吗?

答案 1 :(得分:0)

如果我理解你的代码和问题,你想在旋转后找到图像的边界。如果这不是一个任务,你应该自己实现这个,你应该只使用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;