利用VC++开发ASP图像处理组件(2)


三、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的图片验证数字输出及字符输出将是轻而易举

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3