我想將任何文件轉換爲PNG,並且還要將所有這些過程都轉換爲Java。在Java中將任何文件轉換爲PNG
我想爲圖像使用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!SSSSSSSSSSSSSSSSS(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();
}
「的任何文件」分割文件的大小,或者「任何形象」? –
我想將任何文件(EXE,JAR等)轉換爲PNG。我也想把它轉換回來。 –
什麼*確切*是問題?你不能弄清楚什麼? –