package StudyTEST;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
public class Tongue_Extract {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Tongue_Extract window = new Tongue_Extract();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Tongue_Extract (){
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(650,160,680,740);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 10, 635, 138);
frame.getContentPane().add(panel);
JLabel lblNewLabel = new JLabel("");
String tongueName = "C:\\Users\\201801033013\\eclipse-workspace\\EducationPractice\\src\\JT}@X6)4_2T}X}5ZG_Q@UAP.png";
lblNewLabel.setIcon(new ImageIcon(tongueName));
panel.add(lblNewLabel);
scrollPane = new JScrollPane();
scrollPane.setBounds(32, 204, 598, 420);
frame.getContentPane().add(scrollPane);
JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.GRAY);
panel_1.setForeground(Color.GRAY);
scrollPane.setViewportView(panel_1);
JLabel lblNewLabel_1 = new JLabel("舌像PNG:");
lblNewLabel_1.setBounds(22, 169, 61, 25);
frame.getContentPane().add(lblNewLabel_1);
textField = new JTextField();
textField.setBounds(104, 171, 330, 21);
frame.getContentPane().add(textField);
textField.setColumns(10);
//6个按钮
JButton btnNewButton = new JButton("加载");
btnNewButton.setBounds(511, 170, 93, 23);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("清除最后结点");
btnNewButton_1.setEnabled(false);
btnNewButton_1.setBounds(104, 645, 131, 23);
frame.getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("全部清除");
btnNewButton_2.setEnabled(false);
btnNewButton_2.setBounds(249, 645, 93, 23);
frame.getContentPane().add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("提取舌像");
btnNewButton_3.setEnabled(false);
btnNewButton_3.setBounds(355, 645, 93, 23);
frame.getContentPane().add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("结束");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton_4.setBounds(552, 645, 81, 23);
frame.getContentPane().add(btnNewButton_4);
JCheckBox chckbxNewCheckBox = new JCheckBox("准备提取");
chckbxNewCheckBox.setEnabled(false);
chckbxNewCheckBox.setBounds(10, 645, 103, 23);
frame.getContentPane().add(chckbxNewCheckBox);
label = new JLabel();
panel_1.add(label);
JButton btnNewButton_5 = new JButton("保存");
btnNewButton_5.setBounds(457, 645, 81, 23);
frame.getContentPane().add(btnNewButton_5);
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
//加载按钮
btnNewButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
// TODO Auto-generated method stub
int result = chooser.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION)
{
String fileName = chooser.getCurrentDirectory()+"\\"+chooser.getSelectedFile().getName();
textField.setText(fileName);
String name = chooser.getSelectedFile().getPath();
myPanel = new MyPanel(name);
scrollPane.setViewportView(myPanel);
myPanel.setBackground(Color.GRAY);
scrollPane.setBackground(Color.GRAY);
myPanel.repaint();
scrollPane.repaint();
}
counter++;
chckbxNewCheckBox.setEnabled(true);
}
});
//清除最后一个结点按钮
btnNewButton_1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
myPanel.repaint();
}
});
//清除全部结点
btnNewButton_2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
myPanel.close = false;
myPanel.ndoArrayLen = 0;
myPanel.repaint();
}
});
//提取舌像
btnNewButton_3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
myPanel.startExtract = true;
myPanel.repaint();
scrollPane.setViewportView(myPanel);
myPanel.repaint();
scrollPane.repaint();
}
});
//准备提取
chckbxNewCheckBox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if(myPanel.prepared==false) {
btnNewButton_1.setEnabled(true);
btnNewButton_2.setEnabled(true);
btnNewButton_3.setEnabled(true);
myPanel.prepared = true;
}
else {
btnNewButton_1.setEnabled(false);
btnNewButton_2.setEnabled(false);
btnNewButton_3.setEnabled(false);
myPanel.prepared = false;
}
k++;
}
});
//结束监听器
btnNewButton_4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
System.exit(0);
}
});
}
private JFrame frame;
private JTextField textField;
private JLabel label;
private JFileChooser chooser;
public static int k = 0;
public static int counter = 0;
MyPanel myPanel;
JScrollPane scrollPane;
}
package StudyTEST;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import shiyan.getKB;
public class MyPanel extends JPanel {
public String strFileName;
// 构造函数
public MyPanel(String strFileName) {
this.strFileName = strFileName;
// 实现在图片面板中绘图,并增加鼠标监听器
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 绘图,划线 c
AmouseCounter++; // 鼠标点击次数
JPanel p = (JPanel) e.getSource();
Graphics g = p.getGraphics();
g.setColor(Color.green);
if (prepared) {
if (close)
return;// 如果闭合就不能加点了
// 创建并存储数组
mydotArray[i] = new Point(e.getX(), e.getY());
i++;
ndoArrayLen++;// 数组长度++
if (ndoArrayLen == 1) {
g.drawRect(e.getX(), e.getY(), 5, 5);
}
// 第二个点开始,画图也划线,从第三个点开始,如果最后一个点和第一个点相近,就只与第一个点连线(不画小矩形)
else if (ndoArrayLen == 2) {
// 如果距离正常(大于5),则画图亦划线
if ((e.getX() - mydotArray[i - 2].x) * (e.getX() - mydotArray[i - 2].x)
+ (e.getY() - mydotArray[i - 2].y) * (e.getY() - mydotArray[i - 2].y) >= 25) {
int x1 = mydotArray[i - 2].x;
int y1 = mydotArray[i - 2].y;
g.drawLine(x1 + 2, y1 + 2, e.getX() + 2, e.getY() + 2);
g.drawRect(e.getX(), e.getY(), 5, 5);
}
// 如果距离过小就不会再绘制图形和连线,也要实现不储存新点击结点的鼠标事件
else {
i--;
}
}
// 从第三个点开始,如果最后一个点和第一个点相近,就只与第一个点连线(不画小矩形)
else {
if ((e.getX() - mydotArray[0].x) * (e.getX() - mydotArray[0].x)
+ (e.getY() - mydotArray[0].y) * (e.getY() - mydotArray[0].y) <= 25) {
int f1 = mydotArray[i - 2].x;
int f2 = mydotArray[i - 2].y;
int r1 = mydotArray[0].x;
int r2 = mydotArray[0].y;
g.drawLine(f1 + 2, f2 + 2, r1 + 2, r2 + 2);
ndoArrayLen--;
i--;
close = true;// 已成闭合状态
} else {
if ((e.getX() - mydotArray[i - 2].x) * (e.getX() - mydotArray[i - 2].x)
+ (e.getY() - mydotArray[i - 2].y) * (e.getY() - mydotArray[i - 2].y) >= 25) {
int x1 = mydotArray[i - 2].x;
int y1 = mydotArray[i - 2].y;
g.drawLine(x1 + 2, y1 + 2, e.getX() + 2, e.getY() + 2);
g.drawRect(e.getX(), e.getY(), 5, 5);
} else {
i--;
}
}
}
}
}
});
}
public void paintComponent(Graphics g) {
if(startExtract == true) {
BufferedImage bi = null;
File fin = new File(strFileName);
try {
bi = ImageIO.read(fin);
} catch (IOException e) {
e.printStackTrace();
}
//points[0] = new Point(100,100);
BufferedImage bii = cutImage(mydotArray,ndoArrayLen,bi);
if(bii != null) {
g.drawImage(bii, 0, 0, null);
}
}
else {
ImageIcon image = new ImageIcon(strFileName);
this.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
g.drawImage(image.getImage(), 0, 0, image.getIconWidth(), image.getIconHeight(), this);
g.setColor(Color.green);
// 绘制顶点和连线
// 从新开始画图
if (prepared) {
if (ndoArrayLen >= 2) {// 当有多个点的时候
if (close) {// 当最后一个结点和第一个结点相同只清除线,不清除结点和数组
for (int i = 0; i < ndoArrayLen; i++)
g.drawRect(mydotArray[i].x, mydotArray[i].y, 5, 5);
for (int i = 1; i < ndoArrayLen; i++)
g.drawLine(mydotArray[i - 1].x + 2, mydotArray[i - 1].y + 2, mydotArray[i].x + 2,
mydotArray[i].y + 2);
close = false;
} else {
g.drawRect(mydotArray[0].x, mydotArray[0].y, 5, 5);
for (int i = 1; i < ndoArrayLen - 1; i++) {
g.drawRect(mydotArray[i].x, mydotArray[i].y, 5, 5);
g.drawLine(mydotArray[i - 1].x + 2, mydotArray[i - 1].y + 2, mydotArray[i].x + 2,
mydotArray[i].y + 2);
}
i--;
ndoArrayLen--;
}
} else if (ndoArrayLen == 1) {
i--;
ndoArrayLen--;
} else {
i = 0;
ndoArrayLen = 0;
}
}
}
}
/*
* public BufferedImage cutImage(BufferedImage img) { BufferedImage bimg = img;
* int [][] data = new int[bimg.getWidth()][bimg.getHeight()];
*
* for(int i=0; i<bimg.getWidth(); i++){ for(int j=0; j<bimg.getHeight(); j++){
* data[i][j]=bimg.getRGB(i,j); } } Raster raster = bimg.getData(); int [] temp
* = new int[raster.getWidth()*raster.getHeight()*raster.getNumBands()];
* BufferedImage newImage = null;
*
* for(int i=0; i<bimg.getWidth(); i++){ for(int j=0; j<bimg.getHeight(); j++){
* newImage.setRGB(i, j, data[i][j]); } } return newImage; }
*/
public BufferedImage cutImage(Point [] mydotArray , int len, BufferedImage img) {
int sumx = 0, sumy = 0;
for(int i=0; i<len; i++) {
sumx += mydotArray[i].getX();
sumy += mydotArray[i].getY();
}
Point core = new Point(sumx/len,sumy/len);
//System.out.println("顶点集合的中心点为:" + core.x + " " + core.y);//已经求出重心点了,然后求面积
double corex = core.x;
double corey = core.y;
BufferedImage bimg = img;
for(int a =0; a<len; a++) {
//确定直线,求出k、b
double x1 = (double)mydotArray[a].getX();
double y1 = (double)mydotArray[a].getY();
double x2,y2;
if(a == len-1) {
x2 = (double)mydotArray[0].getX();
y2 = (double)mydotArray[0].getY();
}
else {
x2 = (double)mydotArray[a+1].getX();
y2 = (double)mydotArray[a+1].getY();
}
getKB getkb = new getKB(x1,y1,x2,y2);
double k = getkb.k;
double b = getkb.b;
//判断重心点在直线的哪一侧
int [][] p = new int[bimg.getWidth()][bimg.getHeight()];
if(k>=0 && k<Double.POSITIVE_INFINITY) {//直线斜率在0~正无穷之间
if(corey > k * corex + b) {
//只要 j>k*i+b 的点都保留,否则都舍去
for (int j = bimg.getMinY(); j < bimg.getHeight(); j++) {
for (int i = bimg.getMinX(); i < bimg.getWidth(); i++) {
p[i][j] = bimg.getRGB(i, j);
if(j>k*i+b) {
int a1 = (p[i][j] >> 24) & 0xff;
int r1 = (p[i][j] >> 16) & 0xff;
int g1 = (p[i][j] >> 8) & 0xff;
int b1 = p[i][j] & 0xff;
p[i][j] = (a1 << 24) | (r1 << 16) | (g1 << 8) | b1;
}
else {
p[i][j] = (0 << 24) | (0 << 16) | (0 << 8) | 0;
}
bimg.setRGB(i, j, p[i][j]);
}
}
}
else {
for (int j = bimg.getMinY(); j < bimg.getHeight(); j++) {
for (int i = bimg.getMinX(); i < bimg.getWidth(); i++) {
p[i][j] = bimg.getRGB(i, j);
if(j<k*i+b) {
int a1 = (p[i][j] >> 24) & 0xff;
int r1 = (p[i][j] >> 16) & 0xff;
int g1 = (p[i][j] >> 8) & 0xff;
int b1 = p[i][j] & 0xff;
p[i][j] = (a1 << 24) | (r1 << 16) | (g1 << 8) | b1;
}
else {
p[i][j] = (0 << 24) | (0 << 16) | (0 << 8) | 0;
}
bimg.setRGB(i, j, p[i][j]);
}
}
}
}
else {//直线斜率在负无穷~0之间
if(corey > k * corex + b) {
//只要 j>k*i+b 的点都保留,否则都舍去
for (int j = bimg.getMinY(); j < bimg.getHeight(); j++) {
for (int i = bimg.getMinX(); i < bimg.getWidth(); i++) {
p[i][j] = bimg.getRGB(i, j);
if(j>k*i+b) {
int a1 = (p[i][j] >> 24) & 0xff;
int r1 = (p[i][j] >> 16) & 0xff;
int g1 = (p[i][j] >> 8) & 0xff;
int b1 = p[i][j] & 0xff;
p[i][j] = (a1 << 24) | (r1 << 16) | (g1 << 8) | b1;
}
else {
p[i][j] = (0 << 24) | (0 << 16) | (0 << 8) | 0;
}
bimg.setRGB(i, j, p[i][j]);
}
}
}
else {
for (int j = bimg.getMinY(); j < bimg.getHeight(); j++) {
for (int i = bimg.getMinX(); i < bimg.getWidth(); i++) {
p[i][j] = bimg.getRGB(i, j);
if(j<k*i+b) {
int a1 = (p[i][j] >> 24) & 0xff;
int r1 = (p[i][j] >> 16) & 0xff;
int g1 = (p[i][j] >> 8) & 0xff;
int b1 = p[i][j] & 0xff;
p[i][j] = (a1 << 24) | (r1 << 16) | (g1 << 8) | b1;
}
else {
p[i][j] = (0 << 24) | (0 << 16) | (0 << 8) | 0;
}
bimg.setRGB(i, j, p[i][j]);
}
}
}
}
}
return bimg;
}
public int i = 0;
public int AmouseCounter = 0;
public Point mydotArray[] = new Point[100];
public int ndoArrayLen = 0;
public boolean close = false;// 初始值没有闭合
public boolean prepared = false;
public int paintCounter = 0;
public boolean startExtract = false;
}
package StudyTEST;
public class getKB {
//public double x1,y1;
//public double x2,y2;
public double k;
public double b;
public double x;
public getKB(double x1,double y1, double x2, double y2) {
if(x1 == x2) {
this.k = 0xfffff;
this.b = -0xfffff;
this.x = x1;
}
else {
this.k = (y2-y1)/(x2-x1);
this.b = y1 - this.k * x1;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//getKB getkb = new getKB(100,100,200,100);
//System.out.println(getkb.k + " "+ getkb.b);
}
}
实验结果: