234 lines
4.8 KiB
C++
234 lines
4.8 KiB
C++
// Barcode.cpp : Implementation of CSLNKBarcode
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "Barcode.h"
|
|
#include "..\Barcode\code39.h"
|
|
|
|
// CSLNKBarcode
|
|
|
|
STDMETHODIMP CSLNKBarcode::InterfaceSupportsErrorInfo(REFIID riid)
|
|
{
|
|
static const IID* arr[] =
|
|
{
|
|
&IID_IBarcode
|
|
};
|
|
|
|
for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
|
|
{
|
|
if (InlineIsEqualGUID(*arr[i],riid))
|
|
return S_OK;
|
|
}
|
|
return S_FALSE;
|
|
}
|
|
|
|
STDMETHODIMP CSLNKBarcode::get_Text(BSTR* pVal)
|
|
{
|
|
*pVal = m_text.AllocSysString();
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CSLNKBarcode::put_Text(BSTR newVal)
|
|
{
|
|
m_text = CString(newVal);
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CSLNKBarcode::get_Height(ULONG* pVal)
|
|
{
|
|
*pVal = m_height;
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CSLNKBarcode::put_Height(ULONG newVal)
|
|
{
|
|
m_height = newVal;
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
HRESULT CSLNKBarcode::StreamPNG(CxImage &img, VARIANT *ImageData, BOOL bPNG)
|
|
{
|
|
SAFEARRAY *psaData; BYTE *pData;
|
|
SAFEARRAYBOUND rgsabound[1];
|
|
|
|
long size=0;
|
|
BYTE* pBuffer=0;
|
|
{
|
|
{
|
|
CmyTimer timer("CBarcode::StreamPNG/de PNG encoden");
|
|
if (!img.Encode(pBuffer,size, bPNG?CXIMAGE_FORMAT_PNG:CXIMAGE_FORMAT_GIF))
|
|
return myAtlReportError (GetObjectCLSID(), "\nStreamPNG encode failed: %s", img.GetLastError());
|
|
}
|
|
|
|
// TODO: Echt netjes zou zijn om hier een IStream op te leveren?
|
|
// create safe array and copy image data
|
|
rgsabound[0].lLbound = 0; rgsabound[0].cElements = size;
|
|
psaData = SafeArrayCreate(VT_UI1, 1, rgsabound);
|
|
SafeArrayAccessData(psaData, (void **)&pData);
|
|
memcpy(pData,pBuffer,size);
|
|
SafeArrayUnaccessData(psaData);
|
|
img.FreeMemory (pBuffer);
|
|
// put data in variant
|
|
ImageData->vt = (VT_ARRAY | VT_UI1);
|
|
ImageData->parray = psaData;
|
|
}
|
|
myDoTRACE("\nCBarcode::StreamPNG done, sending %d bytes", size);
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
bool CSLNKBarcode::CreateCxImage(CxImage *img)
|
|
{
|
|
CCode39 oBarcode;
|
|
|
|
if (m_height < 0)
|
|
m_height = 200;
|
|
if (m_narrow < 0)
|
|
{
|
|
if (m_wide > 0)
|
|
m_narrow = myRound((double)m_wide / 3);
|
|
else
|
|
m_narrow = m_height / 50;
|
|
}
|
|
if (m_wide < 0)
|
|
m_wide = m_narrow * 3;
|
|
|
|
//----
|
|
HDC pDC = ::GetDC(0);
|
|
HDC myDC = CreateCompatibleDC(pDC);
|
|
|
|
oBarcode.LoadData(myDC, m_text, m_height, m_narrow, m_wide);
|
|
m_width = oBarcode.GetBarcodePixelWidth();
|
|
|
|
BITMAPINFO bmInfo;
|
|
memset(&bmInfo.bmiHeader,0,sizeof(BITMAPINFOHEADER));
|
|
bmInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
|
|
bmInfo.bmiHeader.biWidth=m_width;
|
|
bmInfo.bmiHeader.biHeight=m_height;
|
|
bmInfo.bmiHeader.biPlanes=1;
|
|
bmInfo.bmiHeader.biCompression=BI_RGB; // Geen compressie
|
|
bmInfo.bmiHeader.biBitCount=24;
|
|
|
|
//create a new bitmap and select it in the memory dc
|
|
BYTE *pbase;
|
|
HBITMAP TmpBmp = CreateDIBSection(pDC, &bmInfo,DIB_RGB_COLORS,(void**)&pbase,0,0);
|
|
//----
|
|
|
|
if (!TmpBmp)
|
|
{
|
|
myDoTRACE("\nTmpBmp==NULL when creating (%d, %d)??", m_width, m_height);
|
|
return false;
|
|
}
|
|
|
|
HGDIOBJ TmpObj=SelectObject(myDC,TmpBmp);
|
|
|
|
oBarcode.DrawBitmap();
|
|
|
|
if (!img->CreateFromHBITMAP(TmpBmp))
|
|
{
|
|
myDoTRACE("CreateFromHBITMAP faalt??");
|
|
return false;
|
|
}
|
|
|
|
if (m_rotation%360 == 90 || m_rotation%360 == -270)
|
|
img->RotateLeft();
|
|
|
|
if (m_rotation%360 == -90 || m_rotation%360 == 270)
|
|
img->RotateRight();
|
|
|
|
if (m_rotation == -180 || m_rotation == 180)
|
|
img->Rotate180();
|
|
|
|
//img->Rotate(m_lRotation);
|
|
|
|
DeleteObject(TmpBmp);
|
|
DeleteDC(myDC);
|
|
ReleaseDC( NULL, pDC ); //Do not forget!
|
|
|
|
return true;
|
|
}
|
|
|
|
STDMETHODIMP CSLNKBarcode::GetAsPNG(VARIANT* pVal)
|
|
{
|
|
try
|
|
{
|
|
myDoTRACE("\nCBarcode::GetAsPNG()");
|
|
|
|
CxImage img;
|
|
BOOL res = CreateCxImage(&img);
|
|
if (!res)
|
|
{
|
|
CString err; err.Format("Could not CreateCxImage: %s", img.GetLastError());
|
|
throw err;
|
|
}
|
|
|
|
return StreamPNG(img, pVal, true /*bPNG*/);
|
|
}
|
|
catch (DWFException& ex)
|
|
{
|
|
CString err;
|
|
err.Format("%ls\n%ls\n%s\n%ls(%d)",ex.type(),ex.message(),ex.function(),ex.file(),ex.line());
|
|
return myAtlReportError (GetObjectCLSID(), "\nCBarcode::GetAsPNG('%s')\n%s", m_text, err);
|
|
}
|
|
catch (CString& err)
|
|
{
|
|
return myAtlReportError (GetObjectCLSID(), "\nCBarcode::GetAsPNG('%s')\n%s", m_text, err);
|
|
}
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
|
|
STDMETHODIMP CSLNKBarcode::get_Narrow(ULONG* pVal)
|
|
{
|
|
*pVal = m_narrow;
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CSLNKBarcode::put_Narrow(ULONG newVal)
|
|
{
|
|
m_narrow = newVal;
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CSLNKBarcode::get_Wide(ULONG* pVal)
|
|
{
|
|
*pVal = m_wide;
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CSLNKBarcode::put_Wide(ULONG newVal)
|
|
{
|
|
m_wide = newVal;
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CSLNKBarcode::get_Width(ULONG* pVal)
|
|
{
|
|
*pVal = m_width;
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CSLNKBarcode::get_Rotation(LONG* pVal)
|
|
{
|
|
*pVal = m_rotation;
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CSLNKBarcode::put_Rotation(LONG newVal)
|
|
{
|
|
m_rotation = newVal;
|
|
|
|
return S_OK;
|
|
}
|