📄 matchimage.java
字号:
package cn.dxm.client;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.sql.ResultSet;
import java.sql.SQLException;
import cn.dxm.client.DB_Conn;
import cn.dxm.client.ImageInfo;
import searchjet.annotation.GetImageFeature;
public class MatchImage {
/**
* @param args
*/
public String[] GetImage(String s){
String[] filepath= new String[100];
try
{
File pathName=new File(s);
String[] fileNames=pathName.list();
for(int i=0;i<fileNames.length;i++)
{
File f=new File(pathName.getPath(),fileNames[i]);
filepath[i]=f.getCanonicalPath();
System.out.println(fileNames[i]);
}
}
catch(IOException e){e.printStackTrace();}
return filepath;
}
/**
* 删除文件,可以是单个文件或文件夹
* @param fileName 待删除的文件名
* @return 文件删除成功返回true,否则返回false
*/
public static boolean delete(String fileName){
File file = new File(fileName);
if(!file.exists()){
System.out.println("删除文件失败:"+fileName+"文件不存在");
return false;
}else{
if(file.isFile()){
return deleteFile(fileName);
}
else{
return deleteDirectory(fileName);
}
}
}
/**
* 删除单个文件
* @param fileName 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String fileName){
File file = new File(fileName);
if(file.isFile() && file.exists()){
file.delete();
System.out.println("删除单个文件"+fileName+"成功!");
return true;
}else{
System.out.println("删除单个文件"+fileName+"失败!");
return false;
}
}
/**
* 删除目录(文件夹)以及目录下的文件
* @param dir 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public static boolean deleteDirectory(String dir){
//如果dir不以文件分隔符结尾,自动添加文件分隔符
if(!dir.endsWith(File.separator)){
dir = dir+File.separator;
}
File dirFile = new File(dir);
//如果dir对应的文件不存在,或者不是一个目录,则退出
if(!dirFile.exists() || !dirFile.isDirectory()){
System.out.println("删除目录失败"+dir+"目录不存在!");
return false;
}
boolean flag = true;
//删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
for(int i=0;i<files.length;i++){
//删除子文件
if(files[i].isFile()){
flag = deleteFile(files[i].getAbsolutePath());
if(!flag){
break;
}
}
//删除子目录
else{
flag = deleteDirectory(files[i].getAbsolutePath());
if(!flag){
break;
}
}
}
if(!flag){
System.out.println("删除目录失败");
return false;
}
return true;
}
public Vector list_img()
{
DB_Conn db_conn=new DB_Conn();
db_conn.ConnectDB();
ResultSet rs=null;
try
{
String sql="select * from img";
sql = new String(sql.getBytes("ISO8859-1"), "GB2312");
rs=db_conn.sm.executeQuery(sql);
Vector vector = new Vector();
//ImageInfo imageinfos=null;
//ResultSet指针最初位于第一行之前,调用next方法使下一行成为当前行
while(rs.next())
{
HashMap hashmap=new HashMap();
// imageinfos = new ImageInfo();
// imageinfos.setId(rs.getInt("id"));
// imageinfos.setName(rs.getString("name"));
// imageinfos.setMoment(rs.getString("moment"));
// imageinfos.setBarycenterx(rs.getString("barycenterx"));
// imageinfos.setBarycentery(rs.getString("barycentery"));
// imageinfos.setBarycentervalue(rs.getString("barycentervalue"));
// imageinfos.setEnergy(rs.getString("energy"));
// imageinfos.setEntropy(rs.getString("entropy"));
// imageinfos.setInertia(rs.getString("inertia"));
// imageinfos.setPlacidity(rs.getString("placidity"));
// imageinfos.setColorhistogram(rs.getString("colorhistogram"));
// //向Vector矢量中添加对象imageinfos
hashmap.put("id",String.valueOf(rs.getInt("id")));//编码
hashmap.put("name",rs.getString("name"));//姓名
hashmap.put("moment",rs.getString("moment"));//力矩
hashmap.put("barycenterx", rs.getString("barycenterx"));//重心矩坐标X
hashmap.put("barycentery", rs.getString("barycentery"));//重心矩坐标Y
hashmap.put("barycentervalue", rs.getString("barycentervalue"));//重心矩值
hashmap.put("energy",rs.getString("energy"));//能量
hashmap.put("entropy", rs.getString("entropy"));//熵
hashmap.put("inertia", rs.getString("inertia"));//惯性矩
hashmap.put("placidity", rs.getString("placidity"));//局部平稳性
hashmap.put("colorhistogram",rs.getString("colorhistogram"));//颜色直方图
vector.addElement(hashmap);
}
return vector;
}
catch (SQLException SqlE)
{
SqlE.printStackTrace();
return null;
}
catch (Exception E)
{
E.printStackTrace();
return null;
}
finally
{
// 关闭连接,释放数据库资源:
db_conn.CloseDB();
}
}
public String getMaxFeature(){
String s=System.getProperty("user.dir")+"\\client\\images";
String[] image= GetImage(s);
GetImageFeature imgfea =new GetImageFeature();
Matching match =new Matching();
HashMap hashmap1=new HashMap();
HashMap hashmap2=new HashMap();
hashmap1 = imgfea.getImageFeature(image[0]);
System.out.println("image[0]="+image[0]);
System.out.println(hashmap1); //截取的图片hashmap值
Vector vector=new Vector();
vector=list_img();
int size=vector.size();
double maxNum=0.0;
double currentNum;
int j=0;
for(int i=0;i<size;i++){
hashmap2 =(HashMap) vector.elementAt(i);
System.out.println(hashmap2); //截取的图片hashmap2值
currentNum =match.Match(hashmap1,hashmap2);
System.out.println(currentNum);
if(currentNum>maxNum) {
maxNum=currentNum;
j=i;
}
}
//delete(s);
Map image1 = new HashMap();
image1 = (Map) vector.elementAt(j);
String name = (String)image1.get("name");
String id = (String)image1.get("id");
System.out.println(id);
System.out.println(name);
System.out.println(maxNum);
return name;
}
/**
* @see 复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
//int length = 0;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
// System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
public static void main(String[] args){
// TODO Auto-generated method stub
// MatchImage img =new MatchImage();
// GetImageFeature imgfea =new GetImageFeature();
// String s="E:\\StudySoft\\MyEclipse 6.0\\workspace\\IMGDeal\\client\\images";
// String[] image= img.GetImage(s);
// //System.out.print(image[0]);
//
// System.out.println(imgfea.getImageFeature(image[0]));
// //img.delete(s);
//
// Vector vector=new Vector();
// vector=img.list_img();
// int size=vector.size();
// System.out.println(size);
// System.out.println(vector.elementAt(0));
// System.out.println(vector.elementAt(1));
// for(int i=0;i<size;i++){
//
// System.out.println(vector.elementAt(i));
// }
MatchImage img =new MatchImage();
img.getMaxFeature();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -