java实现图片与base64字符串之间的转换

来源:互联网 发布:linux vi 搜索 下一个 编辑:程序博客网 时间:2024/05/18 03:27
// 图片转化成base64字符串public static byte[] GetImageStr() {String imgFile = "E://soft//1.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) {e.printStackTrace();}byte[] caly = null;try {caly = Base64.encodeBase64(data);} catch (Exception e) {e.printStackTrace();}return caly;}// base64字符串转化成图片public static boolean GenerateImage(byte[] a ) {if (a == null) // 图像数据为空return false;try {// Base64解码byte[] b = Base64.decodeBase64(a);for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {// 调整异常数据b[i] += 256;}}String imgFilePath = "e://112.jpg";// 新生成的图片OutputStream out = new FileOutputStream(imgFilePath);out.write(b);out.flush();out.close();return true;} catch (Exception e) {return false;}}
0 0
原创粉丝点击