我是靠谱客的博主 愉快雨,最近开发中收集的这篇文章主要介绍C#实现字符字模提取,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

*/ --------------------------------------------------------------------------------------
*/ 出自:
http://hi.baidu.com/xiaoxichun http://hi.csdn.net/xiaochun1413
*/ 作者: simon

*/ 时间: 2008-12-16

*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace bitmap字模
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public enum DataMode { hanzi, zimu }
        public enum geshisize { 十六, 二十四, 三十二, 四十八 };
        private int[] hanzishuzu = { 32, 72, 128, 288 };
        private int[] hanzisize = { 16, 24, 32, 48 };
        public geshisize geshi
        {
            get
            {
                if (radioButton_16.Checked) return geshisize.十六;
                else if (radioButton_24.Checked) return geshisize.二十四;
                else if (radioButton_32.Checked) return geshisize.三十二;
                else return geshisize.四十八;

            }

        }
        public Bitmap GetCharBMP(string str, int size)
        {
            StringFormat sf = new StringFormat(); // 设置格式
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Near;
            Bitmap bmp = new Bitmap(size, size); // 新建位图变量
            Graphics g = Graphics.FromImage(bmp);
            g.DrawString(str, new Font("宋体", size * 3 / 4), Brushes.Black, new Rectangle(0, 0, size, size), sf); // 向图像变量输出字符
            return bmp;
        }
        StringBuilder sb = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        public void CreateCharSetFile(string filePath, int size)
        {
            int k=0;
            int count = 0;
            byte[] data = new byte[size];
            byte temp = 0;
          

            foreach (char ch in filePath)
            {
                int mode=(int)ch;
                DataMode dataMode;
                if (0x4e00 <= mode && mode <= 0x9fa5)
                    dataMode = DataMode.hanzi;
                else
                    dataMode = DataMode.zimu;
                    Bitmap bmp = GetCharBMP(ch.ToString(), hanzisize[(int)geshi]); // 获取待分析的字符位图
           
               
                for (int i = 0; i < bmp.Height; i++)
                {
                  
                    for (int j = 0; j < bmp.Width; j++)
                    {
                       
                        if (bmp.GetPixel(j, i) == Color.FromArgb(0, 0, 0))// 以下几行根据点阵格式计算它的十六进制并写入
                             temp += (byte)Math.Pow(2, (7-j%8));//横向取模大~小
                          

                        if (j % 8 == 7)
                        {
                            count++;
                            if (temp.ToString("x").Length == 2)
                                sb.Append("0x" + temp.ToString("x"));
                            else sb.Append("0x0" + temp.ToString("x"));
                            //if (!(j == bmp.Width - 1 && i == bmp.Height - 1 && ch == filePath[filePath.Length - 1]))
                              sb.Append(",");
                             
                            data[i * (bmp.Width/8) + k] =temp;
                            temp = 0;
                            if (count == 4)
                            {
                                sb.Append("/r/n");//控制每行输出字节数
                                count = 0;
                            }

                             k++;
                        }
                      
                    }
                    k = 0;
                  
                   
                }
                sb.Append("/r/n");//输出一个字符换行
                for (int l = 0; l < bmp.Height; l++)
                {
                    //                   
                    for (int m = 0; m < bmp.Width/8 ; m++)
                    {

                        byte btCode = data[l * (bmp.Width/8) + m];

                        //按字节输出每点的数据。
                        for (int n = 0; n < 8; n++)
                        {
                            if ((btCode& (0x80 >> n))!=0 )
                            {
                                sb2.Append("1");
                            }
                            else
                            {
                                sb2.Append("0");
                            }

                        }

                    }
                    sb2.Append("/r/n");

                }
            }
            //sb.Append("};");
            //sw.Close();
          
          
        }
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            sb.Remove(0,sb.Length);
            sb2.Remove(0,sb2.Length);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            sb.Remove(0, sb.Length);
            sb2.Remove(0, sb2.Length);
            picBox.SizeMode = PictureBoxSizeMode.Zoom;
            string asd = comboBox1.Text;
            picBox.Image = GetCharBMP(asd, hanzisize[(int)geshi]);
            CreateCharSetFile(asd, hanzishuzu[(int)geshi]);
            textBox1.Text = sb.ToString();
            textBox1.Text+= sb2.ToString();
           
        }
    }
}

/**********************以下为实例输出的文本***************************************/

0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x08,0x00,
0x00,0x00,0x00,0x00,
0x0e,0x00,0x00,0x00,
0x00,0x00,0x0e,0x00,
0x00,0x00,0x00,0x00,
0x0e,0x00,0x00,0x00,
0x00,0x00,0x1e,0x00,
0x02,0x00,0x00,0x00,
0x1c,0x00,0x07,0x00,
0x0f,0xff,0xff,0xff,
0xff,0x80,0x04,0x00,
0x1c,0x00,0x00,0x00,
0x00,0x00,0x18,0x00,
0x00,0x00,0x00,0x00,
0x38,0x00,0x00,0x00,
0x00,0x00,0x38,0x00,
0x30,0x00,0x00,0x00,
0x30,0x00,0x78,0x00,
0x03,0xff,0xff,0xff,
0xfc,0x00,0x00,0x00,
0x70,0x00,0x00,0x00,
0x00,0x00,0xe0,0x00,
0x00,0x00,0x00,0x00,
0xe0,0x00,0x00,0x80,
0x00,0x01,0xc0,0x00,
0x01,0xc0,0x00,0x01,
0xc0,0x00,0x03,0xe0,
0x3f,0xff,0xff,0xff,
0xff,0xf0,0x00,0x07,
0x00,0x06,0x00,0x00,
0x00,0x07,0x00,0x02,
0x00,0x00,0x00,0x0e,
0x00,0x01,0x00,0x00,
0x00,0x1c,0x00,0x01,
0x80,0x00,0x00,0x38,
0x00,0x02,0xe0,0x00,
0x00,0x74,0x00,0x03,
0x70,0x00,0x01,0xc7,
0xff,0xff,0xbc,0x00,
0x03,0x86,0x00,0x07,
0xdf,0x00,0x06,0x06,
0x00,0x07,0x0f,0xf8,
0x18,0x06,0x00,0x07,
0x03,0xf0,0x60,0x06,
0x00,0x07,0x01,0xc0,
0x00,0x06,0x00,0x07,
0x00,0x40,0x00,0x06,
0x00,0x07,0x00,0x00,
0x00,0x07,0xff,0xff,
0x00,0x00,0x00,0x06,
0x00,0x07,0x00,0x00,
0x00,0x06,0x00,0x07,
0x00,0x00,0x00,0x06,
0x00,0x07,0x00,0x00,
0x00,0x06,0x00,0x07,
0x00,0x00,0x00,0x06,
0x00,0x07,0x00,0x00,
0x00,0x06,0x00,0x07,
0x00,0x00,0x00,0x07,
0xff,0xff,0x00,0x00,
0x00,0x06,0x00,0x07,
0x00,0x00,0x00,0x06,
0x00,0x07,0x00,0x00,
0x00,0x06,0x00,0x07,
0x00,0x00,0x00,0x08,
0x00,0x04,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,

000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000
000000000000000000001000000000000000000000000000
000000000000000000001110000000000000000000000000
000000000000000000001110000000000000000000000000
000000000000000000001110000000000000000000000000
000000000000000000011110000000000000001000000000
000000000000000000011100000000000000011100000000
000011111111111111111111111111111111111110000000
000001000000000000011100000000000000000000000000
000000000000000000011000000000000000000000000000
000000000000000000111000000000000000000000000000
000000000000000000111000000000000011000000000000
000000000000000000110000000000000111100000000000
000000111111111111111111111111111111110000000000
000000000000000001110000000000000000000000000000
000000000000000011100000000000000000000000000000
000000000000000011100000000000000000000010000000
000000000000000111000000000000000000000111000000
000000000000000111000000000000000000001111100000
001111111111111111111111111111111111111111110000
000000000000011100000000000001100000000000000000
000000000000011100000000000000100000000000000000
000000000000111000000000000000010000000000000000
000000000001110000000000000000011000000000000000
000000000011100000000000000000101110000000000000
000000000111010000000000000000110111000000000000
000000011100011111111111111111111011110000000000
000000111000011000000000000001111101111100000000
000001100000011000000000000001110000111111111000
000110000000011000000000000001110000001111110000
011000000000011000000000000001110000000111000000
000000000000011000000000000001110000000001000000
000000000000011000000000000001110000000000000000
000000000000011111111111111111110000000000000000
000000000000011000000000000001110000000000000000
000000000000011000000000000001110000000000000000
000000000000011000000000000001110000000000000000
000000000000011000000000000001110000000000000000
000000000000011000000000000001110000000000000000
000000000000011000000000000001110000000000000000
000000000000011111111111111111110000000000000000
000000000000011000000000000001110000000000000000
000000000000011000000000000001110000000000000000
000000000000011000000000000001110000000000000000
000000000000100000000000000001000000000000000000
000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000

 

最后

以上就是愉快雨为你收集整理的C#实现字符字模提取的全部内容,希望文章能够帮你解决C#实现字符字模提取所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部