首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> 關於VC++

利用 VC++ 開發 ASP 影像處理元件 (二)

     編輯:關於VC++

三、COM 元件介面設計

前文在輸入文字後,程式中建立裝置內容,對輸入文字計算並輸出指定檔名的點陣圖檔案。本設計使用 JPEG 壓縮格式儲存檔案以減少網路傳輸時間(JPEG 壓縮方法超出本文範圍,不再贅述)。

為讓其他語言呼叫此介面,以 COM 元件方式發布此程式,可供 VB、Delphi、PB、ASP 等程式呼叫。一般 COM 元件建立與編譯超出本文範圍,不再解釋。

元件方法定義

STDMETHOD(OutTextImg) (/*[out, retval]*/ long *pVal);
STDMETHOD(OutImgFromText) (
    BSTR bstrFilePath,
    BSTR bstrText,
    BSTR bstrBgImg,
    long lCSet,
    BSTR bstrFont,
    long lWidth,
    long lHeight,
    long lLeft,
    long lTop,
    long llfHeight,
    [in,optional,defaultvalue(500)] long lWeight,
    [in,optional,defaultvalue(0)] long l3D);

STDMETHOD(OutImg) (BSTR bstrFileName, long lDelFile);

分別實現處理文字存為影像、將影像輸出至使用者瀏覽器。

OutImgFromText 實作

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 類別,前文介紹過,此處簡單呼叫。

OutTextImg 實作

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 介面。

OutImg 實作:將影像資料傳送至瀏覽器

STDMETHODIMP CAspImage::OutImg(BSTR bstrFileName, long lDelFile)
{
    _variant_t vReturnBuffer;
    LPSAFEARRAY psaFile;
    HANDLE hFile;
    DWORD dwSizeOfFile, dwNumberOfBytesRead;
    BOOL bResult;
    unsigned char *pReturnBuffer = NULL;
    long k;
    HRESULT hr = S_OK;

    // 開啟現有檔案
    hFile = ::CreateFile(bstrFileName, GENERIC_READ, FILE_SHARE_READ,
        NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    if (hFile == INVALID_HANDLE_VALUE) return E_FAIL;

    dwSizeOfFile = ::GetFileSize(hFile, NULL);
    if (dwSizeOfFile == 0xFFFFFFFF) return E_FAIL;

    pReturnBuffer = new unsigned char[dwSizeOfFile];
    bResult = ::ReadFile(hFile, pReturnBuffer, dwSizeOfFile,
        &dwNumberOfBytesRead, NULL);
    if (FALSE == bResult) return E_FAIL;

    psaFile = ::SafeArrayCreateVector(VT_UI1, 0, dwSizeOfFile);
    if (!psaFile) return E_FAIL;

    // 填充 SAFEARRAY
    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;
    ::CloseHandle(hFile);

    if (lDelFile != 0) ::DeleteFile(bstrFileName);
    return SUCCEEDED(hr) ? S_OK : E_FAIL;
}

使用 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 程式使用此元件輸出影像

使用前先在伺服器註冊元件:

  1. 複製 XTAspImage.dll 至系統目錄(通常 C:\WINNT\SYSTEM32)
  2. 執行 regsvr32 XTAspImage.dll,成功會有提示。此過程僅需一次。

ASP 呼叫範例

<!--#include file="Config/Function.asp"-->
<!--#include file="Config/SiteInfo.asp"-->
<!--#include file="Config/DbConn.asp"-->
<%
Dim TeachID, PageTextLen, Page, SumPage, BodyLen, TemplateBody, Width, Height, FHeight, sEndStr
TeachID = INT(Request("TeachID"))
PageTextLen = INT(Request("PageTextLen"))
If PageTextLen <= 0 Then PageTextLen = 300
Page = INT(Request("Page"))
Set Rs = Server.CreateObject("ADODB.Recordset")
Sql = "select * from Article where id=" & TeachID
Rs.open sql, conn, 1, 1
If NOT Rs.EOF Then
    TeachingBody = Rs("Content") & constEndStr
Else
    Response.Write("記錄不存在")
    Rs.Close: Set Rs = Nothing: Conn.Close: Set Conn = Nothing: Response.End
End If
BodyLen = len(TeachingBody)
SumPage = GetMaxPageNum(BodyLen, PageTextLen)
Dim sPageText
If Page >= SumPage Then Page = SumPage
If Page <= 0 Then Page = 1
sPageText = mid(TeachingBody, (Page-1) * PageTextLen + 1, PageTextLen)
Dim sFont, FileName
sFont = Request("FontFace")
FileName = GetTempFileName(Server.MapPath("tmp"), "XTImg_", "jpg")
On Error Resume Next
Set Obj = Server.CreateObject("XTAspImage.AspImage")
Response.Clear
If Err.Number <> 0 Then
    Response.Write "請先在伺服器安裝信天 ASPIMAGE 元件!"
    Response.End
End If
Obj.bstrFilePath = FileName
Obj.lHeight = INT(Request("Height"))
Obj.lWidth = INT(Request("Width"))
Obj.bstrFont = sFont
Obj.lLeft = INT(Request("ImgLeft"))
Obj.lTop = INT(Request("ImgTop"))
Obj.llfHeight = INT(Request("llfHeight"))
Obj.lWeight = INT(Request("Weight"))
Obj.l3D = INT(Request("l3D"))
Obj.lCSet = INT(Request("CSet"))
Obj.bstrText = sPageText
If Obj.OutTextImg = 0 Then
    Dim lDelFile ' 是否刪除暫存檔,0=不刪除,非0=刪除
    lDelFile = 1
    ret = Obj.OutImg(FileName, lDelFile)
Else
    Response.Write "輸出檔案失敗!"
End If
Set Obj = nothing
%>
<body></body></html>

說明:第 1-3 行包含資料庫連線與網站設定檔。4.a-4.x 從資料庫讀取文字並計算輸出頁面文字存入 sPageText。4.y-4.nn 建立元件並設定輸出檔名、影像長寬、字元集、字體等。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 的圖片驗證碼輸出將輕而易舉。

下載原始碼:https://www.vckbase.com/code/downcode.asp?id=2362

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved