简体   繁体   English

如何在java中将8位灰度位图转换为4位?

[英]how to convert a 8 bit gray scale bitmap to 4 bit scale in java?

I want to read an gray level image file, for example, lena.bmp, then reduce the gray level resolution in java, I used PixelGrabber to get the pixels data of this image, how to convert this 8 bit gray scale bitmap to 4 bit? 我想读一个灰度图像文件,例如lena.bmp,然后降低java中的灰度级分辨率,我用PixelGrabber来获取该图像的像素数据,如何将这个8位灰度位图转换为4位? Thanks! 谢谢!

Not shure if they can help you here, but maybe you can take a look at JAI (Java Advanced Imaging) , Apache Commons Sanselan or Image4j . 如果他们可以在这里帮助你,那不可能,但也许你可以看看JAI(Java高级成像)Apache Commons SanselanImage4j

Edit: 编辑:

Just tested the Image4J Library: 刚刚测试了Image4J库:

Converting 8 Bit -> 4 Bit is very simple: 转换8位 - > 4位非常简单:

final File inFile = new File("test.bmp");
final File outFile = new File("test2.bmp");

BufferedImage inImage = ImageIO.read(inFile);
BufferedImage outImage = ConvertUtil.convert4(inImage); // Converts to 4 Bit

ImageIO.write(outImage, "bmp", outFile);

See Image4J - ConvertUtil Documentation for more Informations. 有关更多信息,请参见Image4J - ConvertUtil文档

This does reduce the number of bits to 4: 这确实将位数减少到4:

public static void main(String[] args) throws Exception {
    BufferedImage in = ImageIO.read(new File(args[0]));
    int w = in.getWidth(), h = in.getHeight();
    int[] bits = { 4 };
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    int dt = DataBuffer.TYPE_BYTE;
    ColorModel cm = new ComponentColorModel
        (cs, bits, false, false, Transparency.OPAQUE, dt);
    WritableRaster wr = cm.createCompatibleWritableRaster(w, h);
    BufferedImage out = new BufferedImage(cm, wr, false, null);
    Graphics2D g = out.createGraphics();
    g.drawImage(in, 0, 0, null);
    g.dispose();
    ImageIO.write(out, "png", new File(args[1]));
}

The resulting file will look too dark on all viewer applications I've tried so far. 到目前为止,我尝试过的所有查看器应用程序的结果文件看起来都太暗了。 But if you're only interested in the bits, then perhaps operating on the wr raster after the above transformation is good enough for you. 但是,如果你只对这些位感兴趣,那么在上面的转换对你来说,或许可以在wr栅格上运行。

If not, then perhaps you should set up an IndexedColorModel containing the 2 4 gray levels you want. 如果没有,那么也许你应该设置一个包含你想要的2 4个灰度级的IndexedColorModel You can simply multiply the index by 17 to obtain evenly spaced intensities, from 0x0 * 17 = 0x00 through 0xf * 17 = 0xff . 您可以简单地将索引乘以17以获得均匀间隔的强度,从0x0 * 17 = 0x000xf * 17 = 0xff

public static void main(String[] args) throws Exception {
    BufferedImage in = ImageIO.read(new File(args[0]));
    int w = in.getWidth(), h = in.getHeight();
    byte[] v = new byte[1 << 4];
    for (int i = 0; i < v.length; ++i)
        v[i] = (byte)(i*17);
    ColorModel cm = new IndexColorModel(4, v.length, v, v, v);
    WritableRaster wr = cm.createCompatibleWritableRaster(w, h);
    BufferedImage out = new BufferedImage(cm, wr, false, null);
    Graphics2D g = out.createGraphics();
    g.drawImage(in, 0, 0, null);
    g.dispose();
    ImageIO.write(out, "png", new File(args[1]));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM