【问题标题】:After transform image using AffineTransform, photo get low quality使用 AffineTransform 变换图像后,照片质量低
【发布时间】:2016-04-28 21:00:45
【问题描述】:

我正在尝试使用 EXIF 信息修复照片方向,照片旋转正确,但旋转后它们变得非常低质量......我的猜测是在写入新图像期间传递的参数是错误的。任何帮助表示赞赏。

//code get Exif information 
 Metadata metadata = ImageMetadataReader.readMetadata(outputFile);
        Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
        if(directory == null) {
            logger.warn("no EXIF info.");
            outputFile.delete();
            return;
        }
        JpegDirectory jpegDirectory = metadata.getFirstDirectoryOfType(JpegDirectory.class);
        int orientation;
        try {
            orientation = directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
            if(orientation != 1) {
                //rotate image
                int w = jpegDirectory.getImageWidth();
                int h = jpegDirectory.getImageHeight();
                ImageInformation imageInformation = new ImageInformation(orientation, w, h);
                AffineTransform affineTransform = getExifTransformation(imageInformation);

                InputStream pictureStream = new FileInputStream(outputFile);
                BufferedImage pictureBuffer = ImageIO.read(pictureStream);
                pictureStream.close();
                if (pictureBuffer == null) {
                    logger.warn("The picture buffer parsed is null.");
                }
                pictureBuffer = transformImage(pictureBuffer, affineTransform);


    //code do image transfer
    public static BufferedImage transformImage(BufferedImage image, AffineTransform transform) throws Exception {

    AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
    BufferedImage destinationImage = op.createCompatibleDestImage(image,  null );
    Graphics2D g = destinationImage.createGraphics();
    g.setBackground(Color.WHITE);
    g.clearRect(0, 0, destinationImage.getWidth(), destinationImage.getHeight());
    destinationImage = op.filter(image, destinationImage);
    return destinationImage;
}

【问题讨论】:

  • 我不认为图像质量下降,大多数图案看起来像以前一样清晰(见奶瓶上的麋鹿)。但是,我会说颜色混合在一起,整体照明减少了。

标签: java photo bufferedimage affinetransform


【解决方案1】:

感谢所有帮助:-) 把transform函数改成这个后,问题解决了,不知道为什么会这样,gpasch可能是对的

    public static BufferedImage transformImage(BufferedImage image, AffineTransform transform) throws Exception {

    AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
    BufferedImage destinationImage = new BufferedImage(image.getWidth(),image.getHeight(), image.getType());
    destinationImage = op.filter(image, destinationImage);
    return destinationImage;
}

【讨论】:

    【解决方案2】:

    这可能会解决您的问题。根据 AffineTransformOp

    "If destCM is null, an appropriate ColorModel is used; this ColorModel may 
    have an alpha channel even if the source ColorModel is opaque."
    

    因此我建议如下:

    AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
    BufferedImage destinationImage = op.createCompatibleDestImage(image,  null );
    destinationImage = op.filter(image, null);
    return destinationImage;
    

    甚至放弃兼容的图像:

    AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
    BufferedImage destinationImage = op.filter(image, null);
    return destinationImage;
    

    我也不确定双三次是否那么重要,但可能不是问题。

    因为兼容的图像返回带有 alpha 的图像,即透明

    这个

     Graphics2D g = destinationImage.createGraphics();
    g.setBackground(Color.WHITE);
    g.clearRect(0, 0, destinationImage.getWidth(), destinationImage.getHeight());
    

    会在图片上加一层透明度;之后绘制的图像与白色融合在一起。

    【讨论】:

      【解决方案3】:

      保持简单,您应该使用操作预期的尺寸:

      AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
      BufferedImage destinationImage = op.filter(bImage, op.createCompatibleDestImage(bImage, null));
      

      【讨论】:

        猜你喜欢
        • 2014-12-24
        • 1970-01-01
        • 1970-01-01
        • 2015-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-27
        • 1970-01-01
        相关资源
        最近更新 更多