简体   繁体   English

带有Java Resizer的粉红色/紫色

[英]Pink/Purple Color with Java Resizer

I'm using Groovy&Grails and thumbnailator to resize thumbnails with this lines of code: 我正在使用Groovy&Grails和thumbnailator通过以下代码行调整缩略图的大小:

BufferedImage image = ImageIO.read(new FileInputStream("input.jpg"))
BufferedImage output = Thumbnails.of(image).size(400, 400).crop(Positions.CENTER).asBufferedImage()
ByteArrayOutputStream baos = new ByteArrayOutputStream()
ImageIO.write(output, "jpg", baos)
baos.flush()
OutputStream outputStream = new FileOutputStream("output.jpg")
baos.writeTo(outputStream)
baos.close()   

This works in 99% of all cases (same Java, OS, Sourcecode), with input.jpg. 使用input.jpg可以在99%的所有情况下(相同的Java,OS,源代码)工作。 But in some non-reproducible cases, the image will become this: 但是在某些不可复制的情况下,图像将变为:

颜色

I've checked other threads, such as the following link: Pink/Reddish tint while resizing jpeg images using java thumbnailator or imgscalr 我检查了其他线程,例如以下链接: 使用Java thumbnailator或imgscalr调整jpeg图像大小时的粉红色/偏红色调

I'm stuck with the situation that the exact same file works 99%, but in some, to be defined cases, doesn't. 我坚持完全相同的文件可以工作99%的情况,但是在某些情况下(可以定义的情况)却不能。

My question: How can this behaviour be reproduced? 我的问题:如何复制这种行为?

Although the input is expected to be the same every time, something must be changing. 尽管每次输入都期望相同,但是必须有所变化。 What you can do is write out the input file unchanged along with output.jpg . 您可以做的是不变地将输入文件和output.jpg一起写出。 When you encounter the problem then you'll be able to compare it's input file with the input file of a resize that have worked. 遇到问题时,您可以将其输入文件与有效的调整大小的输入文件进行比较。

There should be some difference. 应该有一些区别。 If there is, then the problem is likely in the process providing the input file. 如果存在,则可能是在提供输入文件的过程中出现了问题。 If there isn't, which would be quite surprising, then something is wrong with the resizing code. 如果没有,这将非常令人惊讶,那么调整大小的代码就会出问题。

TIP: You can use SHAx, MD5, etc to determine if the input files are different. 提示:您可以使用SHAx,MD5等来确定输入文件是否不同。

Confirming the input file 确认输入文件

To confirm the input file, you can write the incoming data to a separate file. 要确认输入文件,可以将传入数据写入单独的文件。

def inputCopy = new FileOutputStream("input-SOMETHING-UNIQUE-HERE.jpg")
def input = new WritingInputStream(
    new FileInputStream("input.jpg"),
    inputCopy)

BufferedImage image = ImageIO.read(input)
...
inputCopy.close()

The WritingInputStream writes data to an OutputStream as it's read from an InputStream . InputStream读取时, WritingInputStream将数据写入到OutputStream中。 The source code is shown below: 源代码如下所示:

class WritingInputStream extends FilterInputStream {
    private OutputStream output

    public WritingInputStream(InputStream input, OutputStream output) {
        super(input)

        this.output = output
    }

    int read() {
        int data = super.read()

        output.write(data)

        return data
    }

    int read(byte[] b) {
        int result = super.read(b)

        output.write(b)

        return result
    }

    int read(byte[] b, int off, int len) {
        int result = super.read(b, off, len)

        output.write(b, off, len)

        return result
    }    
}

An alternative to writing the inputCopy file out is to use an ByteArrayOutputStream and log the SHA1 of it's contents which you can then compare that to the SHA1 of the input file. 写出inputCopy文件的另一种方法是使用ByteArrayOutputStream并记录其内容的SHA1,然后可以将其与输入文件的SHA1进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.awt.Frame.setBackground(Color arg0)不显示PINK颜色 - java.awt.Frame.setBackground(Color arg0) not displaying PINK color 使用Java的照片调整大小 - Photo resizer using Java 制作返回 java 中当前颜色的蓝紫色彩虹的最佳方法是什么? - What is the best way to make a blue-purple rainbow that returns the current color in java? android 工作室中的按钮背景颜色始终为紫色 - button background color is always purple in android studio Java Swing-将面板全局更改为具有粉红色背景 - Java Swing - Globally Change Panels to Have Pink Background 使用java ImageIO读取和写入一些TIFF图像时输出TIFF中的粉红色背景颜色 - Pink background colour in output TIFF when reading and writing some TIFF images with java ImageIO 使用java thumbnailator或imgscalr调整jpeg图像大小时的粉红色/偏红色调 - Pink/Reddish tint while resizing jpeg images using java thumbnailator or imgscalr //#在Java中做什么? 在我的IDE中,它变成粉红色,但是我无法在任何地方在线找到它 - What does //# do in Java? In my IDE it turns pink, but I can't find it online anywhere 物品纹理为粉红色/黑色 - Item textures are pink/black setBackgroundColor 总是变成紫色 - setBackgroundColor is always turn purple
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM