织梦CMS - 轻松建站从此开始!

罗索

C语言实现图片插入汉字

jackyhwei 发布于 2011-12-08 14:26 点击:次 
一段直接将汉字(英文)插入某一图片中的C实现代码。
TAG:

#include <iostream>
#include <stdlib.h>

/*
part one,14 bit
*/
struct head
{
    int bfType;          //位图类型
    long int bfsize;     //位图文件大小
    int freserved1;     //必须为0
    int freserved2;     //必须为0
    long int bfoffbits; //第54个开始
}bmp_head;

/*
part,two, 40 bit
*/
struct info
{
    long int bisize;        //本结构站用的字节数 40
    long biwidth;           //位图的宽度
    long biheight;          //位图的高度
    int biplanes;           //目标设备的级别
    int BIBITCOUNT;         //每个象素所需的位数
    long int bigcompression;//位图压缩类型
    long int bisizeimage;   //位图的大小
    long bixpelspermeter;
    long biypelspermeter;
    long int biclrused;
    long int biclrimportant;
}bmp_info;

using namespace std;


//unsigned char meng[30]={""};
//unsigned char kai[30]={""};

unsigned char data[8][40]={""};

unsigned int cnt=0;

int arry_length = 8;

unsigned char buf[50]={""};

/*
    读入文件   
*/
void read(char* txt)
{
    FILE *fp;
    //int i_1,j_1;
    //int i_2,j_2;
    int x[10]={};
    int y[10]={};
   
    if(( fp = fopen(txt,"rb")) = NULL)
    {
        perror("Input file open error");
        exit(1);
    }
    else
    {
        fp = fopen(txt,"rb");
    }
  
    for(int i=0 ;i<cnt/2 ;i++)
    {
        x[i] = buf[2*i] - 0xa0;        //计算第一个字的区码
        y[i] = buf[2*i+1] - 0xa0;        //计算第一个字的位码

        fseek(fp,(94*(x[i]-1)+(y[i]-1))*32,0);    //把指针移到HZK中目标字的偏移位
        fread(data[i],32,1,fp);                    //读取数据
    }
       
    //rewind(fp);                             //指针回到启始的位置
   
    fclose(fp);
}

/*
    从文件中读取想要转化的文字
*/
void readfile(char* txt)
{
    FILE *fp;
    char ch;
   
    if(( fp = fopen(txt,"rb")) = NULL)
    {
        perror("Input file open error");
        exit(1);
    }
    else
    {
        fp = fopen(txt,"rb");
    }

    while(!feof(fp))
    {   
        ch=fgetc(fp);
        cnt++;
    }     
    cnt=(cnt-1);
    fseek(fp,0,0);
    fread(buf,cnt,1,fp);
   
    fclose(fp);
}

/*
    把取得的16进制的编码进行转换,并打引出来
*/
void insert(long bmp_infosize,unsigned char* buff)
{
    int s;
    int *x;                         //x数组用来存放比较的结果
    s = bmp_infosize-1;
    x = (int*)malloc(sizeof(int)*arry_length);
    char y[40] = {""};
    int offset_1 = 0;
    int offset_2 = 0;
    int offset_3 = 0;

for(int time=0 ;time<cnt/2 ;time++)
{
    for(int k=1 ;k<32 ;k++)
    {
        int iMask = 1;                   //iMask是比较的数据
        for(int i = 0; i<8; i++)   
        {
            x[i] = data[time][(k-1)] & iMask;        //进行或运算
            iMask = iMask<<1;              //把比较位向左移
        }
           
        for(int i=7 ;i>=0 ;i--)
        {   
            if( x[i] == 0 )
            {
                printf(" ");
            }
            else
            {
                buff[s-3*320+(7-i)*3-offset_1+offset_2+offset_3] = 0x00;
                buff[s-3*320+(7-i)*3+1-offset_1+offset_2+offset_3] = 0x00;
                buff[s-3*320+(7-i)*3+2-offset_1+offset_2+offset_3] = 0x00;   
                buff[s-3*320+(7-i)*3+3-offset_1+offset_2+offset_3] = 0x00;
                   
                    printf("* ");
            }   
        }
        if(k%2 == 0)
        {
            printf("\n");
            offset_1 = offset_1+320*3;
            offset_2=0;
        }
        else
        {
            offset_2 = 7*3;
        }       
    }  
//offset_3+=3*16;
}
offset_3+=3*16;
}


int main(int argc, char *argv[])
{
    readfile("in.txt");
    read("HZK16");
   
    FILE *fp;
    const int bitcount = 3;
    int x1,y1,x2,y2;
    long bmp_size,bmp_infosize,size;
    unsigned char *temp,*buff;
    int offset=0;
       
    if(( fp = fopen("7.bmp","rb")) = NULL)    //open a picture
    {
        perror("Input file open error");
        exit(1);
    }
    else
    {
        fp = fopen("7.bmp","a+");
    }

    fread( &bmp_head ,14 ,1 ,fp );
    fread( &bmp_info ,40 ,1 ,fp );

    x1=0,y1=0,x2=320,y2=240;
       
    bmp_infosize = (x2-x1+3)/4*4*(y2-y1)*bitcount;
    bmp_size = bmp_infosize+54;
   
    bmp_head.bfsize = bmp_size;
    bmp_info.biwidth = (x2-x1+3)/4*4;
    bmp_info.biheight = y2-y1;
    bmp_info.bisizeimage = bmp_infosize;
   
    size=bmp_size;
    temp = (unsigned char*)malloc((size-54)*sizeof(char));   //read information
    fread( temp ,size-54 ,1 ,fp );
    
    buff = (unsigned char*)malloc( bmp_infosize * sizeof(int) );
       
    fseek(fp,54,0);                  
    fseek(fp,320*bitcount*(240-y2),1);

    for(int i=0 ;i<y2-y1 ;i++)
    {
        fseek( fp ,x1*bitcount ,1 );        //把指针向后移动到x1
        fread( &buff[i+offset] ,bmp_info.biwidth*bitcount ,1 ,fp );//读取图片宽度*4个长度
        fseek( fp ,(320-x2)*bitcount ,1 );                //把指针向后移动到320
        offset = offset+bmp_info.biwidth*bitcount-1;      //把 offset增加 图片宽度*4个长度
    }
   
    fclose(fp);
       
    insert(bmp_infosize,buff);
                
    fp = fopen("8.bmp","a+");          //construct a new picture
     
    fwrite( &bmp_head ,14 ,1 ,fp );
    fwrite( &bmp_info ,40 ,1 ,fp );    
    fwrite( buff ,bmp_infosize ,1 ,fp );

    fclose(fp);

    system("PAUSE");
    return 0;
}

(qqeb)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201112/15437.html]
本文出处:hi.baidu.com/qqeb 作者:qqeb 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容