三、COM組件接口設計
前面我們在輸入文本後,在程序中創建設備上下文,對輸入 的文本進行計算並輸出了指定文件名的位圖文件,在本設計中,我們使用了以JPEG壓縮格式 存儲文件的方式以減小網絡傳輸時間,因jpeg壓縮方法的論述已超出本文范圍,故在此不再 贅述。
為了讓其他語言調用此接口,我們以 COM 組件的方式發布此程序,可供 VB,DELPHI,PB,ASP等程序調用,下面給出COM組件的設計方法,一般COM組件的創建及編譯超 出本文范圍,故不再作解釋。
在組件中清加方法:
STDMETHOD(OutTextImg) (/*[out, retval]*/ long *pVal);ltvalue(500)] long lWeight,
[in,optional,defaultvalue(0)] long l3D);
和
STDMETHOD(OutImg) (BSTR bstrFileName, long lDelFile);
分別實現處理文件本保存為圖像及把圖像輸 出到用戶浏覽器。實現代碼如下:
STDMETHODIMP CAspImage::OutImgFromText(BSTR bstrFilePath,
BSTR bstrText,
BSTR bstrBgImg,
long lCSet,
BSTR bstrFont,
long lWidth,
long lHeight,
long lLeft,
long lTop,
long llfHeight,
long lWeight,
long l3D)
{
CImg img;
try{
if(0 == img.OutImgFromText(
bstrFilePath,
bstrText,
bstrBgImg,
lCSet,
bstrFont,
lWidth,
lHeight,
lLeft,
lTop,
llfHeight,
lWeight,
l3D))
{
return S_OK;
}
else
{
return S_FALSE;
}
}
catch(...)
{
return S_FALSE;
}
return S_OK;
/**/
}
一些處理代碼我們封裝在了Cimg類中,在前面做過介紹,在這裡只是簡單調用即 可。
STDMETHODIMP CAspImage::OutTextImg(long *pVal)
{
HRESULT hr = OutImgFromText(bstrFilePath,
bstrText,
bstrBgImg,
lCSet,
bstrFont,
lWidth,
lHeight,
lLeft,
lTop,
llfHeight,
lWeight,
l3D);
if(SUCCEEDED (hr))
*pVal = 0;
else
*pVal = -1;
return S_OK;
}
OutTextImg 函數只簡單調用OutImgFromText 接口。
STDMETHODIMP CAspImage::OutImg(BSTR bstrFileName, long lDelFile)
{
// TODO: Add your implementation code here
_variant_t vReturnBuffer;
LPSAFEARRAY psaFile;
HANDLE hFile;
DWORD dwSizeOfFile;
DWORD dwNumberOfBytesRead;
BOOL bResult;
unsigned char *pReturnBuffer = NULL;
long k;
HRESULT hr = S_OK;
// Create file in this case only OPENS an existing file (or fails
// if the file does not exist!)
hFile = ::CreateFile(
bstrFileName, // name of the file
GENERIC_READ, // desired access
FILE_SHARE_READ, // shared access
NULL, // security attributes
OPEN_EXISTING, // creation disposition - open only if existing!
FILE_FLAG_SEQUENTIAL_SCAN, // flag attributes
NULL );
if( hFile == INVALID_HANDLE_VALUE )
{
return E_FAIL;
}
dwSizeOfFile = ::GetFileSize( hFile, NULL );
if (dwSizeOfFile == 0xFFFFFFFF)
{
return E_FAIL;
}
pReturnBuffer = new unsigned char[dwSizeOfFile];
// Get the binary content of the file
bResult = ::ReadFile( hFile, pReturnBuffer, dwSizeOfFile, &dwNumberOfBytesRead, NULL );
if( FALSE == bResult )
{
return E_FAIL;
}
psaFile = ::SafeArrayCreateVector( VT_UI1 , 0, dwSizeOfFile );
if( !psaFile )
{
return E_FAIL;
}
// Fill in the SAFEARRAY with the binary content of the file
for( k = 0; k < (int) dwSizeOfFile; k++ )
{
if( FAILED(::SafeArrayPutElement( psaFile, &k, &pReturnBuffer[k] )) )
{
return E_FAIL;
}
}
vReturnBuffer.vt = VT_ARRAY | VT_UI1;
V_ARRAY(&vReturnBuffer) = psaFile;
m_piResponse->BinaryWrite(vReturnBuffer);
if( pReturnBuffer )
delete [] pReturnBuffer;
//_variant_t vOut("OutImg TEST....................");
//m_piResponse- >Write(vOut);
::CloseHandle(hFile);
if(lDelFile != 0)
::DeleteFile(bstrFileName);
return SUCCEEDED (hr) ? S_OK : E_FAIL;
return S_OK;
}
此接口我 們使用m_piResponse->BinaryWrite(vReturnBuffer);將讀入內存的圖像數據轉發給用戶 浏覽器。另外,為了靈活地改變圖像字體,大小,字符集及圖像長寬等,我們要為組件添加 以下屬性。
STDMETHOD(get_bAutoHeighten)(/*[out, retval]*/ BOOL *pVal);
STDMETHOD(put_bAutoHeighten)(/*[in]*/ BOOL newVal);
STDMETHOD(get_l3D)(/* [out, retval]*/ long *pVal);
STDMETHOD(put_l3D)(/*[in]*/ long newVal);
STDMETHOD(get_lWeight)(/*[out, retval]*/ long *pVal);
STDMETHOD (put_lWeight)(/*[in]*/ long newVal);
STDMETHOD(get_lTop)(/*[out, retval]*/ long *pVal);
STDMETHOD(put_lTop)(/*[in]*/ long newVal);
STDMETHOD (get_lLeft)(/*[out, retval]*/ long *pVal);
STDMETHOD(put_lLeft)(/*[in]*/ long newVal);
STDMETHOD(get_lCSet)(/*[out, retval]*/ long *pVal);
STDMETHOD(put_lCSet)(/*[in]*/ long newVal);
STDMETHOD(put_bstrBgImg)(/* [in]*/ BSTR newVal);
STDMETHOD(put_bstrFilePath)(/*[in]*/ BSTR newVal);
STDMETHOD(put_bstrFont)(/*[in]*/ BSTR newVal);
STDMETHOD(put_bstrText)(/* [in]*/ BSTR newVal);
STDMETHOD(get_llfHeight)(/*[out, retval]*/ long *pVal);
STDMETHOD(put_llfHeight)(/*[in]*/ long newVal);
STDMETHOD (get_lHeight)(/*[out, retval]*/ long *pVal);
STDMETHOD(put_lHeight)(/*[in]*/ long newVal);
STDMETHOD(get_lWidth)(/*[out, retval]*/ long *pVal);
STDMETHOD(put_lWidth)(/*[in]*/ long newVal);
分別實現自定義圖象長,寬,字 體大小,字體名稱等屬性。如:設置圖像文本
STDMETHODIMP CAspImage::put_bstrText(BSTR newVal)
{
bstrText = newVal;
return S_OK;
}
設置圖像高度
STDMETHODIMP CAspImage::put_lHeight (long newVal)
{
lHeight = newVal;
return S_OK;
}
四、ASP程序使用此組件輸出圖像到用戶浏覽器,在使用之前首先在服務器上注冊此 組件,方法:
拷貝XTAspImage.dll 到系統目錄,一般為C:\WINNT\SYSTEM32
運行-> regsvr32 XTAspImage.dll
如果安裝成功,會有成功提示。此過程只使用一次。下面是在asp裡調用方法:
1. <!--#include file="Config/Function.asp"-->
2. <!-- #include file="Config/SiteInfo.asp"-->
3. <!--#include file="Config/DbConn.asp"-->
4. <%
a) Dim TeachID, PageTextLen, Page, SumPage, BodyLen, \
TemplateBody, Width, Height, FHeight, sEndStr
b) TeachID = INT(Request("TeachID"))
c) PageTextLen = INT(Request("PageTextLen"))
d) IF PageTextLen <= 0 THEN
i. PageTextLen = 300
e) END IF
f) Page = INT(Request ("Page"))
g) Set Rs = Server.CreateObject ("ADODB.Recordset")
h) Sql="select * from Article where id=" & TeachID
i) Rs.open sql,conn,1,1
j) IF NOT Rs.EOF THEN
i. TeachingBody = Rs("Content") & constEndStr
k) ELSE
i. Response.Write("記錄不存在")
ii. Rs.Close
iii. Set Rs = Nothing
iv. Conn.Close
v. Set Conn = Nothing
vi. Response.End
l) END IF
m) BodyLen = len (TeachingBody)
n) SumPage = GetMaxPageNum(BodyLen, PageTextLen)
o) Dim sPageText
p) IF Page >= SumPage THEN
i. Page = SumPage
q) END IF
r) IF Page <= 0 THEN
i. Page = 1
s) END IF
t) sPageText = mid(TeachingBody, (Page-1) * PageTextLen + 1, PageTextLen)
u) Dim sFont
v) sFont = Request("FontFace")
w) Dim FileName
x) FileName = GetTempFileName(Server.MapPath ("tmp"), "XTImg_", "jpg")
y) On Error Resume Next
z) Set Obj = Server.CreateObject("XTAspImage.AspImage")
aa) Response.Clear
bb) If Err.Number <> 0 Then
i. Response.Write "請先在服務器安裝信天ASPIMAGE組件!"
ii. Response.End
cc) End If
dd) Obj.bstrFilePath = FileName
ee) Obj.lHeight = INT(Request("Height"))
ff) Obj.lWidth = INT (Request("Width"))
gg) Obj.bstrFont = sFont
hh) Obj.lLeft = INT(Request("ImgLeft"))
ii) Obj.lTop = INT(Request ("ImgTop"))
jj) Obj.llfHeight = INT(Request ("llfHeight"))
kk) Obj.lWeight = INT(Request ("Weight"))
ll) Obj.l3D = INT(Request("l3D"))
mm) Obj.lCSet = INT(Request("CSet"))
nn) Obj.bstrText = sPageText
oo) IF Obj.OutTextImg = 0 THEN
i. Dim lDelFile ''是 否刪除臨時文件,0為不刪除,非0為刪除
ii. lDelFile = 1
iii. ret = Obj.OutImg(FileName, lDelFile)
iv. ''Response.Write "輸出文件 成功!"
pp) ELSE
i. Response.Write "輸出文件失敗! "
qq) END IF
rr) Set Obj = nothing
5. %>
6. <body>
7. </body>
8. </html>
1,2,3 行為包含 一數據庫連接文件及網站配置信息
4.a 至 4.x 從通過傳入ID號從數據庫裡讀取 文本,並通過字數計算輸出頁要輸出的文本並保存到sPageText裡。
4.y 至 4.nn創建 信天asp 圖像處理組件並設置輸出文件名,圖像長寬,字符集,字體等。
4.oo以後輸 出文件及把圖像數據轉發給用戶浏覽器。
在普通網頁裡的調用方法為:
<img src="outteachimg.asp? TeachID=67&PageTextLen=500&Height=300&Width=600&Page=1&
FontFace=%BB%AA%CE%C4%D0%C2%CE% BA&l3D=0&FontColor=0&CSet=134&BGImgPath=&ImgLeft=10&
ImgTop=20&ImgBottom=10&ImgRight=10&llfHeight=24&Weight=300" ; width="580" >
五、結束語
圖片處理組件在互聯網程序開 發中使用很常見,例如我們注冊論壇會員或商城會員裡,總會有提示輸入驗證碼的提示,而 此驗證碼為了防軟件自動識別,是以圖像數據輸出的。當我們掌握了圖片處理組件開發方法 時,開發基於asp的圖片驗證數字輸出及字符輸出將是輕而易舉。