Esempio n. 1
0
public class Test extends Applet {
  private ColorModel defaultRGB = ColorModel.getRGBdefault();
  private Image image;
  private int imw, imh, pixels[];

  public void init() {
    MediaTracker mt = new MediaTracker(this);

    URL url = getClass().getResource("tiger.gif");

    try {
      image = createImage((ImageProducer) url.getContent());
      mt.addImage(image, 0);
      mt.waitForID(0);
    } catch (Exception e) {
      e.printStackTrace();
    }
    imw = image.getWidth(this);
    imh = image.getWidth(this);
    pixels = new int[imw * imh];

    try {
      PixelGrabber pg = new PixelGrabber(image, 0, 0, imw, imh, pixels, 0, imw);
      pg.grabPixels();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    addMouseMotionListener(
        new MouseMotionAdapter() {
          public void mouseMoved(MouseEvent e) {
            int mx = e.getX(), my = e.getY();

            if (mx > 0 && mx < imw && my > 0 && my < imh) {
              int pixel = ((int[]) pixels)[my * imw + mx];

              int red = defaultRGB.getRed(pixel),
                  green = defaultRGB.getGreen(pixel),
                  blue = defaultRGB.getBlue(pixel),
                  alpha = defaultRGB.getAlpha(pixel);

              showStatus("red=" + red + " green=" + green + " blue=" + blue + " alpha=" + alpha);
            }
          }
        });
  }

  public void paint(Graphics g) {
    Insets insets = getInsets();
    g.drawImage(image, insets.left, insets.top, this);
  }
}
Esempio n. 2
0
    public int[] doContrast(int[] pix, int iw, int ih, double contrast)
    {    
		ColorModel cm = ColorModel.getRGBdefault();
		int r, g, b;
		for(int i = 0; i < iw*ih; i++)	
		{			
			r = (int) (contrast>=0?cm.getRed(pix[i]) + (255-cm.getRed(pix[i]))*contrast:
				cm.getRed(pix[i]) + cm.getRed(pix[i])*contrast);
			g = (int) (contrast>=0?cm.getGreen(pix[i]) + (255-cm.getGreen(pix[i]))*contrast:
				cm.getGreen(pix[i]) + cm.getGreen(pix[i])*contrast);
			b = (int) (contrast>=0?cm.getBlue(pix[i]) + (255-cm.getBlue(pix[i]))*contrast:
				cm.getBlue(pix[i]) + cm.getBlue(pix[i])*contrast);
	
			pix[i] = 255 << 24|r << 16|g << 8|b;
			
		}
		System.out.println("contrast is: " + contrast);
		return pix;
	}	
Esempio n. 3
0
  public void actionPerformed(ActionEvent evt) {
    Graphics g = getGraphics();
    if (evt.getSource() == openItem) {
      JFileChooser chooser = new JFileChooser();
      common.chooseFile(chooser, "./images", 0); // 设置默认目录,过滤文件
      int r = chooser.showOpenDialog(null);

      if (r == JFileChooser.APPROVE_OPTION) {
        String name = chooser.getSelectedFile().getAbsolutePath();

        // 装载图像
        iImage = common.openImage(name, new MediaTracker(this));

        // 取载入图像的宽和高
        iw = iImage.getWidth(null);
        ih = iImage.getHeight(null);
        bImage = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bImage.createGraphics();
        g2.drawImage(iImage, 0, 0, null);
        loadflag = true;
        repaint();
      }
    } else if (evt.getSource() == rotateItem) // 内置旋转
    {
      setTitle("第4章 图像几何变换 内置旋转 作者 孙燮华");
      common.draw(g, iImage, bImage, common.getParam("旋转角(度):", "30"), 0, 0);
    } else if (evt.getSource() == scaleItem) // 内置缩放
    {
      setTitle("第4章 图像几何变换 内置缩放 作者 孙燮华");
      // 参数选择面板
      Parameters pp = new Parameters("参数", "x方向:", "y方向:", "1.5", "1.5");
      setPanel(pp, "内置缩放");
      float x = pp.getPadx();
      float y = pp.getPady();
      common.draw(g, iImage, bImage, x, y, 1);
    } else if (evt.getSource() == shearItem) // 内置错切
    {
      setTitle("第4章 图像几何变换 内置错切 作者 孙燮华");
      Parameters pp = new Parameters("参数", "x方向:", "y方向:", "0.5", "0.5");
      setPanel(pp, "内置错切");
      float x = pp.getPadx();
      float y = pp.getPady();
      common.draw(g, iImage, bImage, x, y, 2);
    } else if (evt.getSource() == transItem) // 内置平移
    {
      setTitle("第4章 图像几何变换 内置平移 作者 孙燮华");
      Parameters pp = new Parameters("参数", "x方向:", "y方向:", "100", "50");
      setPanel(pp, "内置平移");
      float x = pp.getPadx();
      float y = pp.getPady();
      common.draw(g, iImage, bImage, x, y, 3);
    } else if (evt.getSource() == rotItem) // 旋转算法
    {
      setTitle("第4章 图像几何变换 旋转算法 作者 孙燮华");
      pix = common.grabber(iImage, iw, ih);

      // 旋转,输出图像宽高
      int owh = (int) (Math.sqrt(iw * iw + ih * ih + 0.5));
      opix = geom.imRotate(pix, common.getParam("旋转角(度):", "30"), iw, ih, owh);

      // 将数组中的象素产生一个图像
      MemoryImageSource memoryImage =
          new MemoryImageSource(owh, owh, ColorModel.getRGBdefault(), opix, 0, owh);
      oImage = createImage(memoryImage);
      common.draw(g, iImage, oImage, iw, ih, owh, 4);
    } else if (evt.getSource() == mirItem) // 镜象算法(type:5)
    {
      setTitle("第4章 图像几何变换 镜象算法 作者 孙燮华");
      Parameters pp = new Parameters("选择镜象类型", "水平", "垂直");
      setPanel(pp, "镜象算法");

      pix = common.grabber(iImage, iw, ih);
      opix = geom.imMirror(pix, iw, ih, pp.getRadioState());
      ImageProducer ip = new MemoryImageSource(iw, ih, opix, 0, iw);
      oImage = createImage(ip);
      common.draw(g, iImage, oImage, iw, ih, 0, 5);
    } else if (evt.getSource() == shrItem) // 错切算法(type:6)
    {
      setTitle("第4章 图像几何变换 错切算法 作者 孙燮华");
      Parameters pp = new Parameters("参数", "x方向:", "y方向:", "0.5", "0.5");
      setPanel(pp, "错切算法");

      pix = common.grabber(iImage, iw, ih);

      float shx = pp.getPadx();
      float shy = pp.getPady();

      // 计算包围盒的宽和高
      int ow = (int) (iw + (ih - 1) * shx);
      int oh = (int) ((iw - 1) * shy + ih);

      if (shx > 0 && shy > 0) {
        opix = geom.imShear(pix, shx, shy, iw, ih, ow, oh);
        ImageProducer ip = new MemoryImageSource(ow, oh, opix, 0, ow);
        oImage = createImage(ip);
        common.draw(g, iImage, oImage, iw, ih, 0, 6);
      } else JOptionPane.showMessageDialog(null, "参数必须为正数!");
    } else if (evt.getSource() == trnItem) {
      setTitle("第4章 图像几何变换 平移算法 作者 孙燮华");
      Parameters pp = new Parameters("参数", "x方向:", "y方向:", "100", "50");
      setPanel(pp, "平移算法");
      pix = common.grabber(iImage, iw, ih);
      int tx = (int) pp.getPadx();
      int ty = (int) pp.getPady();

      if (tx > 0 && ty > 0) {
        int ow = iw + tx;
        int oh = ih + ty;
        opix = geom.imTrans(pix, tx, ty, iw, ih, ow, oh);
        ImageProducer ip = new MemoryImageSource(ow, oh, opix, 0, ow);
        oImage = createImage(ip);
        common.draw(g, iImage, oImage, iw, ih, 0, 7);
      } else JOptionPane.showMessageDialog(null, "参数必须为正数!");
    } else if (evt.getSource() == nearItem) {
      setTitle("第4章 图像几何变换 最邻近插值算法 作者 孙燮华");
      pix = common.grabber(iImage, iw, ih);

      float p =
          (Float.valueOf(JOptionPane.showInputDialog(null, "输入缩放参数(0.1-3.0)", "1.50")))
              .floatValue();
      int ow = (int) (p * iw); // 计算目标图宽高
      int oh = (int) (p * ih);
      opix = geom.nearNeighbor(pix, iw, ih, ow, oh, p);
      ImageProducer ip = new MemoryImageSource(ow, oh, opix, 0, ow);
      oImage = createImage(ip);
      common.draw(g, oImage, "最邻近插值", p);
    } else if (evt.getSource() == linrItem) {
      setTitle("第4章 图像几何变换 双线性插值算法 作者 孙燮华");
      pix = common.grabber(iImage, iw, ih);

      float p =
          (Float.valueOf(JOptionPane.showInputDialog(null, "输入缩放参数(0.1-3.0)", "1.50")))
              .floatValue();
      int ow = (int) (p * iw); // 计算目标图宽高
      int oh = (int) (p * ih);
      opix = geom.bilinear(pix, iw, ih, ow, oh, p);
      ImageProducer ip = new MemoryImageSource(ow, oh, opix, 0, ow);
      oImage = createImage(ip);
      common.draw(g, oImage, "双线性插值", p);
    } else if (evt.getSource() == cubicItem) {
      setTitle("第4章 图像几何变换 三次卷积插值算法 作者 孙燮华");
      pix = common.grabber(iImage, iw, ih);

      float p =
          (Float.valueOf(JOptionPane.showInputDialog(null, "输入缩放参数(1.1-3.0)", "1.50")))
              .floatValue();
      if (p < 1) {
        JOptionPane.showMessageDialog(null, "参数p必须大于1!");
        return;
      }
      int ow = (int) (p * iw); // 计算目标图宽高
      int oh = (int) (p * ih);
      opix = geom.scale(pix, iw, ih, ow, oh, p, p);
      ImageProducer ip = new MemoryImageSource(ow, oh, opix, 0, ow);
      oImage = createImage(ip);
      common.draw(g, oImage, "三次卷积插值", p);
    } else if (evt.getSource() == okButton) dialog.dispose();
    else if (evt.getSource() == exitItem) System.exit(0);
  }