⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagegray.java

📁 java编写的程序
💻 JAVA
字号:

import java.awt.*;
import java.applet.*;
import java.awt.image.*;
import java.awt.event.*;
/**
 * This class reads PARAM tags from its HTML host page and sets
 * the color and label properties of the applet. Program execution
 * begins with the init() method. 
 */
public class ImageGray extends Applet implements MouseListener
{
	//Java窗口的尺寸
	int windowWidth,windowHeight;
	
	//imageTest:原始图片;被重新亮度化的图片
	Image imageTest,imageCurrent;

	//双缓冲
	Image offScreenImage;
	Graphics offScreen;

	//图像的尺寸
	int imageWidth,imageHeight;
	
	//图像是否被恢复
	boolean bRestore = true;
	
	//用于存储像素数据的数组
	int pixelsSource[];
	int pixelsDestnation[];
	
	//内存图像管理
	MemoryImageSource memoryImage;
	
	FrameManager frameManager;
	GrayDialog gd;
	
	//亮度修正比
	float fBrightness = 1.0f;
	int lower_valve = 0;
	int upper_valve = 255;

	float param_a = 1.0f;
	int whichMethod = 0;
	
	public void init()
	{
		initForm();

		usePageParams();
		
		initParameters();
	}

	private	final String labelParam = "label";
	private	final String backgroundParam = "background";
	private	final String foregroundParam = "foreground";

	/**
	 * Reads params from the applet's HTML host and sets applet
	 * properties.
	 */
	private void usePageParams()
	{
		final String defaultLabel = "Default label";
		final String defaultBackground = "C0C0C0";
		final String defaultForeground = "000000";
		String labelValue;
		String backgroundValue;
		String foregroundValue;

		labelValue = getParameter(labelParam);
		backgroundValue = getParameter(backgroundParam);
		foregroundValue = getParameter(foregroundParam);

		if ((labelValue == null) || (backgroundValue == null) ||
			(foregroundValue == null))
		{
			/**
			 * There was something wrong with the HTML host tags.
			 * Generate default values.
			 */
			labelValue = defaultLabel;
			backgroundValue = defaultBackground;
			foregroundValue = defaultForeground;
		}

		/**
		 * Set the applet's string label, background color, and
		 * foreground colors.
		 */
		label1.setText(labelValue);
		label1.setBackground(stringToColor(backgroundValue));
		label1.setForeground(stringToColor(foregroundValue));
		this.setBackground(stringToColor(backgroundValue));
		this.setForeground(stringToColor(foregroundValue));
	}

	/**
	 * Converts a string formatted as "rrggbb" to an awt.Color object
	 */
	private Color stringToColor(String paramValue)
	{
		int red;
		int green;
		int blue;

		red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue();
		green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue();
		blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue();

		return new Color(red,green,blue);
	}

	/**
	 * External interface used by design tools to show properties of an applet.
	 */
	public String[][] getParameterInfo()
	{
		String[][] info =
		{
			{ labelParam, "String", "Label string to be displayed" },
			{ backgroundParam, "String", "Background color, format \"rrggbb\"" },
			{ foregroundParam, "String", "Foreground color, format \"rrggbb\"" },
		};
		return info;
	}

	Label label1 = new Label();

	/**
	 * Intializes values for the applet and its components
	 */
	void initForm()
	{
		this.setBackground(Color.lightGray);
		this.setForeground(Color.black);
		label1.setText("label1");
		this.setLayout(new BorderLayout());
		this.add("North",label1);
	}

	public void initParameters()
	{
		windowWidth = Integer.parseInt(getParameter("width"));
		windowHeight = Integer.parseInt(getParameter("height"));
		
		//装载图像并获取图像参数
		imageTest = loadingImage( "seashore.bmp", "Loading image one..." );
		offScreenImage = createImage( windowWidth, windowHeight );
		offScreen = offScreenImage.getGraphics();
		
		imageWidth = imageTest.getWidth(this);
		imageHeight = imageTest.getHeight(this);
		
		//创建存储像素数据的数组
		pixelsSource = new int[imageWidth * imageHeight];
		pixelsDestnation = new int[imageWidth * imageHeight];
		
		//创建像素抓取器
		PixelGrabber  pixelGrabber = new PixelGrabber(imageTest,0,0,imageWidth,imageHeight,pixelsSource,0,imageWidth);
		 
		try
		{
			pixelGrabber.grabPixels ();
		}
		catch(InterruptedException e)
		{}
		 
		//创建内存图像管理器
		memoryImage  = new MemoryImageSource(imageWidth,imageHeight,ColorModel.getRGBdefault(),pixelsDestnation,0,imageWidth);
		
		//创建当前图像,它是经像素修复的图像
		imageCurrent = createImage(memoryImage);
		
		
		this.addMouseListener(this);
		
		//frameManager只作为服务者出现,而不必给它显示的机会,要显示的将是对话框
		frameManager = new FrameManager("Data Frame");
		gd = new GrayDialog(frameManager,"Gray: ",true);
		
		//第一次绘制
		draw_groupBox_border(offScreen);
		
	 
	}
	
	public Image loadingImage(String imageFile,String status)
	{
		Image image;
		MediaTracker mediaTracker  = new MediaTracker(this);
		
		showStatus(status);
		
		image = getImage(getCodeBase(),imageFile);
		mediaTracker .addImage(image,0);
		try
		{
			mediaTracker .waitForID(0);
		}
		catch(InterruptedException e){}
		
		showStatus("");
		return (image);
	}

	public void paint(Graphics g)
	{
				
		if(bRestore)
		{
			offScreen.drawImage(this.imageTest, 15, 32, this );
			g.drawImage( offScreenImage, 0, 0, this );
		}
		//鼠标事件已经发生
		else
		{
			//if(gd.isDataChanged)
			{
				GrayRetriever  gr = null;
				
				switch(whichMethod)
				{
					//灰度比例化
					case 0:
					  if(gd.isDataChanged)
					  {
						BrightnessRetriever  br  = new BrightnessRetriever (this.param_a,lower_valve,upper_valve); 
				        br.retrieveRGB(pixelsSource,80,50,200,150,imageWidth,pixelsDestnation);
						//br.retrieveRGB(pixelsSource,imageWidth,pixelsDestnation);
					  } 
						break;
					//灰度取反	
					case 1:
						gr = new GrayRetriever( );
						gr.setImageProperty(false);
						gr.setType(GrayRetriever.PIXEL_GRAY_REVERSE );
						gr.retrieveRGB(pixelsSource,80,50,200,150,imageWidth,pixelsDestnation);
						break;
					//灰度直方图均衡化	
					case 2:
						
						HistogramRetriever  hr = new HistogramRetriever ();
		                hr.isGrayImage = true;
			            hr.retrieveRGB(pixelsSource,80,50,200,150,imageWidth,pixelsDestnation);
			
                        break;
					default:
						break;
					
				}
				offScreen.drawImage(imageCurrent,15,32,this);
			}
			g.drawImage( offScreenImage, 0, 0, this );

		}
	}
	
	public void draw_groupBox_border(Graphics g)
	{
		BorderRectangle br = new BorderRectangle(13,30,imageWidth + 2,imageHeight + 2);
		br.setBkColor(Color.black);
		br.draw(g);
	}
	
	
	public void update(Graphics g)
	{
		paint(g);
	}
	

	public void mouseClicked(MouseEvent evt)
	{
		if(evt.getModifiers() == MouseEvent.BUTTON1_MASK)
		{
			//恢复原始图像
			bRestore = true;
			
			repaint(15,32,imageWidth - 1,imageHeight - 2);
		}
		else if(evt.getModifiers() == MouseEvent.BUTTON3_MASK)
		{
			bRestore = false;
			
			String str_a,str_b,str_c,str_d;
			
			str_a = String.valueOf(param_a);
			
			gd.tx_1.setText(str_a);
			
			gd.which =  whichMethod;

			//上一次选中的单选按钮
			gd.radioManager.setSelectedCheckbox(gd.radios[whichMethod]);
			
			//显示对话框
			gd.show();
			
			if(gd.isDataChanged)
			{
				//获取对话框的文本值
				str_a = gd.str_1;
				
				//转化为数据
				
				Float float_a = new Float(str_a);
				
				param_a = float_a.floatValue();
		
				whichMethod = gd.which;
					
				
				imageCurrent = createImage(memoryImage);
				repaint(15,32,imageWidth - 1,imageHeight - 2);
			}
		}
	}
	
	public void mouseEntered(MouseEvent evt){}
	public void mouseExited(MouseEvent evt){}
	public void mousePressed(MouseEvent evt){}
	public void mouseReleased(MouseEvent evt){}

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -