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