首页>代码>java swing编写图片相似度处理识别技术代码>/图像对比/src/Demo/ImageComparerUI.java
package Demo;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ImageComparerUI extends JComponent implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton browseBtn;
	private JButton histogramBtn;
	private JButton compareBtn;
	private Dimension mySize;
	
	// image operator
	private MediaTracker tracker;
	private BufferedImage sourceImage;
	private BufferedImage candidateImage;
	private double simility; 
	
	// command constants
	public final static String BROWSE_CMD = "Browse...";
	public final static String HISTOGRAM_CMD = "Histogram Bins";
	public final static String COMPARE_CMD = "Compare Result";
	
	public ImageComparerUI() {
		JPanel btnPanel = new JPanel();
		btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		browseBtn = new JButton("Browse...");
		histogramBtn = new JButton("Histogram Bins");
		compareBtn = new JButton("Compare Result");
		
		// buttons
		btnPanel.add(browseBtn);
		btnPanel.add(histogramBtn);
		btnPanel.add(compareBtn);
		
		// setup listener...
		browseBtn.addActionListener(this);
		histogramBtn.addActionListener(this);
		compareBtn.addActionListener(this);
		
		mySize = new Dimension(620, 500);
		JFrame demoUI = new JFrame("Similiar Image Finder");
		demoUI.getContentPane().setLayout(new BorderLayout());
		demoUI.getContentPane().add(this, BorderLayout.CENTER);
		demoUI.getContentPane().add(btnPanel, BorderLayout.SOUTH);
		demoUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		demoUI.pack();
		demoUI.setVisible(true);
	}
	
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		if(sourceImage != null) {
			Image scaledImage = sourceImage.getScaledInstance(300, 300, Image.SCALE_FAST);
			g2.drawImage(scaledImage, 0, 0, 300, 300, null);
		}
		
		if(candidateImage != null) {
			Image scaledImage = candidateImage.getScaledInstance(300, 330, Image.SCALE_FAST);
			g2.drawImage(scaledImage, 310, 0, 300, 300, null);
		}
		
		// display compare result info here
		Font myFont = new Font("Serif", Font.BOLD, 16);
		g2.setFont(myFont);
		g2.setPaint(Color.RED);
		g2.drawString("The degree of similarity : " + simility, 50, 350);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(BROWSE_CMD.equals(e.getActionCommand())) {
			JFileChooser chooser = new JFileChooser();
			chooser.showOpenDialog(null);
			File f = chooser.getSelectedFile();
			BufferedImage bImage = null;
			if(f == null) return;
			try {
				bImage = ImageIO.read(f);
				
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
			tracker = new MediaTracker(this);
			tracker.addImage(bImage, 1);
			
			// blocked 10 seconds to load the image data
			try {
				if (!tracker.waitForID(1, 10000)) {
					System.out.println("Load error.");
					System.exit(1);
				}// end if
			} catch (InterruptedException ine) {
				ine.printStackTrace();
				System.exit(1);
			} // end catch
			
			if(sourceImage == null) {
				sourceImage = bImage;
			}else if(candidateImage == null) {
				candidateImage = bImage;
			} else {
				sourceImage = null;
				candidateImage = null;
			}
			repaint();
		} else if(HISTOGRAM_CMD.equals(e.getActionCommand())) {
			// do something now...
		} else if(COMPARE_CMD.equals(e.getActionCommand())) {
			ImageComparer imageCom = new ImageComparer(sourceImage, candidateImage);
			simility = imageCom.modelMatch();
			repaint();
		}
		
	}
	
	public Dimension getPreferredSize() {
		return mySize;
	}
	
	public Dimension getMinimumSize() {
		return mySize;
	}
	
	public Dimension getMaximumSize() {
		return mySize;
	}
	
	public static void main(String[] args) {
		new ImageComparerUI();
	}

}
最近下载更多
xingxing1234  LV10 2023年3月22日
公共分类  LV1 2022年10月18日
kgq_kong  LV1 2022年8月31日
zinshao  LV12 2022年7月20日
微信网友_5992582549164032  LV6 2022年6月29日
wdm0408  LV1 2022年2月22日
.rabbit  LV1 2021年12月2日
xierongsong  LV1 2021年10月25日
1005948011  LV7 2021年5月10日
warhorse  LV1 2021年4月8日
最近浏览更多
晨爽明宇  LV1 3月4日
骆红亮 2月29日
暂无贡献等级
xingxing1234  LV10 2023年3月22日
hadesxxx 2022年12月10日
暂无贡献等级
arkic1 2022年12月1日
暂无贡献等级
公共分类  LV1 2022年10月18日
kgq_kong  LV1 2022年8月31日
chp123456 2022年7月26日
暂无贡献等级
zinshao  LV12 2022年7月20日
微信网友_5992582549164032  LV6 2022年6月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友