Android默认字体ASCII码中可显示字符的平均灰度由小到大排序

如题:

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();
	
	public 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;
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		drawInner(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);
		drawInner(mDrawCanvas);
	}
}

@Override
	public 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>();
	private Bitmap mBitmap;
@Override
	public 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 : 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);
					pos = 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";


转载于:https://my.oschina.net/u/189899/blog/299967

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值