coobird / thumbnailator

Thumbnailator - a thumbnail generation library for Java
MIT License
5.14k stars 784 forks source link

background-color was changed !!! #164

Closed szd1993 closed 3 years ago

szd1993 commented 3 years ago

original-img: http://img.alicdn.com/imgextra/i2/2208557321139/O1CN010mpNHj1KHhkOx2d6C_!!2208557321139.jpg

new-img: http://t00img.yangkeduo.com/openapi/images/2020-10-23/795a65accba9cfd8e318674e23ec5592.jpg

the backgroud-color was changed into red ! why ? how to avoid it

     Thumbnails.of(new ByteArrayInputStream(content))
                .forceSize(width , height)
                .outputFormat("JPG")
                .outputQuality(1f)
                .toOutputStream(byteArrayOutputStream);
coobird commented 3 years ago

@szd1993, this particular case seems to be a problem with the JPEG reader that comes with Java.

You could try using the TwelveMonkeys ImageIO library which was able to properly read the JPEG image you posted. Basically, it's as simple as adding a Maven dependency to your project.

bby0 commented 3 years ago

excuse me,There is no solution,I use all TwelveMonkeys jar in the maven @coobird

szd1993 commented 3 years ago
    Image imageTookit = Toolkit.getDefaultToolkit().createImage(new URL(imageUrl));
    BufferedImage sourceImg = toBufferedImage(imageTookit);

   public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }
    image = new ImageIcon(image).getImage();
    boolean hasAlpha = false;
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment
            .getLocalGraphicsEnvironment();
    try {
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image
                .getHeight(null), transparency);
    } catch (HeadlessException e) {
    }
    if (bimage == null) {
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image
                .getHeight(null), type);
    }
    Graphics g = bimage.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
}

@coobird it works ! So, the key is BufferedImage.class

push a new version to fix it , come on

szd1993 commented 3 years ago
    Image imageTookit = Toolkit.getDefaultToolkit().createImage(new URL(imageUrl));
    BufferedImage sourceImg = toBufferedImage(imageTookit);

   public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }
    image = new ImageIcon(image).getImage();
    boolean hasAlpha = false;
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment
            .getLocalGraphicsEnvironment();
    try {
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image
                .getHeight(null), transparency);
    } catch (HeadlessException e) {
    }
    if (bimage == null) {
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image
                .getHeight(null), type);
    }
    Graphics g = bimage.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
}

@coobird it works ! So, the key is BufferedImage.class

push a new version to fix it , come on

 bimage = gc.createCompatibleImage(image.getWidth(null), image
                .getHeight(null), 1);

this line 'Transparency.OPAQUE;'

thiagoalgeri commented 3 years ago

Please, we have the same issue!!

coobird commented 3 years ago

The workaround to use Toolkit may work fine for certain narrow use cases, but is only a workaround and is not a solution that uses the Java Image I/O API, so it won't support use of other image formats. It's not a generalized solution that can be incorporated into Thumbnailator, since it's relied on for many different use cases.

If the workaround works well for you, then that may be a good approach for your use case.

I've verified once again that the JPEG posted in the original image is read fine (i.e. no color distortion) using the TwelveMonkey's JPEG reader/writer plugin (rather than the default one bundled in the Java runtime) so for the general use case, using that JPEG plugin would be a better approach.