import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import org.apache.commons.codec.binary.Base64;
import Decoder.BASE64Decoder;
import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* Base64处理类
*/
public class Base64Util {
/**
* 将base64格式的图片转换成本地图片文件
*
* @method base64ToFileByte
* @param fileJson
* base64字符串
* @param fileName
* 本地临时文件名(如1.png)
* @return File 本地临时文件
* @throws Exception
*/
public static File base64ToFile(String fileJson, String fileName)
throws Exception {
String prvPath = Thread.currentThread().getContextClassLoader()
.getResource("/").getPath();
String path = prvPath.substring(1, prvPath.indexOf("classes"))
+ "temp/";
String tempPath = path + fileName;
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
file = new File(tempPath);
if (file.exists()) {
file.delete();
}
Base64 base64 = new Base64();
byte[] b = base64.decode(fileJson);
OutputStream out = new FileOutputStream(file);
out.write(b);
out.flush();
out.close();
return file;
}
/**
* 将指定信息写入base64字符串
*
* @method toBase64
* @param contents
* @param width
* @param height
* @return
* @throws Exception
*/
public static String toBase64(JSONObject obj, int width, int height)
throws Exception {
Hashtable<Object, Object> hints = new Hashtable<Object, Object>();
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// 指定编码格式
BitMatrix byteMatrix = new MultiFormatWriter().encode(new String(obj
.toJSONString().getBytes("UTF-8"), "iso-8859-1"),
BarcodeFormat.QR_CODE, width, height);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(byteMatrix, "png", bao);
String ok = Base64Code(bao.toByteArray());
bao.flush();
bao.close();
return ok;
}
/**
* base64加密
* @method Base64Code
* @param b
* @return
*/
public static String Base64Code(byte[] b) {
Base64 encoder = new Base64();
StringBuilder pictureBuffer = new StringBuilder();
pictureBuffer.append(encoder.encodeAsString(b));
String codeBase64 = pictureBuffer.toString();
return codeBase64;
}
// base64字符串转化成图片
public static boolean GenerateImage(String imgStr) { // 对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) // 图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 生成jpeg图片
String imgFilePath = "C:\\new.jpg";// 新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
public static void main(String[] args) {
try {
JSONObject container1 = new JSONObject();
container1.put("QQ", "2418741378");
container1.put("昵称", "路人甲");
String aa = Base64Util.toBase64(container1, 150, 150);
GenerateImage(aa);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Base64处理类
于 2018-03-05 22:44:56 首次发布