Java旋转图像将背景的一部分变成黑色 [英] Java rotate image turns part of background black

查看:95
本文介绍了Java旋转图像将背景的一部分变成黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试旋转图像时,似乎背景的一部分变成了黑色(我的图像是透明的)

  import java.awt.geom.AffineTransform;导入java.awt.image.AffineTransformOp;导入java.awt.image.BufferedImage;导入java.io.IOException;导入javax.imageio.ImageIO;导入javax.swing.ImageIcon;导入javax.swing.JLabel;导入javax.swing.JOptionPane;导入javax.swing.JPanel;公共班级主要{公共静态void main(String [] args){尝试 {BufferedImage img = ImageIO.read(Main.class.getResource("/Block.jpg"));BufferedImage rotation = rotation(img.getHeight(),img.getWidth(),img,90);JPanel面板=新的JPanel();panel.add(new JLabel(new ImageIcon(img)));panel.add(new JLabel(new ImageIcon(rotate)));JOptionPane.showMessageDialog(null,panel);} catch(IOException ex){ex.printStackTrace();}}公共静态BufferedImage rotation(int height,int width,BufferedImage originalImg,int angle){BufferedImage rotationImage = null;尝试 {rotationImage =新的BufferedImage(高度,宽度,BufferedImage.TYPE_INT_ARGB);AffineTransform a90 = AffineTransform.getRotateInstance(Math.toRadians(angle),height/2,width/2);AffineTransformOp op90 =新的AffineTransformOp(a90,AffineTransformOp.TYPE_BILINEAR);op90.filter(originalImg,rotationImage);} catch(Exception e){e.printStackTrace();}返回rotateImage;}} 

我的直觉"感觉是还要修改 rotate 方法,因为它不需要 width height 值;

 公共静态BufferedImage rotation(BufferedImage originalImg,int angle){BufferedImage rotationImage = null;尝试 {rotationImage = 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 =新的AffineTransformOp(a90,AffineTransformOp.TYPE_BILINEAR);op90.filter(originalImg,rotationImage);} catch(Exception e){e.printStackTrace();}返回rotateImage;} 

它应该直接使用原始图像的尺寸.这将突出显示图像中可能存在的错误.这还假设您只想将图像旋转90度

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...

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;

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

这篇关于Java旋转图像将背景的一部分变成黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆