Bitmap插入二进制字符串及解析

1.先把汉字转换为二进制字符串,插入bitmap,保存。

package com.example.cusnotification;

import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;

public class MainActivity extends Activity {

	public int onePicWidth = 40 * 12;
	public int onePicHeight = 30 * 12;
	private String str = "为开发者提供全网最优质的服务";
	private String binaryStr;
	public String path = Environment.getExternalStorageDirectory().toString() + "/picture.jpg";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		binaryStr = separate(str);
		//二进制字符串中的空格全部替换成数字2
		String two = binaryStr.replaceAll(" ", "2");
		Bitmap infoBitmap = charBitmap(two);
		saveMyBitmap(path, infoBitmap);
	}
	
	/**
	 * 把图片保存到SD卡指定路径
	 * @param bitName
	 * @param mBitmap
	 */
	public void saveMyBitmap(String bitName, Bitmap mBitmap) {
		File f = new File(bitName);
		FileOutputStream fOut = null;
		try {
			f.createNewFile();
			fOut = new FileOutputStream(f);
			mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
			fOut.flush();
			fOut.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 以100字为段 判断有几页
	 * 分页转换成二进制
	 * @param content
	 */
	private String separate(String content) {
		String binaryContent = "";
		int jsize = content.length() % 100 == 0 ? content.length() / 100 : (content.length() / 100 + 1);
		for (int j = 0; j < jsize; j++) {
			if (j == (jsize - 1)) {
				binaryContent += toBinary(content.substring(j * 100));
			} else {
				binaryContent += toBinary(content.substring(j * 100, (1 + j) * 100));
			}
		}
		return binaryContent;
	}

	/**
	 * 把二进制写入bitmap
	 * 
	 * @param content
	 * @return
	 */
	private Bitmap charBitmap(String content) {
		char[] cha = content.toCharArray();
		Bitmap newBitmap = Bitmap.createBitmap(onePicWidth, onePicHeight,
				Bitmap.Config.RGB_565);
		int[] pixels = new int[onePicWidth * onePicHeight];
		int a = cha.length * 3 % onePicWidth == 0 ? cha.length * 3
				/ onePicWidth : (cha.length * 3 / onePicWidth + 1);
		newBitmap.getPixels(pixels, 0, onePicWidth, 0, 0, onePicWidth, a);
		for (int i = 0; i < onePicWidth * a; i++) {
			pixels[i] = Color.argb(0, 0, 0, 0);
		}
		int m = 0;
		for (int i = 0; i < cha.length; i++) {
			for (int j = 2; j >= 0; j--) {
				if (j <= cha[i] - '0') {
					pixels[m] = Color.argb(255, 255, 255, 255);
				}
				m++;
			}
		}
		newBitmap.setPixels(pixels, 0, onePicWidth, 0, 0, onePicWidth, a);
		return newBitmap;
	}

	/**
	 * 将字符串转换成二进制字符串,以空格相隔
	 */
	public static String toBinary(String str) {
		char[] strChar = str.toCharArray();
		String result = "";
		for (int i = 0; i < strChar.length; i++) {
			result += Integer.toBinaryString(strChar[i]) + " ";
		}
		return result;
	}
}

2.通过图片解析插入的内容


import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ParseBitmap {

	public int onePicWidth = 40 * 12;
	public int onePicHeight = 30 * 12;

	/**
	 * 通过图片路径去解析内容
	 * @param fileName 格式:E://picture.jpg
	 * @return
	 */
	public String getWeiBoText(String fileName) {
		String text = null;
		File file = new File(fileName);
		int[] result = null;
		if (!file.exists()) {
			return null;
		}
		try {
			//读取图片
			BufferedImage bufImg = ImageIO.read(file);
			//获取图片宽高
			int height = bufImg.getHeight();
			int width = bufImg.getWidth();
			int fixwidth = onePicWidth;
			int fixheight = onePicHeight;
			//判断有几张图片拼接
			int picsize = height / fixheight;
			if (width == onePicWidth) {
				result = new int[fixwidth * fixheight];
				int m = 0;
				for (int j = 0; j < fixheight; j++) {
					for (int i = 0; i < fixwidth; i++) {
						//onePicHeight * (picsize - 1) + j 解析开始的高度
						result[m] = bufImg.getRGB(i, (onePicHeight * (picsize - 1) + j)) & 0xFFFFFF;
						m++;
					}
				}
				text = GetTextInfo(result);
			}
			//解析完成后删除图片
	//		file.deleteOnExit();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return text;
	}

	public static String GetTextInfo(int[] result) {
		String text = "";
		int iRgb = 0;
		int r = 0;
		int g = 0;
		int b = 0;
		int[] iResult = new int[result.length / 3];
		ColorModel cm = ColorModel.getRGBdefault();
		//把图片的像素解析成数字 (0 1 2) iResult
		for (int i = 0; i < (result.length / 3); i++) {
			iRgb = 0;
			for (int j = 0; j < 3; j++) {
				r = cm.getRed(result[i * 3 + j]);
				g = cm.getGreen(result[i * 3 + j]);
				b = cm.getBlue(result[i * 3 + j]);
				if (r + g + b > 500) {
					iRgb++;
				}
			}
			iResult[i] = iRgb - 1;
		}
		//转换成二进制 并把2转换成空格
		String str = "";
		for (int i = 0; i < iResult.length; i++) {
			if (String.valueOf(iResult[i]).equals("0")) {
				str += "0";
			} else if (String.valueOf(iResult[i]).equals("1")) {
				str += "1";
			} else if (String.valueOf(iResult[i]).equals("2")) {
				str += " ";
			}
		}
		text = toStr(str);
		return text;
	}

	/**
	 * 二进制转换成汉字
	 * @return
	 */
	public static String toStr(String binStr) {
		String[] tempStr = StrToStrArray(binStr);
		char[] tempChar = new char[tempStr.length];
		for (int i = 0; i < tempStr.length; i++) {
			tempChar[i] = toChar(tempStr[i]);
		}
		return String.valueOf(tempChar);
	}

	/**
	 * 将初始二进制字符串转换成字符串数组,以空格相隔
	 * @param str
	 * @return
	 */
	private static String[] StrToStrArray(String str) {
		return str.split(" ");
	}

	/**
	 * 将二进制字符串转换为char
	 * @param binStr
	 * @return
	 */
	private static char toChar(String binStr) {
		int[] temp = binStrToIntArray(binStr);
		int sum = 0;
		for (int i = 0; i < temp.length; i++) {
			sum += temp[temp.length - 1 - i] << i;
		}
		return (char) sum;
	}

	/**
	 * 将二进制字符串转换成int数组
	 * @param binStr
	 * @return
	 */
	private static int[] binStrToIntArray(String binStr) {
		char[] temp = binStr.toCharArray();
		int[] result = new int[temp.length];

		for (int i = 0; i < temp.length; i++) {
			result[i] = temp[i] - 48;
		}
		return result;
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值