使用SWT通过像素在画布像素上显示图像

时间:2016-06-03 09:30:03

标签: java canvas bitmap swt

我正在尝试通过Pixel读取BitMap像素(尚未完成),然后在SWT中的Canvas上显示每个Pixel。

这是显示像素的正确方法吗?

以下方法似乎有效,但我想知道Java SWT中是否有更好的选项。我的输入将是很多ARGB值,我想知道这是否可以扩展。

public class ReadyBitMapDisplayAsPixels {
BufferedImage bitmapImage = null;

      private void createContents(Shell shell) {

            try {
                bitmapImage = ImageIO.read(new File("c:\\BMP\\Earth.bmp"));
            } catch (IOException e) {
            }

          shell.setLayout(new FillLayout());

        // Create a canvas to draw on
        Canvas canvas = new Canvas(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.NO_REDRAW_RESIZE | SWT.DOUBLE_BUFFERED);

        // Add a handler to do the drawing
        canvas.addPaintListener(new PaintListener() {
          public void paintControl(PaintEvent e) {
            // Get the canvas and its size
            Canvas canvas = (Canvas) e.widget;

            int height = bitmapImage.getHeight();
            int width = bitmapImage.getWidth();

            PaletteData palette=new PaletteData(0xff0000,0x00ff00,0x0000ff);
            ImageData imageData = new ImageData(width, height,24,palette);

            for (int h = 0; h<height; h++)
            {
                for (int w = 0; w<width; w++)
                {
                    int pixel = bitmapImage.getRGB(w, h);
                    //int alpha = (pixel >> 24) & 0xff;
                    int red = (pixel >> 16) & 0xff;
                    int green = (pixel >> 8) & 0xff;
                    int blue = (pixel) & 0xff;
                    imageData.setPixel(h, w, pixel);    
                }
            }

            //imageData = imageData.scaledTo(600, 500);
            Image image = ImageDescriptor.createFromImageData(imageData).createImage();
            e.gc.drawImage(image, 0, 0);
          }
        });
      }
      public static void main(String[] args) 
        {
            new ReadyBitMapDisplayAsPixels().run();
        }

         public void run() {
                Display display = new Display();
                Shell shell = new Shell(display);
                createContents(shell);
                shell.open();
                while (!shell.isDisposed()) {
                  if (!display.readAndDispatch()) {
                    display.sleep();
                  }
                }
                display.dispose();
              }

              /**
               * Creates the main window's contents
               * 
               * @param shell the main window
               */            

}

0 个答案:

没有答案