现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

VC++实现在图片上显示数字

2013-12-08 16:05 工业·编程 ⁄ 共 9076字 ⁄ 字号 暂无评论

本文所讲只适合于RGB的位图。

一、这里介绍一个类SBCT_CManageImage,通过他可以将数据显示在图片上。

用法是:

先调用GetImageBaseInfo把要画的图的image传进来,然后调用GetNumAndShow来显示数字要位图上。

SBCT_CManageImage sbct_CManageImage; 

sbct_CManageImage.GetImageBaseInfo 

sbct_CManageImage.GetNumAndShow 

二、原理

如下图所示,通过七个位图来控制显示出9个数字。

结合以下代码,我们以ShowEnable1为例,我们会调用AttachData,而AttachData就是把每个RGB像素点替换掉BufImage相应位置的数据。

以下代码为SBCT_CManageImage 类的头文件SBCT_ManageImage.h

// SBCT_ManageImage.h: interface for the SBCT_CManageImage class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MANAGEIMAGE_H__A20FC3CF_281F_4219_B0C7_FF400768C5D8__INCLUDED_)
#define AFX_MANAGEIMAGE_H__A20FC3CF_281F_4219_B0C7_FF400768C5D8__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

///////User edit/////////
#include <atlbase.h>

#ifdef SBT_WriteNumOnImage_SDK
#define SBCT_NumImage_EXPORTMDOE __declspec(dllexport)
#else
#define SBCT_NumImage_EXPORTMDOE __declspec(dllimport)
#endif

//////////////////////// RGB DATA STRUCT DEFINATION //////////////////////

typedef struct _STRU_COLOUR
{
    BYTE blue;
    BYTE green;
    BYTE red;
    BYTE A;
}STRU_COLOUR;

//////////////////////// Class of SBCT_CManageImage//////////////////////
class SBCT_NumImage_EXPORTMDOE SBCT_CManageImage 
{
public:
        SBCT_CManageImage();
    virtual ~SBCT_CManageImage();

private:
    void ShowEnable7(bool trueorfalse);
    void ShowEnable6(bool trueorfalse);
    void ShowEnable5(bool trueorfalse);
    void ShowEnable4(bool trueorfalse);
    void ShowEnable3(bool trueorfalse);
    void ShowEnable2(bool trueorfalse);
    void ShowEnable1(bool trueorfalse);

    void AttachData(int i,int j,_STRU_COLOUR RGB);
    void ShowNumChar(int px,int py,int num,int height,int width,int bold,STRU_COLOUR RGB);
    int Numpx,Numpy,Numheight,Numwidth,Numbold;
    STRU_COLOUR NumRGB;
    BYTE * g_BufImage;
    int g_ImageHeight,g_ImageWidth,g_ImageNByte;

 

public:
    void GetNumAndShow(int PositionX,int PositionY,CComBSTR bstrnum);
    void GetNumAndShow(int PositionX, int PositionY, CComBSTR bstrnum,int Height, int Width, int Bold, int Interval, STRU_COLOUR RGB);
    void GetImageBaseInfo(BYTE *Image,int Height,int Width,int nByte);

};

#endif // !defined(AFX_MANAGEIMAGE_H__A20FC3CF_281F_4219_B0C7_FF400768C5D8__INCLUDED_)

SBCT_CManageImage.cpp

// SBCT_ManageImage.cpp: implementation of the SBCT_CManageImage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SBCT_ManageImage.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SBCT_CManageImage::SBCT_CManageImage()
{

}

SBCT_CManageImage::~SBCT_CManageImage()
{

}

void SBCT_CManageImage::GetImageBaseInfo(BYTE *Image, int Height, int Width, int nByte)
{
   
    g_ImageHeight = Height;
    g_ImageWidth = Width;
    g_ImageNByte = nByte;
    g_BufImage = Image;
}

void SBCT_CManageImage::GetNumAndShow(int PositionX, int PositionY, CComBSTR bstrnum,int Height, int Width, int Bold, int Interval,STRU_COLOUR RGB)
{
    CString str = bstrnum;
    int strlength = str.GetLength();
    int position=0;
    int px = PositionX, py = PositionY;
    int height=Height,width=Width,bold=Bold,interval=Interval;
    STRU_COLOUR Rgb =RGB;

    while(strlength--)
    {
       int num = (int)str.GetAt(position++)-48;

       if(px+width >= g_ImageWidth)
       {
           px = 0;
           py = py+Height+4 ;                //the line interval is 4 pot
           if(py >=g_ImageHeight) py =0;
       }
          
      ShowNumChar(px,py,num,height,width,bold,Rgb);

       px = px+width+interval;
     
    }  //while(strlength--)

}

void SBCT_CManageImage::GetNumAndShow(int PositionX, int PositionY, CComBSTR bstrnum)
{
    CString str = bstrnum;
    int strlength = str.GetLength();
    int position=0;
    int px = PositionX, py = PositionY;

    int height=21,width=15,bold=3,interval=5;
    STRU_COLOUR Rgb;
       Rgb.red = (BYTE) 255;
       Rgb.green =(BYTE) 0;
       Rgb.blue = (BYTE) 0;
       if(g_ImageNByte==4)
          Rgb.A   = 255;

    while(strlength--)
    {
       int num = (int)str.GetAt(position++)-48;

       if(px + width>= g_ImageWidth)
       {
           px = 0;
           py = py+height+4 ;                //the line interval is 4 pot
           if(py >= g_ImageHeight) py =0;
       }
          
      ShowNumChar(px,py,num,height,width,bold,Rgb);

      px = px+width+interval;
     
    }  //while(strlength--)

 
}

void SBCT_CManageImage::ShowNumChar(int px,int py,int num, int height, int width,int bold, STRU_COLOUR RGB)
{  
   
    Numpx=px;Numpy=py;Numheight=height;Numwidth=width;Numbold=bold;
    NumRGB=RGB;
    switch(num)
       {
       case 0:
           {
               ShowEnable1(true);
               ShowEnable2(true);
               ShowEnable3(true);
               ShowEnable4(true);
               ShowEnable5(true);
               ShowEnable6(true);
               ShowEnable7(false);
               break;
           }
       case 1:
           {
               ShowEnable1(false);
               ShowEnable2(true);
               ShowEnable3(true);
               ShowEnable4(false);
               ShowEnable5(false);
               ShowEnable6(false);
               ShowEnable7(false);
               break;
           }
       case 2:
           {
               ShowEnable1(true);
               ShowEnable2(true);
               ShowEnable3(false);
               ShowEnable4(true);
               ShowEnable5(true);
               ShowEnable6(false);
               ShowEnable7(true);
               break;
           }
       case 3:
           {
               ShowEnable1(true);
               ShowEnable2(true);
               ShowEnable3(true);
               ShowEnable4(true);
               ShowEnable5(false);
               ShowEnable6(false);
               ShowEnable7(true);
               break;
           }
       case 4:
           {
               ShowEnable1(false);
               ShowEnable2(true);
               ShowEnable3(true);
               ShowEnable4(false);
               ShowEnable5(false);
               ShowEnable6(true);
               ShowEnable7(true);
               break;
           }
       case 5:
           {
               ShowEnable1(true);
               ShowEnable2(false);
               ShowEnable3(true);
               ShowEnable4(true);
               ShowEnable5(false);
               ShowEnable6(true);
               ShowEnable7(true);
               break;
           }
       case 6:
           {
               ShowEnable1(true);
               ShowEnable2(false);
               ShowEnable3(true);
               ShowEnable4(true);
               ShowEnable5(true);
               ShowEnable6(true);
               ShowEnable7(true);
               break;
           }
       case 7:
           {
               ShowEnable1(true);
               ShowEnable2(true);
               ShowEnable3(true);
               ShowEnable4(false);
               ShowEnable5(false);
               ShowEnable6(false);
               ShowEnable7(false);
              break;
           }
       case 8:
           {
               ShowEnable1(true);
               ShowEnable2(true);
               ShowEnable3(true);
               ShowEnable4(true);
               ShowEnable5(true);
               ShowEnable6(true);
               ShowEnable7(true);
              break;
           }
       case 9:
           {
               ShowEnable1(true);
               ShowEnable2(true);
               ShowEnable3(true);
               ShowEnable4(true);
               ShowEnable5(false);
               ShowEnable6(true);
               ShowEnable7(true);
               break;
           }
    }//switch(num)
}

void SBCT_CManageImage::AttachData(int i, int j,STRU_COLOUR RGB)
{  
    int offset=(i*g_ImageWidth+j)*g_ImageNByte;
   
     BYTE* pTmpBuffer = g_BufImage + offset;

    if(g_ImageNByte==4)  memcpy(pTmpBuffer,&RGB,4);
      else     memcpy(pTmpBuffer,&RGB,3);
   
}

void SBCT_CManageImage::ShowEnable1(bool trueorfalse)

    if(trueorfalse)
        for(int i= Numpy;i<Numpy+Numbold;i++)
                   for(int j=Numpx;j<Numpx+Numwidth;j++)
                      AttachData(i,j,NumRGB);

}

void SBCT_CManageImage::ShowEnable2(bool trueorfalse)
{
    if(trueorfalse)
           for(int i=Numpy;i<Numpy+Numheight/2+Numbold/2;i++)
                   for(int j=Numpx+Numwidth-Numbold;j<Numpx+Numwidth;j++)
                       AttachData(i,j,NumRGB);

}

void SBCT_CManageImage::ShowEnable3(bool trueorfalse)
{
    if(trueorfalse)
        for(int i=Numpy+Numheight/2-Numbold/2;i<Numpy+Numheight;i++)
             for(int j=Numpx+Numwidth-Numbold;j<Numpx+Numwidth;j++)
                       AttachData(i,j,NumRGB);

}

void SBCT_CManageImage::ShowEnable4(bool trueorfalse)
{
    if(trueorfalse)
          for(int i=Numpy+Numheight-Numbold;i<Numpy+Numheight;i++)
                   for(int j=Numpx;j<Numpx+Numwidth;j++)
                       AttachData(i, j,NumRGB);

}

void SBCT_CManageImage::ShowEnable5(bool trueorfalse)
{
    if(trueorfalse)
        for(int i=Numpy+Numheight/2-Numbold/2;i<Numpy+Numheight;i++)
             for(int j=Numpx;j<Numpx+Numbold;j++)
                       AttachData(i,j,NumRGB);

}

void SBCT_CManageImage::ShowEnable6(bool trueorfalse)
{
    if(trueorfalse)
        for(int i=Numpy;i<Numpy+Numheight/2+Numbold/2;i++)
                   for(int j=Numpx;j<Numpx+Numbold;j++)
                       AttachData(i, j,NumRGB);

}

void SBCT_CManageImage::ShowEnable7(bool trueorfalse)
{
    if(trueorfalse)
        for(int i=Numpy+Numheight/2-Numbold/2;i<Numpy+Numheight/2+Numbold-Numbold/2;i++)
                   for(int j=Numpx;j<Numpx+Numwidth;j++)
                       AttachData(i,j,NumRGB);

}

给我留言

留言无头像?