简体   繁体   English

Java / SWT显示字节数组中的8位灰度图像

[英]Java/SWT displaying an 8bit grayscaling Image from a byte array

I'm building a GUI with the Eclipse Window Builder Editor. 我正在使用Eclipse Window Builder编辑器构建GUI。 I've made a Composite with a Gridlayout and have put a Label in there. 我用Gridlayout制作了Composite,并在其中放置了Label。 Some method from another class generates a raw 8 bit 1280x1024 grayscale image which is saved in a byte array. 另一个类中的某些方法生成原始的8位1280x1024灰度图像,该图像保存在字节数组中。 This is how I generate my ImageData, etc in the mainMethod : 这就是我在mainMethod中生成ImageData等的方式:

Color white = display.getSystemColor(SWT.COLOR_WHITE);
Color black = display.getSystemColor(SWT.COLOR_BLACK);

PaletteData palette = new PaletteData( 255, 255, 255 );

imgData = new ImageData(1280, 1024, 8, palette);

imgLabel = new Label(composite_3, SWT.NONE);
imgLabel.setImage(null);
imgLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

I'm still having problems to understand what exactly I should do with the PaletteData as I don't need colours anyway. 我仍然很难理解我应该对PaletteData做什么,因为我仍然不需要颜色。 (Color)Depth should be 8 because I want it to have 255 grayvalues? (Color)Depth应该为8,因为我希望它具有255个灰度值?

And this is my setImg method which gets called from another Thread as soon as the byte[] is filled with data: 这是我的setImg方法,当byte []充满数据后立即从另一个线程调用该方法:

public static void setImg(final byte[] b) {
    Display.getDefault().syncExec( new Runnable() {
        @Override
        public void run() {
            imgData.setPixels(0, 0, 1280, b, 0);
            img = new Image(Display.getCurrent(), imgData);
            Image grayImg = new Image(Display.getCurrent(), img, SWT.IMAGE_GRAY);
            imgLabel.setImage(img);

//              img.dispose();
        }
    });
}

I've tried 我试过了

imgLabel.setImage(img);

and

imgLabel.setImage(grayImg);

Ok the first line now displays correctly... It seems that I only can set 1 line of pixels with imgData.setPixels(...) which is kind of useless imo or am I doing something else wrong? 好了,第一行现在可以正确显示了……看来我只能用imgData.setPixels(...)设置一行像素,这是一种无用的imo,还是我做错了其他事情?

Your PaletteData uses the constructor for a direct palette. 您的PaletteData使用构造函数创建直接调色板。 I guess that what You want is the constructor for an indexed palette. 我想您想要的是索引调色板的构造函数。 See also here and here for more explanation. 另请参见此处此处以获取更多说明。

The following code snippet shows 以下代码段显示

  • how to create a grey scale palette using an indexed palette 如何使用索引调色板创建灰度调色板
  • how to set the image as a whole (not line by line) 如何设置整个图像(不是逐行)

     // pixel depth will be 8-bit, so there is space for 256 different (grey) values RGB[] rgb = new RGB[256]; // build grey scale palette: 256 different grey values are generated. for (int i = 0; i < 256; i++) { rgb[i] = new RGB(i, i, i); } // Construct a new indexed palette given an array of RGB values. PaletteData paletteData = new PaletteData(rgb); // create an image with given dimensions, depth and color palette ImageData imageData = new ImageData(1280, 1024, 8, paletteData); // data represents the pixels of the image. This corresponds to 'b' in Your code imageData.data = b; // note that there are also other 'Image', eg java.awt.Image org.eclipse.swt.graphics.Image image = new Image(Display.getCurrent(), imageData); // paint imgLabel.setImage(img); // release operating system resources corresponding to the image image.dispose(); 

NOTE: I tried an example with Your PaletteData, eg 注意:我用Your PaletteData尝试了一个示例,例如

    PaletteData palette = new PaletteData( 255, 255, 255 );

and it worked in the sense that it showed some grey scale pictures (a video actually). 并从某种意义上讲它显示了一些灰度图片(实际上是一个视频)。 However, the indexed version used in given code snippet was much more eye friendly. 但是,给定代码段中使用的索引版本对眼睛更友好。 Have a look at the first link provided above. 看看上面提供的第一个链接。 It gives a good explanation of the difference between indexed and direct color palette. 它很好地解释了索引调色板和直接调色板之间的区别。

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

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