Base64字符串转换为图片和图片PNG格式转JPG格式
package com.weige.entranceguard.utils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Base64Utils {
private static String JPG = "jpg";
private static String PNG = "png";
public static byte[] decode(String base64Str, String fileName, String filePath) {
File file = null;
File dir = new File(filePath);
if (!dir.exists() && !dir.isDirectory()) {
dir.mkdirs();
}
BufferedOutputStream bos = null;
FileOutputStream fos = null;
byte[] b = null;
BASE64Decoder decoder = new BASE64Decoder();
try {
b = decoder.decodeBuffer(replaceEnter(base64Str));
file = new File(filePath + "/" + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return b;
}
public static String encode(byte[] image) {
BASE64Encoder decoder = new BASE64Encoder();
return replaceEnter(decoder.encode(image));
}
public static String encode(String uri) {
BASE64Encoder encoder = new BASE64Encoder();
return replaceEnter(encoder.encode(uri.getBytes()));
}
public static byte[] imageTobyte(String path) {
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
public static String replaceEnter(String str) {
String reg = "[\n-\r]";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
return m.replaceAll("");
}
public static String checkImageBase64Format(String base64ImgData) {
byte[] b = Base64.getDecoder().decode(base64ImgData);
String type = "";
if (0x424D == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
type = "bmp";
} else if (0x8950 == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
type = "png";
} else if (0xFFD8 == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
type = "jpg";
}
return type;
}
public static boolean png2jpeg(String pngFilePath,String jpgFilePath){
File file = new File(pngFilePath);
file.canRead();
BufferedImage bi = null;
boolean flag = false;
try {
bi = ImageIO.read(file);
BufferedImage newBufferedImage = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(bi,0,0, Color.WHITE,null);
flag = ImageIO.write(newBufferedImage,"jpg",new File(jpgFilePath));
return flag;
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
public static String conver(String baseStr,String filePath){
String type = Base64Utils.checkImageBase64Format(baseStr);
if (JPG.equals(type)){
return baseStr;
}else if (PNG.equals(type)){
Long time = System.currentTimeMillis();
String pngName = time.toString()+".png";
decode(baseStr, pngName, filePath);
String pngPath = filePath + pngName;
String jpgName = time.toString()+".jpg";
String jpgPath = filePath + jpgName;
boolean isSuccess = png2jpeg(pngPath,jpgPath);
if (isSuccess){
byte[] jpgBytes =imageTobyte(jpgPath);
String str = encode(jpgBytes);
return str;
}
}
return baseStr;
}
}