Does image conversion result in a red image?

I am trying to convert an image by flipping it horizontally and resizing it. The problem is that when the color transformation of the image is all strange, he got this reddish tint. Is it possible to fix this somehow, I think I read somewhere that this might be some error in the AWT library, but I'm not sure?

Here is the code:

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class LocalImageSizeFlip {

public static void main(String[] args) {
    BufferedImage img = null;

    try {
        img = ImageIO.read(new File("C:\\picture.jpg"));
        AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
        tx.translate(0, -img.getHeight(null));
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
        img = op.filter(img, null);
        img = resize(img, 100, 75);
        File newFile = new File("newPicture.jpg");
        ImageIO.write(img, "JPEG", newFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static BufferedImage resize(BufferedImage image, int width, int height) {
    BufferedImage resizedImage = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();
    return resizedImage;
    }   
}
+5
source share
3 answers

When creating an image, hue usually means that the image is rendered using the wrong color space, Adobe RGB and sRGB are a constant favorite. Try changing TYPE_INT_ARGB to TYPE_INT_RGB in code.

+6
source

: BufferedImage.TYPE_3BYTE_BGR

0

If you already have the converted images and they are almost pinkish / reddish.

You can convert them to RGB again.

try {
        File folder = new File("photo/old");
        File[] list = folder.listFiles();
        for (File f : list) {
            String url = f.getAbsolutePath();
            String replce1 = url.replace('\\', '/');
            File file = new File(replce1);
            if (file.exists()) {
                FileInputStream fis = new FileInputStream(file);

                byte[] buff = new byte[fis.available()];
                fis.read(buff);

                BufferedImage bi = ImageIO.read(file); 
                BufferedImage biOriginal = new BufferedImage(1200, 800,
                    BufferedImage.TYPE_INT_RGB);
                Graphics2D g = resizedImage.createGraphics();
                g.drawImage(bi, 0, 0, 1200, 800, null);
                g.dispose();

                fis.close();

                FileOutputStream fos2 = new FileOutputStream("photo/new/"+file.getName());
                ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
                ImageIO.write(biOriginal, Main.extensionWithotDot, baos2);
                baos2.flush();
                byte[] imageInByte2 = baos2.toByteArray();
                fos2.write(imageInByte2);
                fos2.close();
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
0
source

All Articles