图像转换

@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());
		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();
		Bitmap dest = Bitmap.createBitmap(width * 2, height * 2, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(dest);
		Paint paint = new Paint();
		paint.setTextSize(8);
		float textsize = paint.measureText("W");
		float textHeight = paint.descent() - paint.ascent();
		float textPosY = - paint.ascent();
		float textPosX = - 0;
//		int dimen = 4;
//		int dimenY = (int)(dimen * 2.23f);
		int dimen = (int)textsize / 2;
		int dimenY = (int)textHeight / 2;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < height; i+= dimenY) {
			for (int j = 0; j < width; j+= dimen) {
				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();
				int threshold = 180;
				if (averGray < threshold) {
					int pos = (int)((threshold - averGray) / (float)threshold * dicCount);
					pos = pos > dicCount - 1 ? dicCount - 1 : pos;
					int charInt = Integer.decode(dics[pos]);
					sb.append((char)charInt);
					canvas.drawText(String.valueOf((char)charInt), textPosX, textPosY, paint);
				} else {
					sb.append(' ');
				}
				textPosX += textsize;
			}
			textPosY += textHeight;
			textPosX = 0;
			sb.append("\n");
		}
		
		saveBitmapToFile(dest, Environment.getExternalStorageDirectory().getAbsolutePath(), "dest.png", true, false);
		Log.e("HA", sb.toString());
		((TextView)findViewById(R.id.mytext)).setText(sb.toString());
		File file2 = new File(Environment.getExternalStorageDirectory(), "test.txt");
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter(file2));
			bw.write(sb.toString());
			bw.flush();
		} catch (IOException e) {
		} finally {
			if (bw != null) {
				try {bw.close();}catch(Exception e){};
			}
		}
	}
	
	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);
	}
	
	
	/**
	 * 保存bitmap到文件中,因为不知道当前bitmap的格式,可能致使保存后文件变大或变小,慎重使用
	 * @param bitmap
	 * @param path
	 * @param name
	 * @param isHighDefinition png或jpg存储
	 * @param recycleSelf 是否释放该位图
	 */
	public static void saveBitmapToFile(Bitmap bitmap, String path, String name, boolean isHighDefinition, boolean recycleSelf) {
		File file = new File(path);  
		if (!file.exists()) {  
			file.mkdirs();  
		}  

		File imageFile = new File(file, name);
		// 若是文件已经存在,则不继续保存
		if (imageFile.exists()) {
			return;
		}
		FileOutputStream fos = null;
		try {  
			imageFile.createNewFile();  
			fos = new FileOutputStream(imageFile);
			// TODO 后续能够根据分辨率来保存不一样图片质量
			if (isHighDefinition) {
				bitmap.compress(CompressFormat.PNG, 100, fos);  
			} else {
				bitmap.compress(CompressFormat.JPEG, 50, fos);  
			}
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		} finally {
			if (fos != null) {
				try {
					fos.flush();
				} catch (IOException e) {
				}  
				try {
					fos.close();
				} catch (IOException e) {
				}
			}
		}
	}
	
	private Map<Integer, Character> results = new HashMap<Integer, Character>();
	private Bitmap mBitmap;
	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";
相关文章
相关标签/搜索