简体   繁体   English

Java旋转图像将背景的一部分变成黑色

[英]Java rotate image turns part of background black

When I try to rotate an image it appears that a part of the background turns black (my images are transparents) 当我尝试旋转图像时,似乎背景的一部分变成了黑色(我的图像是透明的)

Background white 背景白色
Part of background turns black 背景部分变成黑色

Here's the code that rotate the image : 这是旋转图像的代码:

public BufferedImage rotate(int height, int width, BufferedImage originalImg, int angle) {
    BufferedImage rotateImage = null;
    try {
        rotateImage = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
        AffineTransform a90 = AffineTransform.getRotateInstance(Math.toRadians(angle), height / 2, width / 2);
        AffineTransformOp op90 = new AffineTransformOp(a90, AffineTransformOp.TYPE_BILINEAR);
        op90.filter(originalImg, rotateImage);
    }
    catch (Exception e) {
        System.err.println(e);
    }
    return rotateImage;
}

So, I downloaded you "original" image (which is not square), modified it so it was square, run your code, got a java.awt.image.ImagingOpException: Unable to transform src image exception, changed BufferedImage.TYPE_INT_RGB to BufferedImage.TYPE_INT_ARGB and got... 因此,我下载了“原始”图像(不是正方形),对其进行了修改,使其java.awt.image.ImagingOpException: Unable to transform src image正方形,运行您的代码,得到了java.awt.image.ImagingOpException: Unable to transform src image异常,将BufferedImage.TYPE_INT_RGB更改为BufferedImage.TYPE_INT_ARGB并得到...

旋转

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Main {

    public static void main(String[] args) {
        try {
            BufferedImage img = ImageIO.read(Main.class.getResource("/Block.jpg"));
            BufferedImage rotate = rotate(img.getHeight(), img.getWidth(), img, 90);

            JPanel panel = new JPanel();
            panel.add(new JLabel(new ImageIcon(img)));
            panel.add(new JLabel(new ImageIcon(rotate)));

            JOptionPane.showMessageDialog(null, panel);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static BufferedImage rotate(int height, int width, BufferedImage originalImg, int angle) {
        BufferedImage rotateImage = null;
        try {
            rotateImage = new BufferedImage(height, width, BufferedImage.TYPE_INT_ARGB);
            AffineTransform a90 = AffineTransform.getRotateInstance(Math.toRadians(angle), height / 2, width / 2);
            AffineTransformOp op90 = new AffineTransformOp(a90, AffineTransformOp.TYPE_BILINEAR);
            op90.filter(originalImg, rotateImage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rotateImage;
    }

}

My, "gut" feeling is to also modify the rotate method, as it shouldn't need width and height values; 我的“直觉”感觉是还要修改rotate方法,因为它不需要widthheight值。

public static BufferedImage rotate(BufferedImage originalImg, int angle) {
    BufferedImage rotateImage = null;
    try {
        rotateImage = new BufferedImage(originalImg.getWidth(), originalImg.getHeight(), BufferedImage.TYPE_INT_ARGB);
        AffineTransform a90 = AffineTransform.getRotateInstance(Math.toRadians(angle), originalImg.getWidth() / 2, originalImg.getHeight() / 2);
        AffineTransformOp op90 = new AffineTransformOp(a90, AffineTransformOp.TYPE_BILINEAR);
        op90.filter(originalImg, rotateImage);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotateImage;
}

it should be using the original image's dimensions directly. 它应该直接使用原始图像的尺寸。 This is will highlight possible errors in your images. 这将突出显示图像中可能存在的错误。 This also assumes that you only want to rotate the image by increments of 90 degrees 这还假设您只想将图像旋转90度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM