Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 Nate

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...