Java之Base64工具类
下文笔者讲述Base64转文件,数组转Base64,Base64转图片的示例分享,如下所示
Base64
一组由Base64编码组成的字符串,我们将其称之为"Base64编码"。例: Base64工具类分享
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 = "D:\\test.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 jsonObject = new JSONObject();
jsonObject.put("QQ", "1000");
jsonObject.put("昵称", "猫猫");
String str = Base64Util.toBase64(jsonObject, 150, 150);
GenerateImage(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


