图片和base64编码字符串 互相转换

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; import java.io.*; /**
* @author lishupeng
* @create 2017-05-06 下午 2:56
**/
public class Base64Test {
public static void main(String[] args) {
String strImg = GetImageStr();
System.out.println(strImg);
GenerateImage(strImg);
} //图片转化成base64字符串
public static String GetImageStr() {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = "C:\\Users\\Administrator\\Desktop\\a\\1.png";//待处理的图片
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);//返回Base64编码过的字节数组字符串
} //base64字符串转化成图片
public static boolean GenerateImage(String imgStr) { //对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) //图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
//Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {//调整异常数据
b[i] += 256;
}
}
//生成jpeg图片
String imgFilePath = "d://222.jpg";//新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}

图片和byte数组互相转换

import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; /**
* @author lishupeng
* @create 2017-05-06 下午 3:48
**/
public class Image2Byte { public static void main(String[] args){
byte[] data = image2byte("C:\\Users\\Administrator\\Desktop\\a\\1.png");
System.out.println(data.toString());
byte2image(data,"d://222.jpg");
} //图片到byte数组
public static byte[] image2byte(String path) {
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
} catch (FileNotFoundException ex1) {
ex1.printStackTrace();
} catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
} //byte数组到图片
public static void byte2image(byte[] data, String path) {
if (data.length < 3 || path.equals("")) return;
try {
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
imageOutput.write(data, 0, data.length);
imageOutput.close();
System.out.println("Make Picture success,Please find image in " + path);
} catch (Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
} //byte数组到16进制字符串
public String byte2string(byte[] data) {
if (data == null || data.length <= 1) return "0x";
if (data.length > 200000) return "0x";
StringBuffer sb = new StringBuffer();
int buf[] = new int[data.length];
//byte数组转化成十进制
for (int k = 0; k < data.length; k++) {
buf[k] = data[k] < 0 ? (data[k] + 256) : (data[k]);
}
//十进制转化成十六进制
for (int k = 0; k < buf.length; k++) {
if (buf[k] < 16) sb.append("0" + Integer.toHexString(buf[k]));
else sb.append(Integer.toHexString(buf[k]));
}
return "0x" + sb.toString().toUpperCase();
}
}

图片和base64编码字符串 互相转换,图片和byte数组互相转换的更多相关文章

  1. js将图片转为base64编码,以字符串传到后台存入数据库

    (前台在中approve_edit.html中,后台不变) 链接参考:http://www.cnblogs.com/Strom-HYL/p/6782176.html 该链接文中并没有用到easyUI的 ...

  2. 将图片转化为base64编码字符串

    pom依赖 <dependency> <groupId>org.ops4j.base</groupId> <artifactId>ops4j-base- ...

  3. 用javascript实现base64编码器以及图片的base64编码

    前面的话 base-64作为常见的编码函数,在基本认证.摘要认证以及一些HTTP扩展中得到了大量应用.在前端领域,也常常把图片转换为base-64编码在网络中传输.本文将详细介绍base64的原理及用 ...

  4. java中图片地址base64编码的相互转换

    public class Base64Url { /** * 将base64编码字符串转换为图片 * @param imgStr: base64编码字符串 * @param path: 图片路径-具体 ...

  5. 字符串与图片的Base64编码转换操作

    //图片 转为 base64编码的文本 private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = ne ...

  6. java编写之jpg图片与base64编码之间的转换

    /** * @author zyq * 将网络图片进行Base64位编码 * @param imgUrl * */ public static String encodeWebImageToBase6 ...

  7. JavaScript—图片与base64编码互相转换

    图片转换为base64编码 <input type = "file" id = "file" onchange="popFileName(thi ...

  8. C# base64编码的文本与图片互转

    /// <summary> /// base64编码的文本转为图片 /// </summary> /// <param name="txtFilePath&qu ...

  9. JAVA 将图片转换为Base64编码

    这里使用的jar包是commons-codec-1.10.jar; 示例代码 import java.io.FileInputStream; import java.io.FileOutputStre ...

随机推荐

  1. python2.7练习小例子(十八)

    19):题目:一个数如果恰好等于它的因子之和,这个数就称为"完数".例如6=1+2+3.编程找出1000以内的所有完数.      #!/usr/bin/python # -*- ...

  2. gp更新来的太快

    意外总是会发生 添加一个判断function的分支,过滤掉function,其实也考虑再进一步去分析它的作用,稍后再议. 更新一下 var gnp = { get: function(url) { r ...

  3. 添加用户-查看用户列表-禁止默认root登陆

    程序小屌丝狒狒: (Q971751392) linux添加用户 adduser feifei passwd [用户名] 设置密码 可以查看所有用户的列表 cat /etc/passwd  w 可以查看 ...

  4. APPium-python实例(记录)

    https://github.com/appium/sample-code/tree/master/sample-code/examples/python

  5. 「日常训练」「小专题·图论」Domino Effect(1-5)

    题意 分析 这题几乎就是一条dijkstra的问题.但是,如何考虑倒在中间? 要意识到这题求什么:单源最短路的最大值.那么有没有更大的?倒在中间有可能会使它更大. 但是要注意一个问题:不要把不存在的边 ...

  6. Long Short-Term Memory (LSTM)

                         Long Short-Term Memory (LSTM) Outline Background LSTM Network Extended LSTM LST ...

  7. 【EasyNetQ】- 使用SSL连接

    EasyNetQ可以通过SSL连接.戈登·库尔特(Gordon Coulter)撰写的这本指南最初是针对一个提出的问题而写的. 首先,您必须仔细按照https://www.rabbitmq.com/s ...

  8. RxJS & Angular

    RxJS & Angular https://www.learnrxjs.io/ https://rxjs-cn.github.io/learn-rxjs-operators/ https:/ ...

  9. spring笔记(二)

    共性问题: 1. 服务器启动报错,什么原因? * jar包缺少.jar包冲突 1) 先检查项目中是否缺少jar包引用 2) 服务器: 检查jar包有没有发布到服务器下: 用户库jar包,需要手动发布到 ...

  10. BZOJ4347 POI2016Nim z utrudnieniem(博弈+动态规划)

    由nim游戏的结论,显然等价于去掉一些数使剩下的数异或和为0. 暴力的dp比较显然,设f[i][j][k]为前i堆移走j堆(模意义下)后异或和为k的方案数.注意到总石子数量不超过1e7,按ai从小到大 ...