如何在JAVA中得到4位16色的BMP

问题:

      使用BufferedImage保存处理的图片,是24位的,发送到LED显示屏上显示效果很差,需要调整为16色再下发。

分析:

     调整BufferedImage构造函数中的RGB类型,依旧无果;原因在于缺少调色板参数

方法:

    在构造BufferedImage前,先设置好调色板信息。

   具体代码如下:

// 图片大小
final int img_width = 96;
final int img_height = 32;

//16色BMP调色板
final int img_bits = 4; 
final byte[] colorTable_16 = {
    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
    (byte) 0x00, (byte) 0x00, (byte) 0x80, (byte) 0x00,
    (byte) 0x00, (byte) 0x80, (byte) 0x00, (byte) 0x00,
    (byte) 0x00, (byte) 0x80, (byte) 0x80, (byte) 0x00,
    (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00,
    (byte) 0x80, (byte) 0x00, (byte) 0x80, (byte) 0x00,
    (byte) 0x80, (byte) 0x80, (byte) 0x00, (byte) 0x00,
    (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x00,
    (byte) 0xc0, (byte) 0xc0, (byte) 0xc0, (byte) 0x00,
    (byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0x00,
    (byte) 0x00, (byte) 0xff, (byte) 0x00, (byte) 0x00,
    (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x00,
    (byte) 0xff, (byte) 0x00, (byte) 0x00, (byte) 0x00,
    (byte) 0xff, (byte) 0x00, (byte) 0xff, (byte) 0x00,
    (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0x00,
    (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00
};
final IndexColorModel color_model = new IndexColorModel(img_bits
    , colorTable_16.length / img_bits, colorTable_16
    , 0, true);

// 构造图片对象
BufferedImage image = new BufferedImage(img_width, img_height, BufferedImage.TYPE_BYTE_BINARY, color_model);
// 获取Graphics2D对象,随后在其上绘画
Graphics2D graphics = image.createGraphics();
....

// 输出到文件

ImageIO.write(image, "bmp", new File(...));

    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值