如何在 java 中创建具有嵌入颜色 table 和像素颜色索引的双层(深度=1)位图文件?

How to create a bi-level (depth=1) bitmap file in java with embedded colour table and pixel colour indices?

我正在关注,http://www.javaworld.com/article/2077561/learn-java/java-tip-60--saving-bitmap-files-in-java.html 通过 java 创建 bmp 文件。
我在谷歌上搜索过如何创建颜色为 table、像素颜色索引为 java 的双层(即 depth =1)图像,但没有太大帮助。
任何有关使用颜色 table、像素颜色索引 java 创建双层图像的帮助都将非常有用!

您提到的文章很旧,可能已经过时了。

您应该能够创建类型为 TYPE_BYTE_BINARYBufferedImageIndexColorModel(包含您选择的颜色),最后使用 ImageIO.write(image, "BMP", file) 编写生成的图像到 BMP 文件。

如果您想了解 BMP 格式的一般知识,WikiPedia article on BMP 是一个很好的资源。

根据 Mr.Harald K 的建议,以下 java 代码创建双层图像并将其写入文件:

package imageprocessing;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.awt.Color;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class WriteBmpFile {

    public static void main(String[] args) {
        SimpleDateFormat timeFormat = new SimpleDateFormat("hh_mm_ss");
        String path = "D:\Project\Images\";
        String onlyBmpFileName = "Img_" + timeFormat.format(new Date());
        String bmpExtension = ".bmp";
        String bmpFilePath = path + onlyBmpFileName + bmpExtension;
        int imgRows = 32;
        int imgCols = 32;
        BufferedImage buffBiLevelImg = new BufferedImage(imgRows, imgCols, BufferedImage.TYPE_BYTE_BINARY);
        for (int r = 0; r <imgRows; r++) {
            for (int c = 0; c < imgCols; c++) {
                if ((r + c) % 2 == 0) {
                    buffBiLevelImg.setRGB(r, c, Color.WHITE.getRGB());
                } else {
                    buffBiLevelImg.setRGB(r, c, Color.BLACK.getRGB());
                }
            }
        }


        try {
            ImageIO.write(buffBiLevelImg, "bmp", new File(bmpFilePath));
        } catch (IOException ioe) {
            System.out.println("Exception Occured While Creating or Writing bitmap image ! and Stack trace is :\n " + ioe);
        }
    }
}