ImageProcessBHO.cpp
上传用户:pass2008
上传日期:2021-07-05
资源大小:3299k
文件大小:9k
源码类别:
Internet/IE编程
开发平台:
Visual C++
- // ImageProcessBHO.cpp : CImageProcessBHO 的实现
- #include "stdafx.h"
- #include "ImageProcessBHO.h"
- #include <stdio.h>
- #include "ImgProcUnit.h"
- // CImageProcessBHO
- STDMETHODIMP CImageProcessBHO::SetSite(IUnknown* pUnkSite)
- {
- if(pUnkSite!=NULL)
- {
- //缓存指向IWebBrowser2的指针。
- HRESULT hr = pUnkSite->QueryInterface(IID_IWebBrowser2, (void**)&m_spWebBrowser);
- if(SUCCEEDED(hr))
- {
- //注册事件
- hr = DispEventAdvise(m_spWebBrowser);
- if(SUCCEEDED(hr))
- {
- m_fAdvised=TRUE;
- }
- }
- }
- else
- {
- //取消注册事件。
- if(m_fAdvised)
- {
- DispEventUnadvise(m_spWebBrowser);
- m_fAdvised = FALSE;
- }
- //在此释放缓存的指针和其他资源。
- m_spWebBrowser.Release();
- }
- //返回基类实现
- return IObjectWithSiteImpl::SetSite(pUnkSite);
- }
- void STDMETHODCALLTYPE CImageProcessBHO::OnDocumentComplete(IDispatch* pDisp, VARIANT* pvarURL)
- {
- HRESULT hr = S_OK;
- CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;
- if(spTempWebBrowser
- && m_spWebBrowser
- && m_spWebBrowser.IsEqualObject(spTempWebBrowser))
- {
- CComPtr<IDispatch> spDispDoc;
- hr = m_spWebBrowser->get_Document(&spDispDoc);
- if(SUCCEEDED(hr))
- {
- CComQIPtr<IHTMLDocument2> spHTMLDoc = spDispDoc;
- if(spHTMLDoc!=NULL)
- {
- BSTR URL;
- spHTMLDoc->get_URL(&URL);
- //domainURL.Empty();
- domainURL = URL;
- // 这两种情况下就不要处理了
- if (domainURL == _T("about:blank") || domainURL == _T("res://ieframe.dll/tabswelcome.htm"))
- {
- return;
- }
- BSTR bstLocalHTML;
- if (m_bNavigatedToLocalHTML)
- {
- //spHTMLDoc->get_URL(&URL);
- //CString tempURL;
- //tempURL = URL;
- if (domainURL == (_T("file://")+localStockedPath))
- {
- //bstLocalHTML = localStockedPath.AllocSysString();
- //m_spWebBrowser->Navigate(bstLocalHTML, NULL, NULL, NULL, NULL); //转到本地保存的HTML页面
- //m_bNavigatedToLocalHTML = FALSE; //确保不进入死循环
- return;
- }
- else if (domainURL == predomainURL)
- {
- bstLocalHTML = localStockedPath.AllocSysString();
- m_spWebBrowser->Navigate(bstLocalHTML, NULL, NULL, NULL, NULL); //转到本地保存的HTML页面
- }
- else
- {
- localStockedPath = domainURL;
- elimInvalidPrefixSymbols(localStockedPath);
- localStockedPath = _T("C:\") + localStockedPath + _T(".htm");
- CFileFind tFileFind;
- if (!tFileFind.FindFile(localStockedPath))
- {
- ::URLDownloadToFile(NULL, domainURL, localStockedPath, 0, NULL); //保存网页,用于修改显示
- RetrieveImagesLinks(spHTMLDoc);
- JointImagesURL(); //得到图片的绝对路径
- //图像下载及边缘化/灰度化处理
- ImgProcUnit* imgProcUnit = new ImgProcUnit(imageLinks, domainURL);
- imgProcUnit->downloadImages(); //下载图片
- imgProcUnit->imageEdgeProcess(); //图像边缘化处理
- for (int i=0; i<imgProcUnit->imageNames.GetCount(); i++)
- {
- imageNames.Add(imgProcUnit->imageNames.GetAt(i));
- }
- //修改保存在本地的网页
- ModifyOriHTMLPicLinks(localStockedPath);
- //m_bNavigatedToLocalHTML = TRUE; //确保不进入死循环
- }
- bstLocalHTML = localStockedPath.AllocSysString();
- m_spWebBrowser->Navigate(bstLocalHTML, NULL, NULL, NULL, NULL); //转到本地保存的HTML页面
- predomainURL = domainURL;
- }
- }
- else
- {
- localStockedPath = domainURL;
- elimInvalidPrefixSymbols(localStockedPath);
- localStockedPath = _T("C:\") + localStockedPath + _T(".html");
- CFileFind tFileFind;
- if (!tFileFind.FindFile(localStockedPath))
- {
- ::URLDownloadToFile(NULL, domainURL, localStockedPath, 0, NULL); //保存网页,用于修改显示
- RetrieveImagesLinks(spHTMLDoc);
- JointImagesURL(); //得到图片的绝对路径
- //图像下载及边缘化/灰度化处理
- ImgProcUnit* imgProcUnit = new ImgProcUnit(imageLinks, domainURL);
- imgProcUnit->downloadImages(); //下载图片
- imgProcUnit->imageEdgeProcess(); //图像边缘化处理
- for (int i=0; i<imgProcUnit->imageNames.GetCount(); i++)
- {
- imageNames.Add(imgProcUnit->imageNames.GetAt(i));
- }
- //修改保存在本地的网页
- ModifyOriHTMLPicLinks(localStockedPath);
- }
- bstLocalHTML = localStockedPath.AllocSysString();
- m_spWebBrowser->Navigate(bstLocalHTML, NULL, NULL, NULL, NULL); //转到本地保存的HTML页面
- predomainURL = domainURL;
- m_bNavigatedToLocalHTML = TRUE; //确保不进入死循环
- }
- }
- }
- }
- }
- // 获取图片的下载链接
- void CImageProcessBHO::RetrieveImagesLinks(IHTMLDocument2* pDocument)
- {
- CComPtr<IHTMLElementCollection> spImages;
- HRESULT hr = pDocument->get_images(&spImages);
- if(hr==S_OK && spImages!=NULL)
- {
- long cImages = 0;
- hr = spImages->get_length(&cImages);
- if(hr==S_OK && cImages>0)
- {
- for(int i=0; i<cImages; i++)
- {
- CComVariant svarItemIndex(i);
- CComVariant svarEmpty;
- CComPtr<IDispatch> spdispImage;
- hr = spImages->item(svarItemIndex, svarEmpty, &spdispImage);
- if(hr==S_OK && spdispImage!=NULL)
- {
- CComQIPtr<IHTMLElement> spElement = spdispImage;
- if(spElement)
- {
- BSTR bst;
- spElement->get_outerHTML(&bst);
- CString otHTML(bst);
- //otHTML.MakeLower();
- imageLinks.Add(otHTML);
- }
- }
- }
- }
- }
- }
- void CImageProcessBHO::ModifyOriHTMLPicLinks(LPCTSTR htmlPath)
- {
- CFile htmlFile;
- if (!htmlFile.Open(htmlPath, CFile::modeReadWrite | CFile::shareExclusive))
- {
- return;
- }
- DWORD dwHtmlSize = htmlFile.GetLength();
- char* lpHtml = new char[dwHtmlSize+1];
- try
- {
- if (htmlFile.Read(lpHtml, dwHtmlSize) != dwHtmlSize)
- {
- delete[] lpHtml;
- lpHtml = NULL;
- return;
- }
- }
- catch (CFileException* e)
- {
- e->Delete();
- delete[] lpHtml;
- lpHtml = NULL;
- return;
- }
- CString procHtml(lpHtml);
- //procHtml.MakeLower();
- delete[] lpHtml;
- lpHtml = NULL;
- htmlFile.Close();
- //修改html文件中图片的路径,改为处理后的图片路径
- int index;
- CString temp;
- for (int i=0; i<imageOuterHTML.GetCount(); i++)
- {
- temp = imageOuterHTML.GetAt(i);
- index = procHtml.Find(temp);
- if (index > -1 && imageNames.GetAt(i) != _T("Download Error!"))
- {
- //replaceIExplorerImgOuterHTML(temp, imageNames.GetAt(i));
- procHtml.Replace(imageOuterHTML.GetAt(i), imageNames.GetAt(i));
- }
- }
- LPTSTR lpNewHtml = procHtml.GetBuffer(procHtml.GetLength()+1);
- if (!htmlFile.Open(htmlPath, CFile::modeCreate | CFile::modeReadWrite | CFile::shareExclusive))
- {
- return;
- }
- htmlFile.Write(lpNewHtml, procHtml.GetLength()+1);
- htmlFile.Close();
- }
- //对于没有给出绝对路径的链接补全其路径
- void CImageProcessBHO::JointImagesURL()
- {
- if (imageLinks.GetCount()>0)
- {
- //int count = 0;
- int cImages = imageLinks.GetCount();
- CString temp;
- CString src("src="");
- CString http("http:");
- for (int i=0; i<cImages; i++)
- {
- temp = imageLinks.GetAt(i);
- //获得src中的内容,可能是绝对路径也可能是相对路径
- temp = GetImgSrc(temp, src);
- imageOuterHTML.Add(temp);
- //匹配"http:"检查图片是否是绝对路径
- if (simpleMatch(temp, http))
- {
- imageLinks.SetAt(i, temp);
- }
- else
- {
- //添加域名路径合成绝对路径
- synFullURLPath(i, temp);
- }
- }
- }
- }
- CString CImageProcessBHO::GetImgSrc(CString& linkInfo, CString& strImgSrc)
- {
- CString imgSrc = _T("");
- int v1 = linkInfo.GetLength();
- int v2 = strImgSrc.GetLength();
- int i = 0, j = 0;
- while (i<v1-v2 && j<v2)
- {
- if (strImgSrc.GetAt(j) == linkInfo.GetAt(i+j))
- {
- j++;
- }
- else
- {
- j = 0;
- i++;
- }
- }
- if (j == v2)
- {
- CString temp(_T(""));
- temp = linkInfo.Right(v1-(i+j));
- int endInfo = temp.Find('"');
- temp = temp.Left(endInfo);
- imgSrc = temp;
- }
- return imgSrc;
- }
- BOOL CImageProcessBHO::simpleMatch(CString& linkInfo, CString& strHttp)
- {
- int v1 = linkInfo.GetLength();
- int v2 = strHttp.GetLength();
- int i = 0, j = 0;
- while (i<v1-v2 && j<v2)
- {
- if (strHttp.GetAt(j) == linkInfo.GetAt(i+j))
- {
- j++;
- }
- else
- {
- j = 0;
- i++;
- }
- }
- if (j == v2)
- {
- return TRUE;
- }
- return FALSE;
- }
- void CImageProcessBHO::synFullURLPath(int index, CString& subStrLink)
- {
- int len = domainURL.GetLength();
- CString temp = domainURL.Left(len-1);
- temp += subStrLink;
- imageLinks.SetAt(index, temp);
- }
- /*
- void CImageProcessBHO::replaceIExplorerImgOuterHTML(CString& strOtHTML, LPCTSTR imgPath)
- {
- CString imgLinkURL(strOtHTML);
- CString src("src="");
- int len = imgLinkURL.GetLength();
- int matchLen = src.GetLength();
- int index = imgLinkURL.Find(src);
- imgLinkURL = imgLinkURL.Right(len-index-matchLen);
- index = imgLinkURL.Find(""");
- imgLinkURL = imgLinkURL.Left(index);
- CString temp;
- temp.Format(imgPath);
- strOtHTML.Replace(imgLinkURL, temp);
- }*/
- BOOL CImageProcessBHO::elimInvalidPrefixSymbols(CString& strPrefix)
- {
- strPrefix.Remove('\');
- strPrefix.Remove('/');
- strPrefix.Remove(':');
- strPrefix.Remove('*');
- strPrefix.Remove('?');
- strPrefix.Remove('"');
- strPrefix.Remove('<');
- strPrefix.Remove('>');
- strPrefix.Remove('|');
- strPrefix.Remove('.');
- strPrefix.Remove('_');
- strPrefix.Remove('-');
- return TRUE;
- }