图片与base的换算
图片转化成base64字符串
public static String ImageToBase64(String targetPath){
String imgFile = targetPath;
InputStream in;
byte[] data = null;
try{
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
}catch (IOException e){
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
base64字符串转化成图片
public static boolean Base64ToImage(String imgStr, String targetPath){
if (imgStr == null) {
return false;
}
BASE64Decoder decoder = new BASE64Decoder();
try{
byte[] b = decoder.decodeBuffer(imgStr);
for(int i=0;i<b.length;++i){
if(b[i]<0){
b[i]+=256;
}
}
String imgFilePath = targetPath;
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e){
return false;
}
}