Commit 98091597 authored by shiyu's avatar shiyu

图片旋转处理

parent 5d33e647
......@@ -14,6 +14,7 @@ import com.wwdz.ch.admin.service.UploadService;
import com.wwdz.ch.admin.util.AuthSupport;
import com.wwdz.ch.core.storage.StorageService;
import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.util.ImgUtil;
import com.wwdz.ch.core.util.MediaUtil;
import com.wwdz.ch.core.util.ResponseUtil;
import com.wwdz.ch.core.validator.Order;
......@@ -31,8 +32,11 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import javax.validation.constraints.NotNull;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -74,7 +78,21 @@ public class AdminStorageController {
String originalFilename = file.getOriginalFilename();
long nowTime = System.currentTimeMillis();
String name = new BASE64Encoder().encode(Long.toString(nowTime).getBytes()) + RandomUtil.randomInt(1000);
String url = storageService.store(file.getInputStream(), file.getSize(), file.getContentType(),
InputStream targetInput = null;
try (InputStream inputStream = file.getInputStream()) {
BufferedImage image = ImageIO.read(inputStream);
ImgUtil.ImageOrientationWithNameResult orientationResult = ImgUtil.getImageFileNameWithWithRepaintInfo(file, image, null);
name = orientationResult.getImageName();
if (orientationResult.getNeedRepaint()) {
logger.info("当前图片需要进行重绘,旋转角度:【{}】", orientationResult.getAngel());
targetInput = ImgUtil.repaintImage2InputStream(image, orientationResult.getAngel(), orientationResult.getType());
}
} catch (Exception e) {
logger.error("get image size failed : ", e);
}
String url = storageService.store(targetInput, file.getSize(), file.getContentType(),
name, MediaUtil.isRotation(file));
Map<String, Object> data = new HashMap<>();
data.put("url", url);
......
This diff is collapsed.
package com.wwdz.ch.core.util;
import java.security.SecureRandom;
public class RandomUtil {
private static final SecureRandom RANDOM = new SecureRandom();
/**
* 获取随机20长度字符串
*
* @return 随机字符串
*/
public static String getRandom20Str() {
return getRandomStr(20);
}
/**
* 获取随机字符串
*
* @param length 字符串长度
* @return 随机字符串
*/
public static String getRandomStr(int length) {
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, length);
}
/**
* 获取随机int值
*
* @param broad 边界
* @return int随机值
*/
public static int getRandomInt(int broad){
return RANDOM.nextInt(broad);
}
public static int getRandomInt(){
return RANDOM.nextInt();
}
}
......@@ -10,6 +10,7 @@ import com.wwdz.ch.core.storage.StorageService;
import com.wwdz.ch.core.type.PageSearchResult;
import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.util.CacheUtil;
import com.wwdz.ch.core.util.ImgUtil;
import com.wwdz.ch.core.util.MediaUtil;
import com.wwdz.ch.db.bean.EsPageInfo;
import com.wwdz.ch.db.bean.ItemOfEs;
......@@ -1116,9 +1117,20 @@ public class ItemServiceImpl implements ItemService {
InputStream imageStream = bufferedImageToInputStream(bufferedImage, lastKey.substring(1));
qiniuStorage.store(imageStream, file.getSize(), file.getContentType(), keyName);*/
InputStream targetInput = null;
try (InputStream inputStream = file.getInputStream()) {
BufferedImage image = ImageIO.read(inputStream);
ImgUtil.ImageOrientationWithNameResult orientationResult = ImgUtil.getImageFileNameWithWithRepaintInfo(file, image, null);
if (orientationResult.getNeedRepaint()) {
logger.info("当前图片需要进行重绘,旋转角度:【{}】", orientationResult.getAngel());
targetInput = ImgUtil.repaintImage2InputStream(image, orientationResult.getAngel(), orientationResult.getType());
}
} catch (Exception e) {
logger.error("get image size failed : ", e);
}
String originalFilename = file.getOriginalFilename();
String url = storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename, MediaUtil.isRotation(file));
String url = storageService.store(targetInput, file.getSize(), file.getContentType(), originalFilename, MediaUtil.isRotation(file));
Map<String, Object> map = new HashMap<>();
map.put("url", url);
return Result.success(map);
......
......@@ -3,6 +3,7 @@ package com.wwdz.ch.wx.web;
import com.alibaba.fastjson.JSONObject;
import com.wwdz.ch.core.storage.StorageService;
import com.wwdz.ch.core.util.CharUtil;
import com.wwdz.ch.core.util.ImgUtil;
import com.wwdz.ch.core.util.MediaUtil;
import com.wwdz.ch.core.util.ResponseUtil;
import com.wwdz.ch.db.dao.dts.DtsStorageDao;
......@@ -22,6 +23,8 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
......@@ -78,7 +81,21 @@ public class WxStorageController {
logger.info("【请求开始】上传文件,请求参数,file:{}", file.getOriginalFilename());
String originalFilename = file.getOriginalFilename();
String url = storageService.store(file.getInputStream(), file.getSize(), file.getContentType(),
InputStream targetInput = null;
try (InputStream inputStream = file.getInputStream()) {
BufferedImage image = ImageIO.read(inputStream);
ImgUtil.ImageOrientationWithNameResult orientationResult = ImgUtil.getImageFileNameWithWithRepaintInfo(file, image, null);
if (orientationResult.getNeedRepaint()) {
logger.info("当前图片需要进行重绘,旋转角度:【{}】", orientationResult.getAngel());
targetInput = ImgUtil.repaintImage2InputStream(image, orientationResult.getAngel(), orientationResult.getType());
}
} catch (Exception e) {
logger.error("get image size failed : ", e);
}
String url = storageService.store(targetInput, file.getSize(), file.getContentType(),
originalFilename, MediaUtil.isRotation(file));
Map<String, Object> data = new HashMap<>();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment