Hipi相关操作,搜索图片

前台上传一个文件,后台接收并放到一个临时文件夹里。作为单个文件

并将该文件传入进行搜索 返回 文件 进行页面预览

页面预览用的一个很笨的方法,后台返回的文件定死名字到定死的文件夹

页面重新加载固定路径的图片。


这是一个自己的思路,建议参考,不要照抄,因为确实很烂。


package com.cee.common.util;




import hipi.image.FloatImage;
import hipi.image.ImageHeader;
import hipi.image.io.JPEGImageUtil;
import hipi.imagebundle.HipiImageBundle;
import hipi.util.ByteUtils;


import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import javax.imageio.ImageIO;




import net.semanticmetadata.lire.ImageSearchHits;
import net.semanticmetadata.lire.ImageSearcher;
import net.semanticmetadata.lire.impl.GenericFastImageSearcher;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.solr.store.hdfs.HdfsDirectory;
import org.mortbay.log.Log;


import com.cee.common.SysParameter;
public class DonloadInfo extends HipiImageBundle {


 


/**


 * @param file_path


 * @param conf
 * @throws Exception 


 */


public DonloadInfo(Path file_path, Configuration conf) throws Exception {
super(file_path, conf);
try {


//开启模式

this.openForRead();
int i=0;
int k=1;
String imageSeacher2file = SysParameter.getParameter("imageSeacher2file");
ArrayList<String> list = (ArrayList<String>) this.getList(conf);
while (this.hasNext()) {
FileOutputStream out =null;
 ImageHeader imageHeader = this.next();
 FloatImage floatimage = this.readImage();
 
 if (floatimage==null||imageHeader==null)

continue;
}
String hashval = ByteUtils.asHex(ByteUtils.FloatArraytoByteArray(floatimage.getData()));
System.out.print(" 本次图片的hash:"+ hashval);


if(list.contains(hashval)){
if(k>list.size()){
break;
}
int  j = list.indexOf(hashval);
if(j<(list.size()-1)&&list.get(j).equals(list.get(j+1))){
  j=j+i;i++;
}else{
i=0;
}
System.out.println("          对比成功,为相似图片,第"+j+"张图片打印中。。。");
out = new FileOutputStream(imageSeacher2file+"\\a"+(j+1)+".jpg");
JPEGImageUtil.getInstance().encodeImage(floatimage, imageHeader, out);
out.flush();
out.close();
k++;
}else{
System.out.println(" 不是相似图片,略过。");
}
}
     System.out.println("对比完毕!!!!!");
this.close();


} catch (IOException e) {
e.printStackTrace();
}
}


//判定有多少张相同的图片,返回所有相似图片的hash值的集合。
public List<String> getList(Configuration conf) throws Exception{
String imagePath = SysParameter.getParameter("imageSeacherfile")+"//1.jpg";
String images = SysParameter.getParameter("images");
ImageSearchHits _hits = this.searchImage1(imagePath, conf.get("fs.defaultFS"), "/hadoop/index", images,"FCTH");
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < _hits.length(); i++)
{
String fileName = _hits.doc(i).getValues("descriptorImageIdentifier")[0];
System.out.println("返回相近图片的hash值:"+fileName);

list.add(fileName);
}
return list;
}


public ImageSearchHits searchImage1(String imgFilePath, String hdfsValue, String lireIndexDir, String resultNum, String searchType)
throws IOException
{

BufferedImage img = null;
if (imgFilePath.length() > 0)
{
File f = new File(imgFilePath);
img = ImageIO.read(f);

}
Configuration conf = new Configuration();
conf.set("fs.defaultFS", hdfsValue);
Path path = new Path((new StringBuilder(String.valueOf(hdfsValue))).append(lireIndexDir).toString());
HdfsDirectory directory = new HdfsDirectory(path, conf);
IndexReader ir = DirectoryReader.open(directory);
ImageSearcher searcher = getSearcher(Integer.parseInt(resultNum), searchType);
ImageSearchHits hits = searcher.search(img, ir);
ir.close();
return hits;
}
private ImageSearcher getSearcher(int resultNum, String searchType)
{
ImageSearcher searcher = new GenericFastImageSearcher(resultNum, net.semanticmetadata.lire.imageanalysis.FCTH.class);
if (searchType.equals("AutoColorCorrelogram"))
searcher = new GenericFastImageSearcher(resultNum, net.semanticmetadata.lire.imageanalysis.AutoColorCorrelogram.class);
else
if (searchType.equals("CEDD"))
searcher = new GenericFastImageSearcher(resultNum, net.semanticmetadata.lire.imageanalysis.CEDD.class);
else
if (searchType.equals("ColorLayout"))
searcher = new GenericFastImageSearcher(resultNum, net.semanticmetadata.lire.imageanalysis.ColorLayout.class);
else
if (searchType.equals("EdgeHistogram"))
searcher = new GenericFastImageSearcher(resultNum, net.semanticmetadata.lire.imageanalysis.EdgeHistogram.class);
else
if (searchType.equals("FCTH"))
searcher = new GenericFastImageSearcher(resultNum, net.semanticmetadata.lire.imageanalysis.FCTH.class);
else
if (searchType.equals("Gabor"))
searcher = new GenericFastImageSearcher(resultNum, net.semanticmetadata.lire.imageanalysis.Gabor.class);
else
if (searchType.equals("ScalableColor"))
searcher = new GenericFastImageSearcher(resultNum, net.semanticmetadata.lire.imageanalysis.ScalableColor.class);
else
if (searchType.equals("SimpleColorHistogram"))
searcher = new GenericFastImageSearcher(resultNum, net.semanticmetadata.lire.imageanalysis.SimpleColorHistogram.class);
else
if (searchType.equals("Tamura"))
searcher = new GenericFastImageSearcher(resultNum, net.semanticmetadata.lire.imageanalysis.Tamura.class);
return searcher;
}

 


}

你可能感兴趣的:(Hipi相关操作,搜索图片)