package com.app21;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.util.Base64;
import sun.misc.BASE64Decoder.encoder.BASE64Decoder;
import sun.misc.BASE64Decoder.encoder.BASE64Encoder;public classImgHelper {/**
* TODO:将byte数组以Base64方式编码为字符串
* @param bytes 待编码的byte数组
* @return 编码后的字符串
**/
public static String encode(byte[] bytes){return newBASE64Encoder().encode(bytes);
}/**
* TODO:将以Base64方式编码的字符串解码为byte数组
* @param encodeStr 待解码的字符串
* @return 解码后的byte数组
* @throws IOException
**/
public static byte[] decode(String encodeStr) throws IOException{byte[] bt = null;
BASE64Decoder decoder= newBASE64Decoder();
bt=decoder.decodeBuffer(encodeStr);returnbt;
}/**
* TODO:将两个byte数组连接起来后,返回连接后的Byte数组
* @param front 拼接后在前面的数组
* @param after 拼接后在后面的数组
* @return 拼接后的数组
**/
public static byte[] connectBytes(byte[] front, byte[] after){byte[] result = new byte[front.length +after.length];
System.arraycopy(front,0, result, 0, after.length);
System.arraycopy(after,0, result, front.length, after.length);returnresult;
}/**
* TODO:将图片以Base64方式编码为字符串
* @param imgUrl 图片的绝对路径(例如:D:\\jsontest\\abc.jpg)
* @return 编码后的字符串
* @throws IOException
**/
public staticString encodeImage(String imgUrl) throws IOException{
FileInputStream fis= newFileInputStream(imgUrl);byte[] rs = new byte[fis.available()];
fis.read(rs);
fis.close();returnencode(rs);
}/**
* 将Bitmap转换成字符串
* @param bitmap
* @return*/
public staticString bitmaptoString(Bitmap bitmap) {
Stringstring = null;
ByteArrayOutputStream bStream= newByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG,100, bStream);byte[] bytes =bStream.toByteArray();string =Base64.encodeToString(bytes, Base64.DEFAULT);return string;
}/**
* 把byte数组转化成 bitmap对象
* @param b
* @return*/
public static Bitmap bytes2Bimap(byte[] b) {if (b.length != 0) {return BitmapFactory.decodeByteArray(b, 0, b.length);
}else{return null;
}
}
}