Package swt

Source Code of swt.ImageGetBrightness

package swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class ImageGetBrightness implements PaintListener {
  Display display = new Display();
  Shell shell = new Shell(display);
  String imageFile;
  Image image;
  Canvas canvas = new Canvas(shell, SWT.NONE);
  Label statusLabel = new Label(shell, SWT.BORDER);

  public static void main(String[] args) {
    new ImageGetBrightness();
  }

  public ImageGetBrightness() {
    shell.setText("Image Brightness");
    GridLayout gridLayout = new GridLayout(1, true);
    gridLayout.horizontalSpacing = 0;
    gridLayout.verticalSpacing = 0;
    gridLayout.marginHeight = 0;
    gridLayout.marginWidth = 0;
    shell.setLayout(gridLayout);

    GridData labelGridData = new GridData();
    labelGridData.grabExcessHorizontalSpace = true;
    labelGridData.horizontalAlignment = SWT.FILL;
    labelGridData.verticalAlignment = SWT.FILL;
    statusLabel.setLayoutData(labelGridData);

    GridData imgGridData = new GridData();
    imgGridData.grabExcessHorizontalSpace = true;
    imgGridData.grabExcessVerticalSpace = true;
    imgGridData.horizontalAlignment = SWT.FILL;
    imgGridData.verticalAlignment = SWT.FILL;
    canvas.setLayoutData(imgGridData);

    canvas.addPaintListener(this);
   
    canvas.addMouseMoveListener(new MouseMoveListener() {
      @Override
      public void mouseMove(MouseEvent e) {
        if (image != null && image.getBounds().contains(e.x, e.y)) {
          RGB rgb = image.getImageData().palette.getRGB(image
              .getImageData().getPixel(e.x, e.y));
          float brightness = rgb.getHSB()[2];
          statusLabel.setText("brightness@" + e.x + "/" + e.y + "\t="
              + brightness);
        } else {
          statusLabel.setText("Au�erhalb des Bildes. Per Mausklick anderes Bild w�hlen");
        }
      }
    });

    canvas.addMouseListener(new MouseListener() {
      @Override
      public void mouseUp(MouseEvent arg0) {
      }

      @Override
      public void mouseDown(MouseEvent arg0) {
        if (arg0.button == 1) {
          chooseImage();
        }
      }

      @Override
      public void mouseDoubleClick(MouseEvent arg0) {
      }
    });

    shell.setSize(300, 200);
    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }

  private void chooseImage() {
    FileDialog fileDialog = new FileDialog(Display.getDefault()
        .getActiveShell());
    fileDialog.setText("Choose a Picture File...");
    String[] extensions = new String[] { "*.jpg; *.bmp; *.jpeg;"
        + " *.gif; *.png" };
    fileDialog.setFilterExtensions(extensions);
    imageFile = fileDialog.open();
    if (imageFile != null) {
      image = new Image(display, imageFile);
      canvas.redraw();
      shell.setSize(image.getBounds().width, image.getBounds().height + 50);
    }
  }

  @Override
  public void paintControl(PaintEvent e) {
    if (image != null)
      e.gc.drawImage(image, 0, 0);
  }
}
TOP

Related Classes of swt.ImageGetBrightness

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.