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)成员内部类的实例化: 外部类名.内部类 ...
随机推荐
- B. Flag of Berland
B. Flag of Berland time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- bzoj2683(要改一点代码)&&bzoj1176: [Balkan2007]Mokia
仍然是一道cdq模版.. 那么对于一个询问,容斥一下分成四个,变成问(1,1)~(x,y),那么对于x,y,修改只有x'<=x&&y'<=y,才对询问有影响,那么加上读入顺 ...
- Burnside&Polya
以前只是直接用了这两个式子..今天才仔细看了证明..[网上的真是难懂啊 我看的几个博客地址(各有优缺): 其实如果能懂的话 只看博客B就可以了 首先是一些置换群方面的定义和性质 博客A:http:/ ...
- css3 vw -----解决页面滚动出现跳动的bug
100vw相对于浏览器的window.innerWidth,是浏览器的内部宽度,注意,滚动条宽度也计算在内!而100%是可用宽度,是不含滚动条的宽度. demo: h1{font-size:8vw;} ...
- CGAN 论文笔记
<Conditional Generative Adversarial Nets>: 发表于2014 computer Science.作者提出了最简单的条件控制GAN模型,在D和G两个模 ...
- Mybatis invalid comparison: java.util.Date and java.lang.String
Mybatis的实体类是java.utils.Date类型,而在Mybatis的XML文件中,使用if判断了,不需要判断是否等于空字符串这种判断,需要人真一些 相关:https://blog.csdn ...
- P2093 [国家集训队]JZPFAR(KDTree)
传送门 类似于p4357 不过因为距离相等的时候要优先选择序号小的,所以要重载一下运算符 //minamoto #include<bits/stdc++.h> #define R regi ...
- 文件系统访问控制ACL设置
1.传统Linux文件系统权限的问题 传统Linux文件系统有三类用户:文件属主-u,组用户-g,其它用户-o,以及三种访问权限:读-r,写-w,执行或目录进入-x,但很多时候并不能满足对文件访问的细 ...
- Kafaka入门篇
1.Apache Kafka - 简介 Apache Kafka是一个分布式发布 - 订阅消息系统和一个强大的队列,可以处理大量的数据,并使您能够将消息从一个端点传递到另一个端点. Kafka适合离线 ...
- Appium教程---Client/Server Architecture
appium的核心其实是一个暴露了一系列REST API的server. 这个server的功能其实很简单:监听一个端口,然后接收由client发送来的command.翻译这些command,把这些c ...