简体   繁体   English

jpg不会被ImageIO.write()覆盖

[英]jpg not being overwritten by ImageIO.write()

I've made the following example, I can't figure out why it isn't working. 我做了下面的例子,我不知道为什么它不起作用。

  • None of my checks are returning false, 我的所有支票都没有退还假票,
  • I have read and write permissions, 我拥有读写权限,
  • the imageIO responds that it has in fact overwritten the image, imageIO回应说它实际上已经覆盖了图像,
  • no exceptions are thrown... 没有异常被抛出...

but the image still hasn't changed on disk. 但是映像仍未在磁盘上更改。

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class example
{
    public static void main(String[] args)
        {
            try
                {
                    // load a jpg file
                    File imageFile = getFile();

                    // Make a Buffered Image from it
                    BufferedImage img = ImageIO.read(imageFile);

                    // for every pixel in the image
                    for (int x = 0; x < img.getWidth(); x++)
                        for (int y = 0; y < img.getHeight(); y++)
                            // check if the RGB integer is an odd number
                            if (img.getRGB(x, y) % 2 != 0)
                                // make it an even number if it is odd (the OCD god demands it!)
                                img.setRGB(x, y, img.getRGB(x, y) - 1);

                    // Write the OCD friendly version to the file
                    System.out.println("Was overwritten: " + ImageIO.write(img, "jpg", imageFile));
                    System.out.println();

                    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                    // Select the same file again
                    imageFile = getFile();

                    // Make the bufferedImage from it
                    img = ImageIO.read(imageFile);

                    // for every pixel in the image
                    for (int x = 0; x < img.getWidth(); x++)
                        for (int y = 0; y < img.getHeight(); y++)
                            // check if the RGB integer is an odd number
                            if (img.getRGB(x, y) % 2 != 0)
                                {
                                    // Report the failing
                                    System.out.println("It didn't work :(");
                                    // Stop the loop
                                    x = img.getWidth();
                                    y = img.getHeight();
                                }
                }
            catch (Exception e)
                {
                    e.printStackTrace();
                }
        }

    public static File getFile()
        {
            // set up a file chooser that only accepts jpgs
            JFileChooser chooser = new JFileChooser();
            chooser.setFileFilter(new FileNameExtensionFilter("JPG Images only", "jpg"));

            // This will represent our loaded file
            File imageFile = null;

            // Select a file
            chooser.showOpenDialog(null);
            imageFile = chooser.getSelectedFile();

            // Check we can do stuff to this file
            System.out.println("Exists: " + imageFile.exists());
            System.out.println("Can Read: " + imageFile.canRead());
            System.out.println("Can Write: " + imageFile.canWrite());
            System.out.println();

            return imageFile;
        }
}

It is overwritten, you just don't detect that visually with your 它已被覆盖,只是您无法通过视觉检测到
eyes. 眼睛。 That is because you change the RGB values by just 1. 这是因为您将RGB值仅更改了1。

for (int x = 0; x < img.getWidth(); x++)
    for (int y = 0; y < img.getHeight(); y++)
        // check if the RGB integer is an odd number
        if (img.getRGB(x, y) % 2 != 0)
            // make it an even number if it is odd (the OCD god demands it!)
            img.setRGB(x, y, 0); /// A ///

Change this line /// A /// as I did and you'll see it is overwritten. 像我一样更改此行/// A /// ,您将看到它被覆盖。

Of course it also depends on the picture you're testing this on. 当然,这还取决于您正在测试的图片。
If img.getRGB(x, y) is never odd, the img.setRGB will never be executed. 如果img.getRGB(x, y)从不为奇数,则将永远不会执行img.setRGB

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM