在 Java 中将 File .bmp 转换为二进制文件并将二进制文件转换回 File .bmp
Convert File .bmp to binary and binary back to File .bmp in Java
我正在尝试将图像 (.bmp) 转换为二进制代码,然后对其进行处理,然后再将其转换回来。让我们把处理部分放在一边,只关注转换。
我现在设法将图像转换为二进制文件:
// Image to byte[]
File file = new File("image.bmp");
BufferedImage bufferedImage = ImageIO.read(file);
WritableRaster raster = bufferedImage.getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
// byte[] to binary String
String string = "";
for (byte b : data.getData()) {
String substring = Integer.toBinaryString((b & 0xFF) + 0x100).substring(1);
string = string.concat(substring);
}
// Binary string to binary LinkedList<Ingeger> - this part is optional
LinkedList<Integer> vector = new LinkedList<>();
for (char c : stringVector.toCharArray()) {
if (c == '0' || c == '1') {
vector.add((int) c - 48);
} else {
System.out.println("Incorrect value.");
break;
}
}
此时我正在将文件 (.bmp) 转换为二进制向量。不知道对不对。
另一个问题是将其转换回 .bmp 文件。
我需要将我的二进制向量(或字符串)转换为 byte[]
并返回 File
或图像。
我相信最后一步应该是这样的:
FileOutputStream fileOutputStream = new FileOutputStream("output.bmp");
fileOutputStream.write(byteArrayFromBinary.getBytes());
有人能帮我弄清楚吗?因为我不确定这到底有什么问题。如有任何建议,我们将不胜感激。
所以经过大量的研究终于想出了一个解决方案。
要将 .bmp 格式的 BufferedImage 转换为二进制格式:
public String imageToVector(BufferedImage image) throws Exception {
String vectorInString = "";
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
Color color = new Color(image.getRGB(x, y), false);
vectorInString = vectorInString.concat(decimalToBinary(color.getRed()));
vectorInString = vectorInString.concat(decimalToBinary(color.getGreen()));
vectorInString = vectorInString.concat(decimalToBinary(color.getBlue()));
}
}
return vectorInString;
}
要从二进制向量转换回具有 .bmp 格式的 BufferedImage:
String[] splittedString = vectorInString.split("(?<=\G.{8})");
int i = 0;
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
int red = Integer.parseInt(splittedString[i], 2);
i++;
int green = Integer.parseInt(splittedString[i], 2);
i++;
int blue = Integer.parseInt(splittedString[i], 2);
i++;
Color color = new Color(red, green, blue);
image.setRGB(x, y, color.getRGB());
}
}
如果您有任何其他问题,请提出,我可以解释为什么我需要这个解决方案。
我正在尝试将图像 (.bmp) 转换为二进制代码,然后对其进行处理,然后再将其转换回来。让我们把处理部分放在一边,只关注转换。
我现在设法将图像转换为二进制文件:
// Image to byte[]
File file = new File("image.bmp");
BufferedImage bufferedImage = ImageIO.read(file);
WritableRaster raster = bufferedImage.getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
// byte[] to binary String
String string = "";
for (byte b : data.getData()) {
String substring = Integer.toBinaryString((b & 0xFF) + 0x100).substring(1);
string = string.concat(substring);
}
// Binary string to binary LinkedList<Ingeger> - this part is optional
LinkedList<Integer> vector = new LinkedList<>();
for (char c : stringVector.toCharArray()) {
if (c == '0' || c == '1') {
vector.add((int) c - 48);
} else {
System.out.println("Incorrect value.");
break;
}
}
此时我正在将文件 (.bmp) 转换为二进制向量。不知道对不对。
另一个问题是将其转换回 .bmp 文件。
我需要将我的二进制向量(或字符串)转换为 byte[]
并返回 File
或图像。
我相信最后一步应该是这样的:
FileOutputStream fileOutputStream = new FileOutputStream("output.bmp");
fileOutputStream.write(byteArrayFromBinary.getBytes());
有人能帮我弄清楚吗?因为我不确定这到底有什么问题。如有任何建议,我们将不胜感激。
所以经过大量的研究终于想出了一个解决方案。
要将 .bmp 格式的 BufferedImage 转换为二进制格式:
public String imageToVector(BufferedImage image) throws Exception {
String vectorInString = "";
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
Color color = new Color(image.getRGB(x, y), false);
vectorInString = vectorInString.concat(decimalToBinary(color.getRed()));
vectorInString = vectorInString.concat(decimalToBinary(color.getGreen()));
vectorInString = vectorInString.concat(decimalToBinary(color.getBlue()));
}
}
return vectorInString;
}
要从二进制向量转换回具有 .bmp 格式的 BufferedImage:
String[] splittedString = vectorInString.split("(?<=\G.{8})");
int i = 0;
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
int red = Integer.parseInt(splittedString[i], 2);
i++;
int green = Integer.parseInt(splittedString[i], 2);
i++;
int blue = Integer.parseInt(splittedString[i], 2);
i++;
Color color = new Color(red, green, blue);
image.setRGB(x, y, color.getRGB());
}
}
如果您有任何其他问题,请提出,我可以解释为什么我需要这个解决方案。