Android default font ASCII code can display characters in the average grayscale from small to large sort

Source: Internet
Author: User

Title:

46,96,39,58,45,44,59,34,95,126,33,94,105,114,124,47,73,61,60,62,42,108,92,49,116,43,106,63,118,41,40,76,102,123,55,125,74 , 84,99,120,122,93,91,117,110,115,89,111,70,121,101,50,97,86,107,51,104,90,67,52,80,53,65,113,88,112,69,37,48,85,100,98,54 , 75,83,57,35,72,119,71,36,79,103,68,56,82,81,109,66,38,78,87,77,64

Package com.example.test;import android.content.context;import android.graphics.bitmap;import  android.graphics.canvas;import android.graphics.color;import android.graphics.paint;import  android.graphics.Paint.FontMetrics;import android.util.AttributeSet;import android.view.View; public class charview extends view{private paint mpaint;private char  Mchar;private int mtextsize;private fontmetrics mfm = new fontmetrics (); Private canvas mdrawcanvas = new canvas ();p Ublic charview (Context context,  attributeset attrs)  {this (context, attrs, -1);} Public charview (Context context)  {this (context, null, -1);} Public charview (Context context, attributeset attrs, int defstyle)  {super ( Context, attrs, defstyle); Mpaint = new paint (); Mpaint.setcoloR (color.red);} Public void setchar (char c)  {mchar = c;mtextsize = measurechar (c,  getmeasuredwidth ()); invalidate ();} Private int measurechar (char c, int maxwidth)  {int result = 0;for   (int i = 0; i < maxwidth * 2; i++)  { Mpaint.settextsize (i);int textsize =  (int.) Mpaint.measuretext (String.valueof (c)); Mpaint.getfontmetrics (mFm);int textheight =  (int) (mfm.bottom - mfm.top);if  ( textsize > maxwidth | |  textheight > maxwidth)  {result = i;break;}} Return result;} @Overrideprotected  void ondraw (Canvas canvas)  {super.ondraw (canvas);d rawinner (canvas);} Private void drawinner (Canvas canvas)  {if  (mtextsize == 0)  {mtextsize  = measurechar (Mchar, getmeasuredwidth ());} Mpaint.settextsize (MTextSize); Mpaint.getfontmetrics (mFm); int baseheight = -(int) mfm.top;int x =  (int) ( Getmeasuredwidth ()  - mpaint.measuretext (string.valueof (Mchar)))  / 2;//canvas.drawline (0,  baseheight+mfm.ascent, 500, baseheight+mfm.ascent, mpaint);//canvas.drawLine (0,  Baseheight, 500, baseheight, mpaint);//canvas.drawline (0, baseheight+mfm.descent, 500 ,  baseheight+mfm.descent, mpaint);//canvas.drawline (0, baseheight+mfm.bottom, 500,  Baseheight+mfm.bottom, mpaint); Canvas.drawtext (string.valueof (Mchar), x, baseheight,  Mpaint);} Public void drawbitmap (Bitmap bitmap)  {bitmap.erasecolor (color.transparent); Mdrawcanvas.setbitmap (bitmap);d Rawinner (Mdrawcanvas);}}

@Overridepublic  void onclick (view v)  {if  (mbitmap == null)  {mbitmap  = bitmap.createbitmap (Mcharview.getmeasuredwidth (),  mcharview.getmeasuredheight (),  Bitmap.Config.ARGB_8888);} for  (int i = 33; i < 127; i++)  {char c =  (char) I;mcharview.setchar (c); Mcharview.drawbitmap (MBITMAP);int avergray =  (int) (CalAverageGray ( MBITMAP)  * 100000); Results.put (avergray, c);} List<integer> floats = new arraylist<integer> (); Floats.addAll (Results.keySet () ); Collections.sort (floats); Stringbuilder sb = new stringbuilder ();for  (integer f : floats)  { LOG.D ("Result",  results.get (f) + " : " +f), Sb.append (string.valueof ((int) results.get (f))). Append (" ,");} LOG.D ("Result",  "result = " +sb.tostring ());} Private float calaveragegray (bitmap b)  {int  Width = b.getwidth (); Int height = b.getheight ();int nonewhitecount =  0;for (int i = 0; i < height; i++)  {for  (int j =  0; j < width; j++)  {int color = b.getpixel (j, i); int  r = color.red (color); Int g = color.green (color);int bl =  Color.Blue (color);//LOG.D ("Pix",  "pos = " + "(" + "j, " + "I" + ")" + "rgb = " +r+ ", " +g+ " , " +BL);if  (r > 100)  {nonewhitecount++;}} return nonewhitecount /  (float) (width * height);} private map<integer, character> results = new hashmap<integer,  Character> ();p rivate bitmap mbitmap;
@Overridepublic  void onclick (view v)  {string[] dics = dic.split (","); int  dicCount = dics.length; File file = new file (Environment.getexternalstoragedirectory (),  "test.jpg"); Bitmap bitmap = bitmapfactory.decodefile (File.getabsolutepath ());int width =  Bitmap.getwidth (); Int height = bitmap.getheight (); Int dimen = 10;int dimeny  =  (int) (height /  (float) width * dimen); Stringbuilder sb = new stringbuilder ();for  (int i = 0; i  < height; i+= dimen)  {for  (int j = 0; j < width;  j+= dimeny)  {int w = dimen > width - j ? width  - j : dimen;int h = dimeny > height - i ?  height - i :&nBsp;dimeny; Bitmap temp = bitmap.createbitmap (bitmap, j, i, w, h); Float averGray  = calaveragegray (temp); Temp.recycle ();if  (avergray < 128)  {int pos  =  (int) ((128 - avergray)  / 128f * diccount);p os = pos  > diccount - 1 ? diccount - 1 : pos;int charint =  integer.decode (Dics[pos]); Sb.append ((char) charint);}  else {sb.append ('   ');}} Sb.append ("\ n");} LOG.E ("HA",  sb.tostring ());} Private float calaveragegray (bitmap b)  {int width = b.getwidth ();int  Height = b.getheight (); Float graycount = 0;for (int i = 0; i  < height; i++)  {for  (int j = 0; j < width; j++)  {int color = b.getpixel (j, i); Int r = color.Red (color); Int g = color.green (color); Int bl = color.blue (color);graycount  + = (r * 19595 + g * 38469 + bl * 7472)  >> 16;}} return graycount /  (width * height);} private string dic =  " 46,96,39,58,45,44,59,34,95,126,33,94,105,114,124,47,73,61,60,62,42,108,92,49,116,43,106,63,118,41,40,76,102,123,55,125,74 , 84,99,120,122,93,91,117,110,115,89,111,70,121,101,50,97,86,107,51,104,90,67,52,80,53,65,113,88,112,69,37,48,85,100,98,54 , 75,83,57,35,72,119,71,36,79,103,68,56,82,81,109,66,38,78,87,77,64 ";


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.