任务是从 int RGB 矩阵生成 BMP 文件,然后从 BMP 图像读取该矩阵。
我已经使用此类来生成 BMP 文件,但似乎很难逆转这种从图像获取 rgbValues
数组的方法:
/**
* Class for creating bmp images from the pixel matrix (an int matrix).
* @author eafkuor
* @link http://forum.codecall.net/topic/62457-creating-a-bmp-image-in-java/
*/
import java.io.File;
import java.io.FileOutputStream;
public class BMP {
private final static int BMP_CODE = 19778;
byte[] bytes;
public void saveBMP(String filename, int[][] rgbValues) {
try {
FileOutputStream fos = new FileOutputStream(new File(filename));
bytes = new byte[54 + 3 * rgbValues.length * rgbValues[0].length + getPadding(rgbValues[0].length) * rgbValues.length];
saveFileHeader();
saveInfoHeader(rgbValues.length, rgbValues[0].length);
saveRgbQuad();
saveBitmapData(rgbValues);
fos.write(bytes);
fos.close();
} catch (Exception ignored) {}
}
private void saveFileHeader() {
byte[] a = intToByteCouple(BMP_CODE);
bytes[0] = a[1];
bytes[1] = a[0];
a = intToFourBytes(bytes.length);
bytes[5] = a[0];
bytes[4] = a[1];
bytes[3] = a[2];
bytes[2] = a[3];
//data offset
bytes[10] = 54;
}
private void saveInfoHeader(int height, int width) {
bytes[14] = 40;
byte[] a = intToFourBytes(width);
bytes[22] = a[3];
bytes[23] = a[2];
bytes[24] = a[1];
bytes[25] = a[0];
a = intToFourBytes(height);
bytes[18] = a[3];
bytes[19] = a[2];
bytes[20] = a[1];
bytes[21] = a[0];
bytes[26] = 1;
bytes[28] = 24;
}
private void saveRgbQuad() {
}
private void saveBitmapData(int[][] rgbValues) {
int i;
for (i = 0; i < rgbValues.length; i++) {
writeLine(i, rgbValues);
}
}
private void writeLine(int row, int[][] rgbValues) {
final int offset = 54;
final int rowLength = rgbValues[row].length;
final int padding = getPadding(rgbValues[0].length);
int i;
for (i = 0; i < rowLength; i++) {
int rgb = rgbValues[row][i];
int temp = offset + 3 * (i + rowLength * row) + row * padding;
bytes[temp] = (byte) (rgb >> 16);
bytes[temp + 1] = (byte) (rgb >> 8);
bytes[temp + 2] = (byte) rgb;
}
i--;
int temp = offset + 3 * (i + rowLength * row) + row * padding + 3;
for (int j = 0; j < padding; j++)
bytes[temp + j] = 0;
}
private byte[] intToByteCouple(int x) {
byte[] array = new byte[2];
array[1] = (byte) x;
array[0] = (byte) (x >> 8);
return array;
}
private byte[] intToFourBytes(int x) {
byte[] array = new byte[4];
array[3] = (byte) x;
array[2] = (byte) (x >> 8);
array[1] = (byte) (x >> 16);
array[0] = (byte) (x >> 24);
return array;
}
private int getPadding(int rowLength) {
int padding = (3 * rowLength) % 4;
if (padding != 0)
padding = 4 - padding;
return padding;
}
}
有没有java库可以制作这样的东西?或者也许有人可以建议如何反转 BMP 文件?
最佳答案
在我看来,如果任务很容易实现并且您可以更好地了解幕后发生的事情,那么使用 API 是一件懒惰的事情。在您自己至少完成一次之后,您就可以放心地使用 API。最后,您应该使用 API,因为工作代码可能比您自己的版本更强大且使用安全。
至少在理论上,反转图像是一件非常简单的事情。该过程包括两个步骤:
- 将图像的颜色矩阵放入内存
- 创建一个新图像,以便从内存中的图像(仅从右侧开始)中选择从左侧开始的每个像素。
举例说明:
假设我们有一个宽度为 4px、高度为 4px 的图像。 0 表示像素为黑色,1 表示像素为白色。
因此,实际上我们有一个 4x4 二进制矩阵。让我们将其编写如下:
[1][0]
[1][0]
要反转它,我们只需将数组复制到一个新的 4x4 矩阵中,但我们从右侧开始将值复制到新矩阵中:
[1][0] => [0][1]
[1][0] => [0][1]
现在我们有了反转的图像:
[0][1]
[0][1]
这就是想法。
在 RGB 环境中,原始图像的矩阵将如下所示:
[rgb(255, 255, 255)][rgb(0, 0, 0)]
[rgb(255, 255, 255)][rgb(0, 0, 0)]
现在去了解如何提取每个像素的颜色并将其写入新的像素。这是一项简单的任务。
关于java从像素矩阵创建BMP图像并反转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26595835/