如何使用java保留旋转图像的原始颜色

时间:2017-07-01 13:23:15

标签: java image exif

我有一个接收图像文件的java应用程序。我想使用exif信息旋转它们。

在StackOverflow上,我找到了两种不同的方法,在两种情况下我都以BufferedImage开头:

        BufferedImage image=ImageIO.read(sourceFile.getInputStream());

第一个解决方案来自this post

        ImageTransformer.ImageInformation imageInformation=ImageTransformer.readImageInformation(sourceFile.getInputStream());
        AffineTransform tranform=ImageTransformer.getExifTransformation(imageInformation);
        image=ImageTransformer.transformImage(image,tranform);

在这里的一些帖子中也推荐了第二个,使用Thumbnailator:

BufferedImage image = Thumbnails.of(sourceFile.getInputStream()).scale(1).asBufferedImage();

两种方式都可以正确旋转我的图像。但是这两种解决方案都会改变颜色,这是我不想要的。我会附上两个文件," white"一个是旋转错误的原始文件。这正是我在注释掉两个旋转代码时所实现的。 White image

另一个是红色的,是一个旋转正确但颜色错误的。 Red Image

如何仅通过更改旋转来获得原始颜色?

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,事实证明您不应该将一种格式转换为另一种格式(BufferedImage转换为ByteArrayOutputStream,反之亦然)。如果您有字节数组,请使用ByteArrayOutputStream和ByteArrayInputStream。

$ldapResource = ldap_connect('ldap://*********');
ldap_set_option($ldapResource, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapResource, LDAP_OPT_REFERRALS, 0);

ldap_bind($ldapResource, "CN=My company standard Account, OU=*******", "account_password");

答案 1 :(得分:1)

我一直试图将图像的中心与框架的中心对齐,但我没有找到一种好的方法,但我设法旋转图像而不会失去其属性:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;

public class Rotate extends JComponent {
    // Attributes
    private String route = null;
    private double degree = 0.0;

    // Constructor
    public Rotate(String route, double degree) {
        this.setRoute(route);
        this.setDegree(degree);
    }

    // Create a bufferedImage for the given route
    private BufferedImage getBufferedImage() {
        Image image = null;
        try {
            image = ImageIO.read(new File(getRoute()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (BufferedImage) image;
    }

    // Paint the image
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(getDegree()));
        g2.setTransform(at);
        // Here I tried to calculate you must add the coordenates to start
        // drawing the image in your frame
        g2.drawImage(getBufferedImage(), 0, 0, null);
    }

    public double getDiagonal() {
        return Math.hypot(getBufferedImage().getWidth(), getBufferedImage().getHeight());
    }

    public String getRoute() {
        return route;
    }

    public void setRoute(String route) {
        this.route = route;
    }

    public double getDegree() {
        return degree;
    }

    public void setDegree(double degree) {
        this.degree = degree;
    }

}

进行测试:

import java.awt.Container;

import javax.swing.JFrame;

public class ImageRotation {

    public static void main(String[] args) {
        // The route of the image and the degrees of rotation
        String route = "C:\\Users\\Public\\Pictures\\Sample Pictures\\download.jpg";
        double degrees = 30;
        // Frame to display the image
        JFrame jf = new JFrame("Image Rotated");
        // Introduce the route and degrees
        Rotate tl = new Rotate(route, degrees);
        // Set the size of the frame
        int hyp = (int) tl.getDiagonal();
        jf.setSize(hyp, hyp);
        // Print the rotated image in the frame
        Container cp = jf.getContentPane();
        cp.add(tl);
        // Display
        jf.setVisible(true);
    }
}