AS3 to analyze 2D images into text text implementation code

Source: Internet
Author: User

For any point in a pixel, you can get his grayscale. Calculation formula of gray scale

The code is as follows Copy Code

GETR (color) * 0.299 + GETG (color) * 0.587 + GETB (color) * 0.114;

And each text also has its corresponding light and shade, than the true heart of a ball, it will be more dark than the hollow, because it more closely, the way to find the text

The code is as follows Copy Code
Private Function Getdensity (t:string): number {
var Ttf:textfield = new TextField;
Ttf.autosize = Textfieldautosize.left;
Ttf.text = t;

var bmd:bitmapdata = new BitmapData (Ttf.width, Ttf.height, False, 0XFFFFFF);
Bmd.draw (TTF);
var c:int = 0;
for (var x:int = 0; x < bmd.width;x++) {
for (var y:int = 0; y < bmd.height;y++) {
if (Bmd.getpixel (x,y)!=0xffffff) {
C + +;
}
}
}
return c/ttf.width/ttf.height;
}


Complete code

The code is as follows Copy Code

Package
{
Import Flash.display.Bitmap;
Import Flash.display.BitmapData;
Import Flash.display.Loader;
Import Flash.display.Sprite;
Import flash.events.Event;
Import Flash.geom.Rectangle;
Import flash.net.FileReference;
Import Flash.text.TextField;
Import flash.text.TextFieldAutoSize;
Import Flash.text.TextFormat;
Import Sliz.miniui.Button;
Import Sliz.utils.UIUtils;
/**
* ...
* @author Lizhi
*/
public class Testtext extends Sprite
{
private var fonts:array = [];
private Var Tf:textfield;

private Var file:filereference;
private Var Loader:loader;
Public Function Testtext ()
{
tf = new TextField;
var tfm:textformat = new TextFormat;
Tfm.size = 10;
tfm.leading =-2;
Tf.defaulttextformat = TFM;
Tf.autosize = Textfieldautosize.left;
var text:string = "";
var min:int = 32;
var max:int = 120;
while (++min < max) {
var c:string = string.fromcharcode (min);
text + C;
}
Text + ". '" *#@%+-) (tii?><~ ";
for (var i:int = 0; i < text.length;i++) {
c = Text.charat (i);
Fonts.push ({d:getdensity (c), t:c});
}
Fonts.sorton ("D", array.numeric| array.descending);

[Embed (Source = ' 123.png ')]var Ic:class;
var bmd:bitmapdata = (new IC as Bitmap). BitmapData;
Render (BMD);
AddChild (TF);

Uiutils.changestage (stage);

var Explorer:button = New Button ("Explorer image", 2, this, browse);

Loader = new loader ();
Loader.contentLoaderInfo.addEventListener (Event.complete, onLoad2);
}

Private function Render (Bmd:bitmapdata): void {
Tf.width = 1000;
Tf.height = 1000;
var x:int = 0;
var y:int = 0;
Tf.text = "";
while (Y < bmd.height) {
while (X<bmd.width) {
var gray:number = Getgray (Bmd.getpixel (x, y))/0xFF;
var index:int = Int ((fonts.length-1) * gray);
Tf.appendtext (FONTS[INDEX].T);
var r:rectangle = tf.getcharboundaries (tf.text.length-1);
x + + r.width;
}
Tf.appendtext ("n");
y = R.bottom
x = 0;
}
}

Private Function Getdensity (t:string): number {
var Ttf:textfield = new TextField;
Ttf.autosize = Textfieldautosize.left;
Ttf.text = t;

var bmd:bitmapdata = new BitmapData (Ttf.width, Ttf.height, False, 0XFFFFFF);
Bmd.draw (TTF);
var c:int = 0;
for (var x:int = 0; x < bmd.width;x++) {
for (var y:int = 0; y < bmd.height;y++) {
if (Bmd.getpixel (x,y)!=0xffffff) {
C + +;
}
}
}
return c/ttf.width/ttf.height;
}

Private Function Getr (color:uint): UINT {
return color << 8 >>> 24;
}
Private Function Getg (color:uint): UINT {
return color << >>> 24;
}
Private Function Getb (color:uint): UINT {
return color << >>> 24;
}
Private Function Getgray (color:uint): UINT {
return GETR (color) * 0.299 + GETG (color) * 0.587 + GETB (color) * 0.114;
}

Private Function browse (e:event): void {
File = new Filereference ();
File.browse ();
File.addeventlistener (Event.select, onselect);
}

Private Function Onselect (e:event): void {
File.load ();
File.addeventlistener (Event.complete, onLoad);
}

Private Function OnLoad (e:event): void {

Loader.loadbytes (File.data);

}

Private Function OnLoad2 (e:event): void {
var content:bitmap = loader.contentLoaderInfo.content as Bitmap;
Render (Content.bitmapdata);
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.