【问题标题】:Rotation by AffineTransform makes saved image emptyAffineTransform 旋转使保存的图像为空
【发布时间】:2012-05-13 18:30:31
【问题描述】:

好的,我的问题很简单,在执行 AffineTransform 后,我的图像没有正确保存(但是它在 JPanel 上正确绘制!)。这真的很奇怪,所以任何提示都非常感谢......

看一下代码:

    public BufferedImage performRotation(BufferedImage bi){

    if (angle!=180){
        at.translate(0.5*bi.getHeight(), 0.5*bi.getWidth());
        if(clockwise){
            at.rotate(Math.toRadians(angle));
        }else{
            at.rotate(Math.toRadians(-angle));
        }            
        at.translate(-0.5*bi.getWidth(), -0.5*bi.getHeight());
    }
    else if(angle==180){
        at.translate(0.5*bi.getWidth(), 0.5*bi.getHeight());
        at.rotate(Math.toRadians(angle));
        at.translate(-0.5*bi.getWidth(), -0.5*bi.getHeight());
    }

    AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
    BufferedImage bi2 = op.filter(bi, null);

    try {                  
    ImageIO.write(bi, "bmp", new File("BEFORE filterORIG.bmp"));
    ImageIO.write(bi2, "bmp", new File("AFTER filterNEW.bmp"));
    } catch (IOException ex) {
        Logger.getLogger(DrawingField.class.getName()).log(Level.SEVERE, null, ex);
    }

filterORIG 之前的文件已正确保存 -> 有一个图像,但它已预先旋转。

File AFTER... 保存为空白文件。

真正有趣的是,前面提到的事实是,这种转换在我用作显示的 JPanel 上被普遍显示(我可以观察到所需转换的效果)

任何帮助表示赞赏...

【问题讨论】:

  • 我猜这是因为返回的不是转换的结果,而是一个“过滤”版本(不管是什么)。我的猜测是 new BufferedImage bi2(); op.filter(bi,bi2); 是你想要的。但我对它的了解还不够,无法写出完整的答案。
  • 是的,它的作品!多谢!几个小时以来我一直在努力解决它!我真的很感激!

标签: java image-processing rotation save affinetransform


【解决方案1】:

尝试写png图片,即:

ImageIO.write(bi, "png", new File("BEFORE filterORIG.png"));
ImageIO.write(bi2, "png", new File("AFTER filterNEW.png"));

生成的图像 (bi2) 可能有一个 aplha 通道,ImageIO 可能不允许将带有 aplha 的图像编码为bmp

或者,使用TYPE_INT_RGB 颜色模型创建目标图像,并将其用作filter() 方法中的第二个参数。

【讨论】:

    猜你喜欢
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    相关资源
    最近更新 更多