我需要根据 nio ShortBuffer 中的数据创建灰度图像。我有一个函数,可以将 ShortBuffer 中的数据映射到无符号字节,但采用 int 形式(很容易更改)。我发现的方法使用 RGB 加透明度颜色模型,看起来效率很低。我无法了解如何应用 TYPE_BYTE_GRAY 并修改代码。我是 Java 新手。这是我的代码:
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] = …Run Code Online (Sandbox Code Playgroud)