MultipartFile 转 Base64字符串
public static String multiPartFileToBase64String(
MultipartFile multiPartFile,
boolean isPrefix) {
String baseStr = null;
BASE64Encoder encoder = new BASE64Encoder();
try {
baseStr = encoder.encode(multiPartFile.getBytes());
baseStr = baseStr.replaceAll("\r\n", "");
if (isPrefix) {
baseStr = "data:image/jpeg;base64," + baseStr;
}
} catch (IOException e) {
}
return baseStr;
}
图片url 转 Base64字符串
public static String imageUrlToBase64(String imagePath, boolean isPrefix) {
ByteArrayOutputStream outPut = new ByteArrayOutputStream();
byte[] data = new byte[1024];
InputStream inStream = null;
try {
URL url = new URL(imagePath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(10 * 1000);
if (conn.getResponseCode() != 200) {
return "fail";
}
inStream = conn.getInputStream();
int len = -1;
while ((len = inStream.read(data)) != -1) {
outPut.write(data, 0, len);
}
byte[] encode = Base64.getEncoder().encode(outPut.toByteArray());
if (isPrefix) {
String res = String.format("%s%s", "data:image/jpeg;base64,", new String(encode));
return res;
}
return new String(encode);
} catch (Exception e) {
} finally {
try {
inStream.close();
} catch (Exception e) {
}
}
}
图片url 转 MultipartFile
public static MultipartFile imgUrlToMultiPartFile(String url) {
URLConnection connection = null;
InputStream inputStream = null;
byte[] bytes = null;
try {
connection = new URL(url).openConnection();
inputStream = connection.getInputStream();
bytes = StreamUtils.copyToByteArray(inputStream);
} catch (Exception e) {
log.error(e.toString());
}
String contentType = connection.getContentType();
String[] contentTypes = contentType.split("/");
String filename = "file." + contentTypes[1];
return new ByteArrayMultipartFile(bytes, filename, contentType);
}
图片url 转 File流
public static File urlToFile(URL url) {
InputStream is = null;
File file = null;
FileOutputStream fos = null;
try {
file = File.createTempFile("tmp", null);
URLConnection urlConn = null;
urlConn = url.openConnection();
is = urlConn.getInputStream();
fos = new FileOutputStream(file);
byte[] buffer = new byte[4096];
int length;
while ((length = is.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
return file;
} catch (Exception e) {
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
}
}
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
}
}
}
}
base64图片 压缩
引入依赖
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.20</version>
</dependency>
代码编写
public static String compressPic(String imgBase64, int targetSize, boolean throwException){
if(StringUtils.isEmpty(imgBase64)){
log.error("imgBase64 param not null ......");
if(throwException){
throw new MixException(BusinessErrorEnum.BASE_PARAM_FORMAT);
}
}
targetSize = targetSize <= 0 ? 100 : targetSize;
String newBase64 = imgBase64;
InputStream inputStream = null;
ByteArrayOutputStream outputStream = null;
try {
if(imgBase64.startsWith("data:")){
imgBase64 = imgBase64.split(",")[1];
}
byte[] imageBytes = Base64.getDecoder().decode(imgBase64);
float imageOriginalSize = (float) imageBytes.length / 1024;
log.info("--------------compressedImageBytes#imageOriginalSize(Byte) = {}", imageOriginalSize);
if(imageOriginalSize < targetSize){
return newBase64;
}
outputStream = new ByteArrayOutputStream();
int index = 0;
byte[] compressedImageBytes = imageBytes;
BigDecimal scale = new BigDecimal(1.0f);
float quality = targetSize / imageOriginalSize;
while (scale.floatValue() >= 0.1 && compressedImageBytes.length / 1024 > targetSize){
inputStream = new ByteArrayInputStream(imageBytes);
outputStream.reset();
Thumbnails.of(inputStream)
.scale(scale.floatValue())
.outputFormat("jpg")
.outputQuality(quality)
.toOutputStream(outputStream);
compressedImageBytes = outputStream.toByteArray();
scale = scale.subtract(new BigDecimal(0.05f));
++index;
log.info("--------------compressedImageBytes#index = {},scale = {}, nowSize = {}", index, scale.floatValue(), compressedImageBytes.length / 1024);
}
byte[] encode = Base64.getEncoder().encode(compressedImageBytes);
newBase64 = new String(encode);
log.info("--------------compressedImageBytes#nowImgSize(Byte) = {}", compressedImageBytes.length / 1024);
}catch (Exception e){
log.error("compressPic business error:", e);
if(throwException){
throw new MixException(BusinessErrorEnum.BASE_SERVER_UNKNOWN);
}
}finally {
try {
if(inputStream != null){
inputStream.close();
}
if(outputStream != null){
outputStream.close();
}
} catch (Exception e) {
log.error("compressPic close stream error:", e);
if(throwException){
throw new MixException(BusinessErrorEnum.BASE_SERVER_UNKNOWN);
}
}
}
return newBase64;
}
抠图
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RasterFormatException;
import java.io.*;
@Slf4j
public class ImageUtil {
private final static String PNG = "png";
private final static String JPG = "jpg";
private final static String JPEG = "jpeg";
private final static long TWO_MB = 1024 * 1024 * 2;
public static MultipartFile slicingImage(InputStream file, String fileType, FaceDetectVo faceDetectVo){
Integer width = faceDetectVo.getW();
Integer high = faceDetectVo.getH();
Integer x = faceDetectVo.getX();
Integer y = faceDetectVo.getY();
try {
BufferedImage originalImage = ImageIO.read(file);
int maxWidth = originalImage.getWidth();
int maxHigh= originalImage.getHeight();
Color backgroundColor = getDominantColor(originalImage);
x = x > 0 ? x : 0;
y = y > 0 ? y : 0;
width = width > 0 ? width : 0;
high = high > 0 ? high : 0;
width = x + width > maxWidth ? maxWidth - x: width;
high = y + high > maxHigh ? maxHigh - y : high;
BufferedImage subImage = originalImage.getSubimage(x, y, width, high);
if (PNG.equals(fileType)){
BufferedImage imageWithBackground = new BufferedImage(width, high, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = imageWithBackground.createGraphics();
g2d.setColor(backgroundColor);
g2d.fillRect(0,0, width, high);
g2d.drawImage(subImage, 0, 0, null);
g2d.dispose();
MultipartFile multipartFile = convertBufferedImageToMultipartFile(subImage, "data.png", PNG);
if (multipartFile.getSize() > TWO_MB){
multipartFile = convertBufferedImageToMultipartFile(subImage, "data.jpg", JPG);
log.info("抠图大小大于2mb, 尝试转为jpg, 文件大小为: {}", multipartFile.getSize());
}
System.out.println("图片切分完成!");
return multipartFile;
}
BufferedImage imageWithBackground = new BufferedImage(width, high, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = imageWithBackground.createGraphics();
g2d.setColor(backgroundColor);
g2d.fillRect(0,0, width, high);
g2d.drawImage(subImage, 0, 0, null);
g2d.dispose();
MultipartFile multipartFile = convertBufferedImageToMultipartFile(imageWithBackground, "data." + fileType, fileType);
System.out.println("图片切分完成!");
return multipartFile;
} catch (IOException e) {
log.error("图片切分失败!", e);
} catch (RasterFormatException e) {
log.info("切分坐标超出图片范围,请检查坐标设置。");
}
return null;
}
private static Color getDominantColor(BufferedImage image) {
return new Color(image.getRGB(0, 0));
}
private static MultipartFile convertBufferedImageToMultipartFile(BufferedImage image, String fileName, String fileType) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, fileType, baos);
InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
return new MultipartFile() {
@Override
public String getName() {
return fileName;
}
@Override
public String getOriginalFilename() {
return fileName;
}
@Override
public String getContentType() {
return "image/" + fileType;
}
@Override
public boolean isEmpty() {
return baos.size() == 0;
}
@Override
public long getSize() {
return baos.size();
}
@Override
public byte[] getBytes() throws IOException {
return baos.toByteArray();
}
@Override
public InputStream getInputStream() throws IOException {
return inputStream;
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
}
};
}
public static String getFileType(MultipartFile file) {
String fileName = file.getOriginalFilename().toLowerCase();
if (fileName.endsWith(StringPool.DOT + PNG)) {
return PNG;
}
if (fileName.endsWith(StringPool.DOT + JPG)) {
return JPG;
}
if (fileName.endsWith(StringPool.DOT + JPEG)) {
return JPEG;
}else {
return PNG;
}
}
}