Base64对图片的编码和解码
2018-03-14 17:10
567 查看
public class test{
//图片转化成base64字符串
public static String GetImageStr(){
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = "E:\\Image\\image.jpg";
//将图片转为字节数组
InputStream in = null;
byte[] data = null;
try {
//读取图片字节数组
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//对字节数组base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
//base64字符串转化成图片
public static boolean GenerateImage(String imgStr) {
//对字节数组字符串进行Base64解码并生成图片
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;
}
}
//生成jpeg图片
String imgpath = "E:\\image.jpg";
FileOutputStream out = new FileOutputStream(imgpath);
out.write(b);
out.flush();
out.close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public static void main(String[] args)
{
String strImg = GetImageStr();
System.out.println(strImg);
GenerateImage(strImg);
}
}
//图片转化成base64字符串
public static String GetImageStr(){
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = "E:\\Image\\image.jpg";
//将图片转为字节数组
InputStream in = null;
byte[] data = null;
try {
//读取图片字节数组
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//对字节数组base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
//base64字符串转化成图片
public static boolean GenerateImage(String imgStr) {
//对字节数组字符串进行Base64解码并生成图片
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;
}
}
//生成jpeg图片
String imgpath = "E:\\image.jpg";
FileOutputStream out = new FileOutputStream(imgpath);
out.write(b);
out.flush();
out.close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public static void main(String[] args)
{
String strImg = GetImageStr();
System.out.println(strImg);
GenerateImage(strImg);
}
}
相关文章推荐
- java利用Base64编码和解码图片文件
- 图片base64编码的前端展示及后端解码,编码
- 利用base64对图片进行编码及解码
- Mac平台命令行下使用base64对图片编码和解码
- Android图片的Base64编码与解码及解码Base64图片方法
- 图片通过Base64Coder编码、解码
- 对编码后的字节数组字符串进行Base64解码并生成图片
- base64编码解码图片
- android Java BASE64编码和解码二:图片的编码和解码
- C#和JAVA利用BASE64实现图片编码解码
- 使用PHP对图片进行base64编码和解码
- android Java BASE64编码和解码二:图片的编码和解码
- PHP解码base64编码的图片例
- JS 对图片base64编码字符串进行解码并输出图像
- android 图片base64编码解码
- 利用base64对图片进行编码及解码
- android BASE64编码和解码二:图片的编码和解码
- java利用Base64编码和解码图片文件
- js对图片base64编码字符串进行解码并输出图像示例
- c#对base64的编码的图片解码