如题:
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";