如何使用TYPE_BYTE_GRAY有效地创建使用AWT灰度的BufferedImage [英] How to use TYPE_BYTE_GRAY to efficiently create a grayscale bufferedimage using AWT

查看:621
本文介绍了如何使用TYPE_BYTE_GRAY有效地创建使用AWT灰度的BufferedImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在NIO ShortBuffer创建从数据的灰度图像。我有一个在ShortBuffer的数据映射到无符号字节,但在一个int的函数(容易改变)。我发现的方法使用RGB加上透明颜色模式,似乎是相当inefficent。我一直没能看到如何应用TYPE_BYTE_GRAY和修改code。我是新来的Java。这里是我的code:

I need to create a grayscale image from data in an nio ShortBuffer. I have a function that maps the data in the ShortBuffer to unsigned byte but is in an int (easily changed). The method I found uses an RGB plus transparency color model and appears to be quite inefficent. i have not been able to see how to apply the TYPE_BYTE_GRAY and modify the code. i'm new to Java. Here's my code:

    public void paintComponent(Graphics g) {
    final BufferedImage image;
    int[] iArray = {0, 0, 0, 255};  //  pixel

    image = (BufferedImage) createImage(WIDTH, HEIGHT);

    WritableRaster raster = image.getRaster();
    sBuf.rewind();  // nio ShortBuffer
    for (int row = 0; row < HEIGHT; row++) {
        for (int col = 0; col < WIDTH; col++) {
            int v = stats.mapPix(sBuf.get());  // map short to byte
            iArray[0] = v;  // RGBT
            iArray[1] = v;  
            iArray[2] = v;
            raster.setPixel(col, row, iArray);
        }
    }
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}

TIA
内特

TIA Nate

推荐答案

一个办法是创造的 的BufferedImage 通过书面形式向光栅,你现在所做的。一旦你的 的BufferedImage ,你可以用它转换为 TYPE_BYTE_GRAY 的<一个href=\"http://java.sun.com/javase/6/docs/api/java/awt/image/ColorConvertOp.html#filter%28java.awt.image.BufferedImage,%20java.awt.image.BufferedImage%29\"相对=nofollow> 过滤器() 的ColorConvertOp ,如本的例如

One approach would be to create the BufferedImage by writing to the raster as you are doing now. Once you have the BufferedImage, you can convert it to TYPE_BYTE_GRAY using the filter() method of ColorConvertOp, as shown in this example.

这篇关于如何使用TYPE_BYTE_GRAY有效地创建使用AWT灰度的BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆