0
我做了下面的例子,我找不出它为什么不起作用。jpg没有被ImageIO.write覆盖()
- 我的检查都不是返回false,
- 我有读写权限,
- 的ImageIO的响应,它实际上已经覆盖图像,
- 没有异常抛出...
但图像在磁盘上仍然没有改变。
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;
}
}
因此,我将该行设置为/// img.setRGB(x,y,0); ///虽然它修改了图像,但它确实是奇怪的东西,而不是将其设置为纯色,基本图像有点显示,但主要是静态丢失,究竟发生了什么?这就像它试图将原始图像叠加到某种白色和黑色的静态背景上......这是否与jpg压缩有关? – Troyseph
我不知道你想要做什么,但你的图像被覆盖。这是你的问题。 –
上面的例子真的是愚蠢的代码,我玩弄图像隐藏数据,奇数像素代表'1',甚至像素代表'0',所以理想情况下我想像素,我改变由一个值保持改变。我的问题是,如果你运行原始程序,并使所有的像素,甚至,然后重新检查该图像,它仍然有奇怪的像素... – Troyseph