javaweb基于内容的图片搜索引擎(4)_前台检索以及结果

图片也得到了,索引也通过lire成功建立了,剩下的,当然就是显示了,以及业务功能实现了。

前面已经说了,这个系统是javaweb,主要在struts2,spring,hibernate和tomcat服务器,mysql数据库服务器下开发的。

前台页面一个是imagesearch.JSP,主要就是供本地上传和图片url,类似于百度的识图。

而后,图像文件就会以流的形式传到后台,后台在利用lire来检索开始已经建立好的索引,最后,由lire检索后的文件会以精度排序,

并且以最开始生成检索的文件名来标示,主要功能代码如图:

public String upload() throws Exception{
		List<ImageInfo> imageInfos = new ArrayList<ImageInfo>();
		IndexLocation indexLocation = ildi.getRootLocation(1);
		String cacheName;
		InputStream inStream;
		if (file == null) {
			// 用链接来搜图的
			String[] pic = url.split("\\.");
			
			System.out.println();
			cacheName = System.currentTimeMillis() + "." + pic[pic.length-1];

			try {
				java.net.URL givenUrl = new java.net.URL(url);
				java.net.HttpURLConnection conn = (java.net.HttpURLConnection) givenUrl
						.openConnection();
				conn.setRequestMethod("GET");
				conn.setConnectTimeout(5 * 1000);
				inStream = conn.getInputStream();// 通过输入流获取图片数据
			} catch (Exception e) {
				return "linkError";
			}

			// 检索过程,通过检索而得到目标的文件地址list,方法来通用实现!
			//保存图片到本地
			
			OutputStream os = new FileOutputStream(new File(ildi.getRootLocation(3).getValue(),cacheName));
			byte[] buf = new byte[10240];
			int length = 0;
			while(-1 != (length = inStream.read(buf))){
				os.write(buf,0,length);
			}
			os.close();
			
		} else {
			
		
			//1452166113700.image/jpeg
			cacheName = System.currentTimeMillis() + "." + this.fileContentType.substring(6);
			// 用上传文件的方式来搜索的
			try {
				inStream = new FileInputStream(file);
				//保存图片到本地
				OutputStream os = new FileOutputStream(new File(ildi.getRootLocation(3).getValue(),cacheName));
				byte[] buf = new byte[1024];
				int length = 0;
				while(-1 != (length = inStream.read(buf))){
					os.write(buf,0,length);
				}
				os.close();
			} catch (Exception e) {
				return "fileError";
			}

		}

		System.out.println("000");
		IndexReader ir = IndexReader.open(FSDirectory.open(new File(indexLocation.getValue())));
        ImageSearcher is = ImageSearcherFactory.createJCDImageSearcher(10000);
        //FileInputStream fis = new FileInputStream();

        BufferedImage bi;
        if(file == null){
        	bi = ImageIO.read(new java.net.URL(url));
        	 request.setAttribute("rootPicture", url);
        }else{
        	bi = ImageIO.read(file);
        	//D:\\cache
        	String str = ildi.getRootLocation(3).getValue().toString();
        	String strimg = str.substring(0,2) + "\\" + str.substring(4);
        	System.out.println("980930129739812791##"+strimg + "\\" + cacheName);
        	request.setAttribute("rootPicture", strimg + "\\" + cacheName);
        }
       // BufferedImage bi = ImageIO.read(new java.net.URL(url));
        ImageSearchHits ish = is.search(bi, ir);
        for (int i = 0; i < ish.length(); i++) {
        	String str = ish.doc(i).getFieldable(DocumentBuilder.FIELD_NAME_IDENTIFIER).stringValue();
        	ImageInfo imgImageInfo = new ImageInfo();
        	imgImageInfo.setScore(ish.score(i));
        	imgImageInfo.setUrl(str);
        	imgImageInfo.setTitle(pdi.getPictueByName(str).getDescription());
        	imgImageInfo.setHomeUrl(pdi.getPictueByName(str).getObjurl());
        	imageInfos.add(imgImageInfo);
            System.out.println(ish.score(i) + ": " + str);
        }
        request.setAttribute("imageInfos", imageInfos);
       
       
       
		return "success";
	}
最后 当然通过传到的list集合来显示啊,但是呢,过程不会总是一帆风顺的。。。。。。

看下面的结果图:





没错,有问题!!!

下一篇就会有我找问题的感悟和收获了。





  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值