FindJpg 模組報告

楊奇龍發表於2010-04-06

三.關於搜尋部分

  1想建立索引。構建jpg圖片解析器,在索引時將jpg圖片的exif資訊及其文字資訊如名稱,存放路徑,大小,日期等等加入索引!具體實現程式碼如下:

public void BulidIndex(string path)//建立索引

        {

            DateTime biStart = DateTime.Now;//建立索引開始

            DirectoryInfo[] ChildDirectory;//子目錄集

            FileInfo[] files;//當前所有檔案

            DirectoryInfo FatherDirectory = new DirectoryInfo(path); //當前目錄

            ChildDirectory = FatherDirectory.GetDirectories("*.*"); //得到子目錄集

            files = FatherDirectory.GetFiles("*.jpg");//得到jpg檔案集,可以進行操作

 

            Analyzer analyzer = new MyAnalyzer();//宣告一個分詞器,

IndexWriter indexWriter = new IndexWriter("index", analyzer, true);/*建立一個IndexWriter的例項,這個類是負責建立索引的,有很多建構函式,這裡使用的是其中的一個。三個引數分別是:索引建立到哪個目錄,用什麼分詞器,還有就是是否建立。如果是否建立為false,那麼就是以增量的方式來建立。*/

            for (int i = 0; i < files.Length; i++)

            {

                string maker = "unkown", explord = "unkown",

                       iso = "unkown", aperture = "unkown", focalLength="unkown";

                Document doc = new Document();//宣告一個document並將圖片的名稱,存放路徑,大小,日期,

                                             相機制造商,曝光度,ISO,焦距,光圈值依次透過field 新增入document中。

 

                FindExifinfo(files[i].FullName, ref maker, ref explord, ref iso, ref focalLength, ref aperture);

                doc.Add(new Field("Name", files[i].Name, Field.Store.YES, Field.Index.TOKENIZED));

                doc.Add(new Field("FullName", files[i].FullName, Field.Store.YES, Field.Index.NO));

                doc.Add(new Field("Length", files[i].Length.ToString(), Field.Store.YES, Field.Index.NO));

                doc.Add(new Field("LastWriteTime", files[i].LastWriteTime.ToString(), Field.Store.YES, Field.Index.NO));

                doc.Add(new Field("CreationTime", files[i].CreationTime.ToString(), Field.Store.YES, Field.Index.NO));

                doc.Add(new Field("Maker", maker, Field.Store.YES, Field.Index.UN_TOKENIZED));

                doc.Add(new Field("Explord", explord, Field.Store.YES, Field.Index.UN_TOKENIZED));

                doc.Add(new Field("ISO", iso, Field.Store.YES, Field.Index.UN_TOKENIZED));

                doc.Add(new Field("FocalLength", focalLength, Field.Store.YES, Field.Index.UN_TOKENIZED));

                doc.Add(new Field("Aperture", aperture, Field.Store.YES, Field.Index.UN_TOKENIZED));

    indexWriter.AddDocument(doc); /*呼叫了AddDocument方法,在AddDocument方法中,先組織一個Docuement物件,然後把這個物件交給IndexWriter*/

 

            }

 

            indexWriter.Optimize();//Optimize最佳化索引           

indexWriter.Close();//最後關閉建立過程

            DateTime biStop = DateTime.Now;//建立索引結束

            this.status1.Text = "建立索引用時:" + (biStop - biStart).TotalSeconds + "";

        }

2 執行搜尋並獲取結果:

 private void button1_Click(object sender, EventArgs e)

        {

 

            listView1.Items.Clear();

            Hits hits = null;

            Query query = null;

            Analyzer analyzer = new MyAnalyzer();

            DateTime Start = DateTime.Now;//索引開始時間

            string TEXT= this.tbkey.Text;

            BooleanQuery BQ = new BooleanQuery( );   //使用Boolean 查詢  

            if (TEXT == "")

                return;

            try

            {

              switch (this.comboBox1.SelectedIndex)

                {

                    case 0:

                        QueryParser parser = new QueryParser("Name", analyzer);

                        query = parser.Parse(tbkey.Text);

     

                        break;

                    case 1: //使用Boolean 查詢 含有某個關鍵詞或其他關鍵詞 should 表示“或”的關係

                        Term T2 = new Term("Maker", TEXT);

                        TermQuery q2 = new TermQuery(T2);

                        BQ.Add(q2, BooleanClause.Occur.SHOULD);

                        break;

                    case 2: //按照片的ISO速率進行搜尋

                        Term T3 = new Term("ISO", TEXT);

                        TermQuery q3 = new TermQuery(T3);

                        BQ.Add(q3, BooleanClause.Occur.SHOULD);

                        break;

                    case 3: //按照片的 曝光度進行搜尋

                        Term T4 = new Term("Explord", TEXT);

                        TermQuery q4 = new TermQuery(T4);

                        BQ.Add(q4, BooleanClause.Occur.SHOULD);

                        break;

                    case 4: //按照片的 焦距進行搜尋

                        Term T5 = new Term("FocalLength", TEXT);

                        TermQuery q5 = new TermQuery(T5);

                        BQ.Add(q5, BooleanClause.Occur.SHOULD);

                        break;

                    case 5: //按照片的光圈進行搜尋

                        Term T6 = new Term("Aperture", TEXT);

                        TermQuery q6 = new TermQuery(T6);

                        BQ.Add(q6, BooleanClause.Occur.SHOULD);

                        break;

                    default: break;

               

                }

            }

            catch (Exception)

            {

                throw;

            }

 

            IndexSearcher searcher = new IndexSearcher("index");

 

            if (searcher != null)

            {

                if( 0 == this.comboBox1.SelectedIndex) //使用if語句對搜尋方式進行分類,如果是按照相片或圖片名稱進行搜尋則進行關鍵字匹配查詢

                    hits = searcher.Search(query);

                else

                    hits = searcher.Search(BQ); //如果不是按名稱搜尋,則進行嚴格匹配查詢搜尋!

 

                if (hits.Length() > 0)

                {

                    this.status1.Text = "  共檢索 " + hits.Length().ToString() + "個物件";

                }

                this.imageList.Images.Clear();

                for (int i = 0; i < hits.Length(); i++)

                { //將搜尋結果分別新增入listview和imagelist中,此過程比較耗時間!無奈!!!

                    Document doc = hits.Doc(i);

                    int itemNumber = this.listView1.Items.Count;

                    //string fullname = doc.Get("FullName");

                  string[] subItem = { doc.Get("Name"), doc.Get("FullName"), (Convert.ToInt32(doc.Get("Length")) >> 10).ToString() + "KB", doc.Get("LastWriteTime") }; //使用右移 加快程式的執行速度!

                  this.imageList.Images.Add(doc.Get("FullName"), Bitmap.FromFile(doc.Get("FullName")));

                  this.listView1.Items.Add(new ListViewItem(subItem, doc.Get("FullName")));//顯示結果較慢的元兇!

                }

            }

            else

            {

                this.status1.Text = "  共檢索 0 個物件";

            }

 

            DateTime Stop = DateTime.Now;//索引完成時間

            this.status1.Text += " 搜尋用時:" + (Stop - Start).TotalSeconds + "";

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-631392/,如需轉載,請註明出處,否則將追究法律責任。

相關文章