我想将任何文件转换为 PNG 并反转该过程,所有这些都在 Java 中。
我想对图像使用 int-RGB 形式,并将文件中的字节作为 RGB 整数中的字节。这应该会产生一个图像。
我已经通过仅以红色存储字节来实现这一点,但我无法弄清楚如何同时使用绿色和蓝色。
这是我目前使用的代码,它只使用红色,它工作正常:
public static void fileToImage(String sourceFile, String imageFile) throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream(sourceFile));
int size = ((int) Math.sqrt(dis.available())) + 2;
BufferedImage image = new BufferedImage(size,size, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
int red = dis.read(); // I'm using only red
int green = 0; // default
int blue = 0; // default
int rgb = (0xFF << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF);
image.setRGB(x, y, rgb);
}
}
dis.close();
ImageIO.write(image, "png", new File(imageFile));
}
public static void imageToFile(String imageFile, String outputFile) throws IOException {
BufferedImage image = ImageIO.read(new File(imageFile));
DataOutputStream dos = new DataOutputStream(new FileOutputStream(outputFile));
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int rgb = image.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
dos.write(red); // I'm using only red
}
}
dos.close();
}
编辑:好的,所以我修改了代码,这里是:
public static void fileToImage(String sourceFile, String imageFile) throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream(sourceFile));
int size = ((int) Math.sqrt(dis.available())) + 2;
BufferedImage image = new BufferedImage(size,size, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
int red = dis.read();
int green = dis.read();
int blue = dis.read();
int rgb = (0xFF << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF);
image.setRGB(x, y, rgb);
}
}
dis.close();
ImageIO.write(image, "png", new File(imageFile));
}
public static void imageToFile(String imageFile, String outputFile) throws IOException {
BufferedImage image = ImageIO.read(new File(imageFile));
DataOutputStream dos = new DataOutputStream(new FileOutputStream(outputFile));
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int rgb = image.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
dos.write(red);
dos.write(green);
dos.write(blue);
}
}
dos.close();
}
这确实“有效”,但并不完全符合预期。生成的 PNG 中有很多黑色空间,因为我认为图像的“大小”是错误的。因此,在将 PNG 转换回原始文件时,它会变得比原来大得多。
编辑:我现在遇到的问题是:例如,如果我使用 fileToImage 方法将具有以下内容的文本文件转换为 PNG:hello world!然后我用imageToFile把它转回来,输出是:hello world!SSSSSSSSSSSSSSS(S代表“空格”,有15个)
编辑:仍然无法弄清楚这一点。这是我正在使用的:
private static final int NAN = -1;
private static int readByte(DataInputStream dis) throws IOException {
int b;
try {
b = dis.readByte();
} catch (EOFException e) {
b = NAN;
}
return b;
}
public static void fileToImage(String sourceFile, String imageFile) throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream(sourceFile));
int size = ((int) Math.sqrt(dis.available())) + 2;
BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < size; y++) {
boolean finished = false;
for (int x = 0; x < size; x++) {
int alpha = 3;
int red = readByte(dis);
int green = readByte(dis);
int blue = readByte(dis);
if (red == NAN) {
alpha--;
red = 0;
}
if (green == NAN) {
alpha--;
green = 0;
}
if (blue == NAN) {
alpha--;
blue = 0;
}
int rgb = ((alpha & 0xFF) << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF);
image.setRGB(x, y, rgb);
if (alpha < 3) {
finished = true;
break;
}
}
if (finished) break;
}
dis.close();
ImageIO.write(image, "png", new File(imageFile));
}
public static void imageToFile(String imageFile, String outputFile) throws IOException {
BufferedImage image = ImageIO.read(new File(imageFile));
DataOutputStream dos = new DataOutputStream(new FileOutputStream(outputFile));
for (int y = 0; y < image.getHeight(); y++) {
boolean finished = false;
for (int x = 0; x < image.getWidth(); x++) {
int rgb = image.getRGB(x, y);
int alpha = (rgb >> 24) & 0xFF;
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
if (alpha == 0) {
finished = true;
break;
}
if (alpha >= 1) dos.write(red);
if (alpha >= 2) dos.write(green);
if (alpha == 3) dos.write(blue);
}
if (finished) break;
}
dos.close();
}