Java常用工具类---image图片处理工具类、Json工具类
package com.jarvis.base.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import net.coobird.thumbnailator.Thumbnails;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
*
*
* @Title: ImageHelper.java
* @Package com.jarvis.base.util
* @Description:图片处理工具类。
* @version V1.0
*/
@SuppressWarnings("restriction")
public class ImageHelper {
/**
* @描述:Base64解码并生成图片
* @入参:@param imgStr
* @入参:@param imgFile
* @入参:@throws IOException
* @出参:void
*/
public static void generateImage(String imgStr, String imgFile) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
// Base64解码
byte[] bytes;
OutputStream out = null;
try {
bytes = decoder.decodeBuffer(imgStr);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
// 生成图片
out = new FileOutputStream(imgFile);
out.write(bytes);
out.flush();
} catch (IOException e) {
throw new IOException();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @throws IOException
* @描述:根据路径得到base编码后图片
* @入参:@param imgFilePath
* @入参:@return
* @出参:String
*/
public static String getImageStr(String imgFilePath) throws IOException {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
byte[] data = null;
// 读取图片字节数组
try {
InputStream in = new FileInputStream(imgFilePath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
throw new IOException();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);// 返回Base64编码过的字节数组字符串
}
/**
* @throws IOException
* @描述:图片旋转
* @入参:@param base64In 传入的图片base64
* @入参:@param angle 图片旋转度数
* @入参:@throws Exception
* @出参:String 传出的图片base64
*/
public static String imgAngleRevolve(String base64In, int angle) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
Thumbnails.of(base64ToIo(base64In)).scale(1.0).rotate(angle).toOutputStream(os);
} catch (IOException e) {
throw new IOException();
}
byte[] bs = os.toByteArray();
String s = new BASE64Encoder().encode(bs);
return s;
}
/**
* @描述:base64转为io流
* @入参:@param strBase64
* @入参:@return
* @入参:@throws IOException
* @出参:InputStream
*/
public static InputStream base64ToIo(String strBase64) throws IOException {
// 解码,然后将字节转换为文件
byte[] bytes = new BASE64Decoder().decodeBuffer(strBase64); // 将字符串转换为byte数组
return new ByteArrayInputStream(bytes);
}
}
package com.jarvis.base.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
/**
*
*
* @Title: FastJsonUtil.java
* @Package com.jarvis.base.util
* @Description:fastjson工具类
* @version V1.0
*/
public class FastJsonUtil {
private static final SerializeConfig config;
static {
config = new SerializeConfig();
config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
}
private static final SerializerFeature[] features = { SerializerFeature.WriteMapNullValue, // 输出空置字段
SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null
SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null
SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null
SerializerFeature.WriteNullStringAsEmpty, // 字符类型字段如果为null,输出为"",而不是null
SerializerFeature.PrettyFormat //是否需要格式化输出Json数据
};
/**
* @param object
* @return Return:String Description:将对象转成成Json对象
*/
public static String toJSONString(Object object) {
return JSON.toJSONString(object, config, features);
}
/**
* @param object
* @return Return:String Description:使用和json-lib兼容的日期输出格式
*/
public static String toJSONNoFeatures(Object object) {
return JSON.toJSONString(object, config);
}
/**
* @param jsonStr
* @return Return:Object Description:将Json数据转换成JSONObject
*/
public static JSONObject toJsonObj(String jsonStr) {
return (JSONObject) JSON.parse(jsonStr);
}
/**
* @param jsonStr
* @param clazz
* @return Return:T Description:将Json数据转换成Object
*/
public static <T> T toBean(String jsonStr, Class<T> clazz) {
return JSON.parseObject(jsonStr, clazz);
}
/**
* @param jsonStr
* @return Return:Object[] Description:将Json数据转换为数组
*/
public static <T> Object[] toArray(String jsonStr) {
return toArray(jsonStr, null);
}
/**
* @param jsonStr
* @param clazz
* @return Return:Object[] Description:将Json数据转换为数组
*/
public static <T> Object[] toArray(String jsonStr, Class<T> clazz) {
return JSON.parseArray(jsonStr, clazz).toArray();
}
/**
* @param jsonStr
* @param clazz
* @return Return:List<T> Description:将Json数据转换为List
*/
public static <T> List<T> toList(String jsonStr, Class<T> clazz) {
return JSON.parseArray(jsonStr, clazz);
}
/**
* 将javabean转化为序列化的JSONObject对象
*
* @param keyvalue
* @return
*/
public static JSONObject beanToJsonObj(Object bean) {
String jsonStr = JSON.toJSONString(bean);
JSONObject objectJson = (JSONObject) JSON.parse(jsonStr);
return objectJson;
}
/**
* json字符串转化为map
*
* @param s
* @return
*/
public static Map<?, ?> stringToCollect(String jsonStr) {
Map<?, ?> map = JSONObject.parseObject(jsonStr);
return map;
}
/**
* 将map转化为string
*
* @param m
* @return
*/
public static String collectToString(Map<?, ?> map) {
String jsonStr = JSONObject.toJSONString(map);
return jsonStr;
}
/**
* @param t
* @param file
* @throws IOException
* Return:void Description:将对象的Json数据写入文件。
*/
public static <T> void writeJsonToFile(T t, File file) throws IOException {
String jsonStr = JSONObject.toJSONString(t, SerializerFeature.PrettyFormat);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(jsonStr);
bw.close();
}
/**
* @param t
* @param filename
* @throws IOException
* Return:void Description:将对象的Json数据写入文件。
*/
public static <T> void writeJsonToFile(T t, String filename) throws IOException {
writeJsonToFile(t, new File(filename));
}
/**
* @param cls
* @param file
* @return
* @throws IOException
* Return:T Description:将文件中的Json数据转换成Object对象
*/
public static <T> T readJsonFromFile(Class<T> cls, File file) throws IOException {
StringBuilder strBuilder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line = null;
while ((line = br.readLine()) != null) {
strBuilder.append(line);
}
br.close();
return JSONObject.parseObject(strBuilder.toString(), cls);
}
/**
* Author:Jack Time:2017年9月2日下午4:22:30
*
* @param cls
* @param filename
* @return
* @throws IOException
* Return:T Description:将文件中的Json数据转换成Object对象
*/
public static <T> T readJsonFromFile(Class<T> cls, String filename) throws IOException {
return readJsonFromFile(cls, new File(filename));
}
/**
* Author:Jack Time:2017年9月2日下午4:23:06
*
* @param typeReference
* @param file
* @return
* @throws IOException
* Return:T Description:从文件中读取出Json对象
*/
public static <T> T readJsonFromFile(TypeReference<T> typeReference, File file) throws IOException {
StringBuilder strBuilder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line = null;
while ((line = br.readLine()) != null) {
strBuilder.append(line);
}
br.close();
return JSONObject.parseObject(strBuilder.toString(), typeReference);
}
/**
* Author:Jack Time:2017年9月2日下午4:23:11
*
* @param typeReference
* @param filename
* @return
* @throws IOException
* Return:T Description:从文件中读取出Json对象
*/
public static <T> T readJsonFromFile(TypeReference<T> typeReference, String filename) throws IOException {
return readJsonFromFile(typeReference, new File(filename));
}
}
---------------------
Java常用工具类---image图片处理工具类、Json工具类的更多相关文章
- java常用英语单词
abstract (关键字) 抽象 ['.bstr.kt] access vt.访问,存取 ['.kses]'(n.入口,使用权) algorithm n.算法 ['.lg.riem] annotat ...
- Java常用英语汇总(面试必备)
Java常用英语汇总(面试必备) abstract (关键字) 抽象 ['.bstr.kt] access vt.访问,存 ...
- java常用开发工具类之 图片水印,文字水印,缩放,补白工具类
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- Java常用类归纳(Object、System、Properties、包装类和工具类等等)
Object类 Object 是类层次结构的根类.每个类都使用 Object 作为超类,所有对象(包括数组)都实现这个类的方法.了解Object的方法是很有必要的. protected Object ...
- 写一个java常用的加密工具类
1.叙述 java security包下有很多加密算法类,我们可以很简单的调用它们.他们虽然功能很全,但是使用起来步骤有些繁琐.我在这里封装来一些常用的加密算法及他们常用的一些方法,来简化代码. 工具 ...
- JavaEE-实验一 Java常用工具类编程
该博客仅专为我的小伙伴提供参考而附加,没空加上代码具体解析,望各位谅解 1. 使用类String类的分割split 将字符串 “Solutions to selected exercises ca ...
- Java常用工具类整理
字符数组转String package com.sunsheen.hcc.fabric.utils; /** * 字符数组工具 * @author WangSong * */ public class ...
- Java 常用工具类之 String 类
String 类的特点: 字符串对象一旦被初始化就不会被改变. //以下代码的区别: String s = "abc"; // 在常量池中创建一个字符串对象, 池中没有就建立, 池 ...
- Java生成带小图标的二维码-google zxing 工具类
近期一直忙于开发微信商城项目,应客户要求,要开发个有图标的二维码.经过两次改版,终于实现了该功能(第一次没有小图标,这次才整合好的),如下是完整代码 . 该代码使用Java7开发,另外使用 core- ...
- java内部类、接口、集合框架、泛型、工具类、实现类
.t1 { background-color: #ff8080; width: 1100px; height: 40px } 一.内部类 1.成员内部类. (1)成员内部类的实例化: 外部类名.内部类 ...
随机推荐
- HDU3488 Tour —— 二分图最大权匹配 KM算法
题目链接:https://vjudge.net/problem/HDU-3488 Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit ...
- YTU 2844: 改错题A-看电影
2844: 改错题A-看电影 时间限制: 1 Sec 内存限制: 128 MB 提交: 69 解决: 47 题目描述 注:本题只需要提交标记为修改部分之间的代码,请按照C++方式提交. 小平家长为 ...
- 日元兑换——国内兑换需要护照和签证,国外的机场有兑换ATM
在中国换日元:在中国的商业银行都可以换取日元,但是换汇者必须持有护照.签证等材料.换汇的汇率是按照即时汇率进行结算,如是现钞则按钞买价兑换,另外还要收取0.5%的手续费. 在日本换日元:除了在日本银行 ...
- java抛出异常后,后续代码是否可继续执行
参考:https://www.cnblogs.com/wangyingli/p/5912269.html 仅此可正常执行异常后内容 try{ throw new Exception("参数越 ...
- 并不对劲的bzoj2638
为了反驳很对劲的太刀流,并不对劲的片手流决定与之针锋相对. 很对劲的太刀流-> 2638: 黑白染色 Time Limit: 20 Sec Memory Limit: 256 MBSubmit ...
- Java Socket传输数据的文件系统介绍
转自:http://developer.51cto.com/art/201003/189963.htm Java Socket传输数据在进行的时候有很多的事情需要我们不断的进行有关代码的学习.只有不断 ...
- 【181】IDL 代码从 Windows 转移到 Linux
文件夹分隔符,Windows 是“/”,Linux 是“\”,按照程序,需要修改 通过 bash 运行 *.pro 文件,貌似只能运行没有参数的,有参数的需要写入到文件中 idl 的文件不能用大写字母 ...
- 12_传智播客iOS视频教程_注释和函数的定义和调用
OC的注释和C语言的注释一模一样.它也分单行注释和多行注释. OC程序里面当然可以定义一个函数.并且定义的方式方法和调用的方式方法和我们C语言是一模一样的.OC有什么好学的?一样还学个什么呢? 重点是 ...
- Entity Framework 学习整理
MSDN: http://msdn.microsoft.com/en-us/data/aa937723 台湾博客: http://www.dotblogs.com.tw/yc421206/ http: ...
- 【已解决】python中文字符乱码(GB2312,GBK,GB18030相关的问题)
http://againinput4.blog.163.com/blog/static/1727994912011111011432810/ [已解决]python中文字符乱码(GB2312,GB ...