当前位置: 代码迷 >> JavaScript >> 发个小东西,仅供玩玩解决办法
  详细解决方案

发个小东西,仅供玩玩解决办法

热度:429   发布时间:2012-02-19 19:43:38.0
发个小东西,仅供玩玩
想自己做个把图片转换为字符画的小软件,原理就是根据图片的每个像素的灰度,转成不同的字符。
所以研究起点阵字库了。
HTML code

<html>
    <head>
        <title></title>
<script language="javascript" type="text/javascript">
    //ASCII点阵字库.
    var fontData=[
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xE0,
0x01,0xE0,0x01,0xE0,0x01,0xE0,0x01,0xE0,0x01,0xE0,0x01,0xE0,0x01,0xE0,0x00,0xC0,
0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0xC0,0x01,0xC0,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x9E,0x0F,0x3C,0x0E,0x38,0x1C,0x70,0x18,0x60,
0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0C,
0x03,0x0C,0x03,0x0C,0x07,0x1C,0x06,0x18,0x06,0x18,0x7F,0xFE,0x7F,0xFE,0x06,0x18,
0x06,0x18,0x0E,0x38,0x0C,0x30,0x0C,0x30,0x7F,0xFE,0x7F,0xFE,0x0C,0x30,0x0C,0x30,
0x1C,0x70,0x18,0x60,0x18,0x60,0x18,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x01,0x80,0x07,0xE0,
0x0F,0xF0,0x1D,0xF0,0x39,0xB8,0x39,0xB8,0x39,0xB8,0x39,0x80,0x3D,0x80,0x1F,0x80,
0x0F,0xC0,0x03,0xE0,0x01,0xF0,0x01,0xF8,0x01,0xB8,0x71,0xB8,0x71,0xB8,0x71,0xB8,
0x39,0xB8,0x3D,0xB0,0x1F,0xF0,0x07,0xE0,0x01,0x80,0x01,0x80,0x01,0x80,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x08,
0x3C,0x10,0x66,0x10,0x66,0x10,0x66,0x20,0x66,0x20,0x66,0x40,0x66,0x40,0x66,0x80,
0x3C,0x80,0x19,0x98,0x01,0x3C,0x01,0x66,0x02,0x66,0x02,0x66,0x04,0x66,0x04,0x66,
0x08,0x66,0x08,0x66,0x08,0x3C,0x10,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x80,
0x0F,0xC0,0x1C,0x60,0x18,0x60,0x18,0x60,0x18,0x60,0x18,0xE0,0x0D,0xC0,0x0F,0x80,
0x0F,0x00,0x1F,0x00,0x3F,0x00,0x3B,0x8E,0x71,0xCE,0x70,0xEE,0x70,0xFC,0x70,0x7C,
0x78,0x3E,0x3C,0x7F,0x1F,0xF6,0x07,0xC2,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x00,
0x1C,0x00,0x1C,0x00,0x0C,0x00,0x08,0x00,0x18,0x00,0x10,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x0E,0x00,0x1C,0x00,0x38,
0x00,0x30,0x00,0x60,0x00,0x60,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x01,0x80,0x01,0x80,
0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x00,0xC0,0x00,0xC0,
0x00,0xC0,0x00,0x60,0x00,0x60,0x00,0x30,0x00,0x38,0x00,0x1C,0x00,0x0E,0x00,0x04,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x70,0x00,0x38,0x00,0x1C,0x00,
0x0C,0x00,0x06,0x00,0x06,0x00,0x03,0x00,0x03,0x00,0x03,0x00,0x01,0x80,0x01,0x80,
0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x03,0x00,0x03,0x00,
0x03,0x00,0x06,0x00,0x06,0x00,0x0C,0x00,0x1C,0x00,0x38,0x00,0x70,0x00,0x20,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0xC0,0x01,0xC0,0x01,0xC0,0x21,0xC2,0x79,0xCF,0x3D,0xDE,
0x1F,0xFC,0x07,0xF0,0x07,0xF0,0x1F,0xFC,0x7D,0xDF,0x79,0xCF,0x21,0xC2,0x01,0xC0,
0x01,0xC0,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,
0x7F,0xFE,0x7F,0xFE,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x1C,0x00,0x1C,0x00,0x1C,0x00,0x0C,0x00,0x18,0x00,0x10,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x7F,0xFE,0x7F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x1C,0x00,0x1C,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01,
0x00,0x02,0x00,0x04,0x00,0x04,0x00,0x08,0x00,0x10,0x00,0x10,0x00,0x20,0x00,0x20,
0x00,0x40,0x00,0x80,0x00,0x80,0x01,0x00,0x02,0x00,0x02,0x00,0x04,0x00,0x04,0x00,
0x08,0x00,0x10,0x00,0x10,0x00,0x20,0x00,0x40,0x00,0x40,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xE0,
0x0F,0xF0,0x1C,0x38,0x38,0x1C,0x38,0x1C,0x38,0x1C,0x70,0x0E,0x70,0x0E,0x70,0x0E,
0x70,0x0E,0x70,0x0E,0x70,0x0E,0x70,0x0E,0x70,0x0E,0x70,0x0E,0x38,0x1C,0x38,0x1C,
0x18,0x1C,0x1C,0x38,0x0F,0xF0,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,
0x00,0xC0,0x01,0xC0,0x03,0xC0,0x1F,0xC0,0x1F,0xC0,0x01,0xC0,0x01,0xC0,0x01,0xC0,
0x01,0xC0,0x01,0xC0,0x01,0xC0,0x01,0xC0,0x01,0xC0,0x01,0xC0,0x01,0xC0,0x01,0xC0,
0x01,0xC0,0x01,0xC0,0x01,0xC0,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xE0,
0x07,0xF0,0x0E,0x38,0x0E,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x00,0x3C,
0x00,0x38,0x00,0x70,0x00,0xE0,0x01,0xC0,0x03,0x80,0x07,0x00,0x0E,0x00,0x1E,0x00,
0x1C,0x00,0x38,0x00,0x3F,0xFC,0x3F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xC0,
0x0F,0xF0,0x1C,0x70,0x1C,0x38,0x38,0x38,0x38,0x38,0x00,0x38,0x00,0x38,0x00,0x70,
0x01,0xE0,0x01,0xF0,0x00,0x78,0x00,0x3C,0x00,0x1C,0x70,0x1C,0x70,0x1C,0x70,0x1C,
0x38,0x3C,0x3C,0x38,0x1F,0xF0,0x07,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,
0x00,0xF0,0x00,0xF0,0x01,0xF0,0x03,0xF0,0x03,0x70,0x06,0x70,0x0E,0x70,0x0C,0x70,
0x1C,0x70,0x18,0x70,0x30,0x70,0x70,0x70,0x7F,0xFE,0x7F,0xFE,0x00,0x70,0x00,0x70,
0x00,0x70,0x00,0x70,0x00,0x70,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xF8,
0x1F,0xF8,0x18,0x00,0x18,0x00,0x18,0x00,0x38,0x00,0x30,0x00,0x33,0xE0,0x3F,0xF0,
0x3C,0x78,0x30,0x38,0x00,0x1C,0x00,0x1C,0x00,0x1C,0x00,0x1C,0x70,0x1C,0x70,0x1C,
0x70,0x18,0x38,0x38,0x3C,0x70,0x1F,0xF0,0x07,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xF0,
0x07,0xF8,0x0E,0x1C,0x0C,0x0E,0x1C,0x0E,0x1C,0x00,0x38,0x00,0x39,0xF0,0x3B,0xF8,
0x3E,0x3C,0x3C,0x1C,0x38,0x0E,0x38,0x0E,0x38,0x0E,0x38,0x0E,0x38,0x0E,0x18,0x0E,
0x1C,0x1C,0x0E,0x3C,0x07,0xF8,0x03,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFC,
0x3F,0xFC,0x00,0x1C,0x00,0x3C,0x00,0x38,0x00,0x38,0x00,0x38,0x00,0x70,0x00,0x70,
0x00,0x70,0x00,0xE0,0x00,0xE0,0x00,0xE0,0x01,0xC0,0x01,0xC0,0x01,0xC0,0x03,0x80,
0x03,0x80,0x03,0x80,0x07,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xC0,
0x0F,0xE0,0x1C,0x70,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x1C,0x70,
0x0F,0xE0,0x0F,0xE0,0x3C,0x78,0x38,0x38,0x70,0x1C,0x70,0x1C,0x70,0x1C,0x70,0x1C,
0x78,0x3C,0x3C,0x78,0x1F,0xF0,0x07,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
];
    //转换一个字节为线段.
    function getBitLine(data){
        var dots=0;
        var line="";
        var bit=1;
        for(var i=0;i<8;i++){
            if(bit&data){
                line="■"+line;
                dots++;
            }
            else{
                line=" "+line;
            }
            bit=bit<<1;
        }
        return {dots:dots,line:line};
    }
    window.onload=function(){
        var lines=[];
        var dots=0;
        var char=32; //字库数据太多,帖子发不了,删掉了大部分。从空格开始.
        var nl=0;
        for(var i=0;i<fontData.length;i+=2){
            var c1=getBitLine(fontData[i])
            var c2=getBitLine(fontData[i+1]);
            dots+=c1.dots+c2.dots;
            lines.push(c1.line+c2.line);
            nl++;
            if(nl==32){
                var str="<font size='2'><br/>"+String.fromCharCode(char)+
                    "("+char+"):"+dots+"dots</font>";
                lines.push(str);
                
                nl=0;
                dots=0;
                char++;
            }
        }
        var content=lines.join("<br/>");
        document.getElementById("divOut").innerHTML+=content;
    }
</script>
    </head>
    <body>
        <div id="divOut" style="font-size:4px; line-height:4px;"></div>
    </body>
</html>

 
  相关解决方案