博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
base64字符串转化成图片
阅读量:4554 次
发布时间:2019-06-08

本文共 2332 字,大约阅读时间需要 7 分钟。

package com.dhht.wechat.util; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; /**  * @Author: sh  * @Description: ImgUtil  * @Date: 9:14 2019/7/1  */ public class ImgUtil {
/** * 图片转化成base64字符串 * * @param imgPath * @return */ public static String GetImageStr(String imgPath) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 String imgFile = imgPath;// 待处理的图片 InputStream in = null; byte[] data = null; String encode = null; // 返回Base64编码过的字节数组字符串 // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); try {
// 读取图片字节数组 in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); encode = encoder.encode(data); } catch (IOException e) {
e.printStackTrace(); } finally {
try {
in.close(); } catch (IOException e) {
// TODO Auto-generated catch block e.printStackTrace(); } } return encode; } /** * base64字符串转化成图片 * * @param imgData 图片编码 * @param imgFilePath 存放到本地路径 * @return * @throws IOException */ @SuppressWarnings("finally") public static boolean GenerateImage(String imgData, String imgFilePath) throws IOException { // 对字节数组字符串进行Base64解码并生成图片 if (imgData == null) // 图像数据为空 return false; BASE64Decoder decoder = new BASE64Decoder(); OutputStream out = null; try {
out = new FileOutputStream(imgFilePath); // Base64解码 byte[] b = decoder.decodeBuffer(imgData); for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据 b[i] += 256; } } out.write(b); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) {
// TODO Auto-generated catch block e.printStackTrace(); } finally {
out.flush(); out.close(); return true; } } }

转载于:https://www.cnblogs.com/sung1024/p/11391965.html

你可能感兴趣的文章
ExtJS下拉树
查看>>
android 调用系统相机录像并保存
查看>>
BW系统表的命名规则
查看>>
Asp.Net在IE10下出现_doPostBack未定义的解决办法 LinkButton
查看>>
《CLR via C#》Part2之Chapter5 基元类型、引用类型和值类型(一)
查看>>
1-9 RHEL7-文件权限管理
查看>>
apache服务器安装
查看>>
Search a 2D Matrix
查看>>
文件解析漏洞
查看>>
弹性成像的一些术语
查看>>
作业2
查看>>
vim 笔记
查看>>
MySQL的基本使用命令
查看>>
output 参数在存储过程中的用法
查看>>
大数加法和乘法(高精度)
查看>>
利用SynchronizationContext.Current在线程间同步上下文
查看>>
python各种类型转换-int,str,char,float,ord,hex,oct等
查看>>
sublime Text3 快捷键
查看>>
19 年书单
查看>>
不变模式
查看>>