www.pudn.com > PKEncodeDemo.zip > SampleDoc.cpp


// SampleDoc.cpp : implementation of the CSampleDoc class 
// 
 
#include "stdafx.h" 
#include "Sample.h" 
 
 
#include "SampleDoc.h" 
#include "EncodeDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CSampleDoc 
 
IMPLEMENT_DYNCREATE(CSampleDoc, CDocument) 
 
BEGIN_MESSAGE_MAP(CSampleDoc, CDocument) 
	//{{AFX_MSG_MAP(CSampleDoc) 
	ON_COMMAND(ID_ENCODE, OnEncode) 
	ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_AS, OnUpdateFileSaveAs) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSampleDoc construction/destruction 
 
CSampleDoc::CSampleDoc() 
{ 
	// TODO: add one-time construction code here 
	PtInitImage( &m_image ) ; 
} 
 
CSampleDoc::~CSampleDoc() 
{ 
	PtFreeImage( &m_image ); 
} 
 
BOOL CSampleDoc::OnNewDocument() 
{ 
	if (!CDocument::OnNewDocument()) 
		return FALSE; 
	 
	// TODO: add reinitialization code here 
	// (SDI documents will reuse this document) 
    PtFreeImage( &m_image );//free the memory storing the last encoding image  
	return TRUE; 
} 
 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CSampleDoc serialization 
 
void CSampleDoc::Serialize(CArchive& ar) 
{ 
	if (ar.IsStoring()) 
	{ 
		// TODO: add storing code here 
	} 
	else 
	{ 
		// TODO: add loading code here 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CSampleDoc diagnostics 
 
#ifdef _DEBUG 
void CSampleDoc::AssertValid() const 
{ 
	CDocument::AssertValid(); 
} 
 
void CSampleDoc::Dump(CDumpContext& dc) const 
{ 
	CDocument::Dump(dc); 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CSampleDoc commands 
 
void CSampleDoc::OnEncode()  
{ 
	static CEncodeDlg dlg; 
	static BOOL isFirst=TRUE; 
	PTPDF417ENCODE  barEncode; 
	PtPDF417EncodeInit( &barEncode ); 
    PtFreeImage( &m_image );//free the memory storing the last encoding image  
	if( isFirst ) 
	{ 
		dlg.m_radioAspect = 0;//use aspect w:H 
		dlg.m_isPaste = FALSE;//don't paste;  
		dlg.m_percent = barEncode.wEccPercent; 
		dlg.m_level = barEncode.wEccLevel; 
		dlg.m_cols = barEncode.wCols; 
        dlg.m_rows = barEncode.wRows; 
		dlg.m_aspectH = barEncode.wAspectHeigh; 
		dlg.m_aspectW = barEncode.wAspectWidth; 
		dlg.m_xModule = barEncode.wXModule; 
		dlg.m_moduleRatio = barEncode.wModuleAspectRatio; 
		dlg.m_isTruncated = barEncode.bIsTruncated; 
		dlg.m_leftSpace = barEncode.wLeftSpace; 
		dlg.m_rightSpace = barEncode.wRightSpace; 
		dlg.m_topSpace = barEncode.wTopSpace; 
		dlg.m_bottomSpace = barEncode.wBottomSpace; 
		dlg.m_data = "Hello World!"; 
		isFirst = FALSE; 
	} 
	 
	if( dlg.DoModal()!=IDOK )  
	{ 
        UpdateAllViews(NULL); 
        return; 
    } 
	barEncode.wEccPercent = dlg.m_percent; 
	barEncode.wEccLevel = dlg.m_level; 
	if(dlg.m_radioAspect==0) 
    { 
        barEncode.wCols = 0; 
	    barEncode.wRows = 0; 
    } 
    else 
    { 
        barEncode.wRows = dlg.m_rows; 
        barEncode.wCols = dlg.m_cols; 
    } 
    barEncode.wAspectHeigh = dlg.m_aspectH; 
	barEncode.wAspectWidth = dlg.m_aspectW ; 
	barEncode.wXModule = dlg.m_xModule; 
	barEncode.wModuleAspectRatio = dlg.m_moduleRatio; 
	barEncode.bIsTruncated = dlg.m_isTruncated; 
	barEncode.wLeftSpace = dlg.m_leftSpace; 
	barEncode.wRightSpace = dlg.m_rightSpace; 
	barEncode.wTopSpace = dlg.m_topSpace; 
	barEncode.wBottomSpace = dlg.m_bottomSpace; 
	barEncode.pData = (char*)(LPCTSTR )dlg.m_data; 
	barEncode.nDataLength = strlen( barEncode.pData ); 
	 
   /* BYTE data[]={0x44,0x45,0x4d,0x4f,0x0d,0x0a,0x5e,0x5e, 
                 0x43,0x50,0x4b,0x46,0x58,0x2d,0x34,0x30, 
                 0x30};//encode binary data 
	barEncode.pData= (char*)data; 
	barEncode.nDataLength=17;*/ 
     
	 
	if( dlg.m_isPaste == FALSE ) 
	{ 
		if( PtPDF417Encode( &barEncode, &m_image) != PT_PDF417ENCODE_SUCCESS ) 
			::AfxMessageBox( "Encode error!" ); 
		UpdateAllViews(NULL); 
		return; 
	} 
 
	//dlg.m_isPaste == TRUE 
	if( PtLoadImage( (LPCSTR)dlg.m_fileName, &m_image )!=PT_IMAGERW_SUCCESS ) 
		::AfxMessageBox( "fail to load the bmp file "+dlg.m_fileName+"!" ); 
	else 
		if( PtPDF417EncodeToImage( &barEncode, &m_image, dlg.m_startX, dlg.m_startY )!=PT_PDF417ENCODE_SUCCESS ) 
			::AfxMessageBox( "fail to paste the image!" ); 
		 
	UpdateAllViews(NULL); 
	 
} 
 
BOOL CSampleDoc::OnSaveDocument(LPCTSTR lpszPathName)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	if( PtSaveImage( lpszPathName, &m_image )!=PT_IMAGERW_SUCCESS ) 
		::AfxMessageBox ( "Save File Error" ); 
	return TRUE; 
	//return CDocument::OnSaveDocument(lpszPathName); 
} 
 
 
void CSampleDoc::OnUpdateFileSaveAs(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	if(m_image.pBits == NULL) 
		pCmdUI->Enable(FALSE); 
	else 
		pCmdUI->Enable(TRUE); 
} 
 
 
BOOL CSampleDoc::DoSave(LPCTSTR pszPathName, BOOL bReplace ) 
{ 
	if( m_image.pBits == NULL ) 
		return FALSE; 
	CFileDialog dlgFile(FALSE);//read 
	dlgFile.m_ofn.Flags |= (OFN_HIDEREADONLY | OFN_FILEMUSTEXIST); 
	dlgFile.m_ofn.lpstrTitle = "Save image file"; 
	dlgFile.m_ofn.nFilterIndex = 1;	 
 
	CString strFilter = ""; 
	strFilter = strFilter + "BMP files"+(TCHAR)NULL+"*.bmp"+(TCHAR)NULL 
	                      + "TIF files"+(TCHAR)NULL+"*.tif"+(TCHAR)NULL 
						  + "JPG files"+(TCHAR)NULL+"*.jpg"+(TCHAR)NULL 
						  + "PNG files"+(TCHAR)NULL+"*.png"+(TCHAR)NULL+(TCHAR)NULL; 
	dlgFile.m_ofn.lpstrFilter =strFilter; 
	dlgFile.m_ofn.lpstrDefExt = "bmp"; 
 
    if( dlgFile.DoModal() != IDOK ) 
		return FALSE; 
	if( OnSaveDocument(dlgFile.GetPathName()) ) 
	{ 
		SetPathName(dlgFile.GetPathName(), TRUE); 
		return TRUE; 
	} 
	else 
		return FALSE; 
}