Java common tools --- image photo processing tools, Json Tools

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 {
	/**
	 @ * Description: Base64 decoded picture and generating 
			OUT = new new a FileOutputStream (imgfile); 
			out.write (bytes); 
			out.flush ();
	 @ * Into the reference: @param imgStr 
	 * @ parameters into: @param imgfile 
	 * @ parameters into: @throws IOException 
	 * @ illustrating parameters: void 
	 * / 
	public static void generateImage (imgStr String, String imgfile) throws IOException { 
		Base64Decoder = new new Decoder Base64Decoder (); 
		// the Base64 decoding 
		byte [] bytes; 
		the OutputStream OUT = null; 
		the try { 
			bytes = decoder.decodeBuffer (imgStr); 
			for (int I = 0; I <bytes.length; I ++) { 
				IF (bytes [i] <0) {// adjust abnormality data 
					bytes [I] + = 256; 
				} 
			} 
			// generate picture 
		} the catch (IOException E) { 
			the throw new new IOException (); 
		} the finally { 
			! IF (OUT = null) { 
				try { 
					the out.close (); 
				} the catch (IOException E) { 
					e.printStackTrace (); 
				} 
			} 
		} 

	} 

	/ ** 
	 * @throws IOException 
	 * @ description: The base obtained after encoding the image path 
	 * @ parameters into: @param imgFilePath 
	 * @ parameters into: @return 
	 * @ illustrating parameters: String 
	 * / 
	public static String getImageStr (String imgFilePath) throws IOException the image file {// byte array into a string, Base64 encoding process and subjected to 
		byte [] Data = null; 

		// read byte array images 
		the try { 
			the InputStream = new new in the FileInputStream (imgFilePath);
			= new new byte Data [in.available ()]; 
			in.read (Data); 
			in.close (); 
		} the catch (IOException E) { 
			the throw new new IOException (); 
		} 

		// byte array Base64 encoding 
		BASE64Encoder encoder = Base64Encoder new new (); 
	public static String imgAngleRevolve (String base64In, int angle) throws IOException { 
		ByteArrayOutputStream os ByteArrayOutputStream new new = (); 
		the try {
		return encoder.encode (data); // returns the byte array Base64 encoded string 
	} 

	/ ** 
	 * @throws IOException 
	 * @ Description: Image rotation 
	 * @ parameters into: @param base64In incoming image Base64 
	 * @ into reference: @param angle degree image rotation 
	 * @ to the Senate: @throws Exception 
	 * @ out parameters: String outgoing picture Base64 
	 * /
			. Thumbnails.of (base64ToIo (base64In)) Scale (1.0) .rotate (angle) .toOutputStream (OS); 
		} the catch (IOException E) { 
			the throw new new IOException (); 
		} 
		byte [] os.toByteArray BS = (); 
		S = Base64Encoder new new String () encode (BS);. 
		return S; 
	} 

	/ ** 
	 * @ description: base64 stream into io 
	 * @ parameters into: @param strBase64 
	 * @ parameters into: @return 
	 * @ parameters into: @ IOException throws 
	 * @ illustrating parameters: the InputStream 
	 * / 
	public static base64ToIo the InputStream (String strBase64) throws IOException { 
		// decoded and then converted to byte file 
		byte [] bytes = new BASE64Decoder ( ) decodeBuffer (strBase64); // will. string into a byte array 
		return new new A ByteArrayInputStream (bytes); 
	} 
}






 
Package com.jarvis.base.util; 

Import java.io.BufferedReader; 
Import java.io.BufferedWriter; 
Import java.io.File; 
Import a java.io.FileInputStream; 
java.io.FileOutputStream Import; 
Import java.io.IOException; 
Import the java.io.InputStreamReader; 
Import java.io.OutputStreamWriter; 
Import java.util.List; 
Import a 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 tools 
 * @version V1.0   
 * / 
public class FastJsonUtil { 

	Private static Final SerializeConfig config; 

	static { 
		config = new new SerializeConfig (); 
		config.put (java.util.Date.class, new new JSONLibDataFormatSerializer ()); // use and json-lib date output formats compatible 
		config.put (java.sql.Date.class, new JSONLibDataFormatSerializer () ) ; // use json-lib and output formats compatible date 
	} 

	Private static Final SerializerFeature [] = {SerializerFeature.WriteMapNullValue Features, // output vacant field 
			SerializerFeature.WriteNullListAsEmpty, // list if the field is null, the output is [], and not null 
			SerializerFeature.WriteNullNumberAsZero, // If the numeric field is null, the output is 0, instead of null 
			SerializerFeature.WriteNullBooleanAsFalse, // Boolean field if null, the output is false, rather than null 
			SerializerFeature.WriteNullStringAsEmpty, character type field // If null, the output is "" not null 
			SerializerFeature.PrettyFormat // need to format output Json data 
	}; 

	/ ** 
	 * @param Object 
	 * @return the return: String the Description: the object into a subject to Json 
	 * / 
	public static String the toJSONString (Object Object) { 
		return JSON.toJSONString (Object, config, Features); 
	} 

	/ ** 
	 * @param Object
	 * @Return Return: String Description: json-lib use and compatible with the output format of the date 
	 * / 
	public static String toJSONNoFeatures (Object object) {
		JSON.toJSONString return (Object, config); 
	} 

	/ ** 
	 * @param jsonStr 
	 * @return the Return: the Description Object: to convert data into Json the JSONObject 
	 * / 
	public static toJsonObj the JSONObject (String jsonStr) { 
		return (the JSONObject) the JSON.parse (jsonStr); 
	} 
	
	/ ** 
	 * @param jsonStr 
	 * @param clazz 
	 * @return the Return: T the Description: converting the data into Json Object 
	 * / 
	public static <T> T toBean (String jsonStr, Class <T> clazz) { 
		JSON.parseObject return (jsonStr, clazz); 
	} 

	/ ** 
	 * @param jsonStr 
	 * @return the return: Object [] the Description: Json data into the array
	 */
	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 into the serialized objects 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();
		JSONObject.parseObject return (strBuilder.toString (), CLS); 
	} 

	/ ** 
	 * Author: Jack Time: 2017 Nian 9 2 December, afternoon 4:22:30 
	 * 
	 * @param CLS 
	 * @param filename 
	 * @return 
	 * @ throws IOException 
	 * the return: T the Description: converting Json data file into Object object 
	 * / 
	public static <T> T readJsonFromFile (Class <T> CLS, String filename) throws IOException { 
		return readJsonFromFile (CLS, new new file (filename) ); 
	} 

	/ ** 
	 * Author: Jack Time: 2017 Nian 9 2 December, afternoon 4:23:06 
	 * 
	 * @param typereference 
	 * @param file 
	 * @return 
	 * @throws IOException 
	 * the Return: T the Description: read from a file remove Object 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));
	}

}

  

Original: https: //blog.csdn.net/zhang150114/article/details/90411800

Guess you like

Origin www.cnblogs.com/qbdj/p/10974692.html
Recommended