Commit 5d33e647 authored by shiyu's avatar shiyu

图片旋转判断

parent c57cfa0b
......@@ -2,6 +2,11 @@ package com.wwdz.ch.admin.controller;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.github.pagehelper.PageInfo;
import com.wwdz.ch.admin.annotation.RequiresPermissionsDesc;
import com.wwdz.ch.admin.service.AdminStorageService;
......@@ -9,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.MediaUtil;
import com.wwdz.ch.core.util.ResponseUtil;
import com.wwdz.ch.core.validator.Order;
import com.wwdz.ch.core.validator.Sort;
......@@ -62,18 +68,16 @@ public class AdminStorageController {
@RequiresPermissions("admin:storage:create")
@RequiresPermissionsDesc(menu = { "系统管理", "对象存储" }, button = "上传")
@PostMapping("/create")
public Object create(@RequestParam("file") MultipartFile file) throws IOException {
public Object create(@RequestParam("file") MultipartFile file) throws Exception {
logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 系统管理->对象存储->上传,请求参数,file:{}", file.getOriginalFilename());
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(),
name);
name, MediaUtil.isRotation(file));
Map<String, Object> data = new HashMap<>();
data.put("url", url);
logger.info("【请求结束】系统管理->对象存储->查询:响应结果:{}", JSONObject.toJSONString(data));
return ResponseUtil.ok(data);
}
......
......@@ -22,6 +22,7 @@ import javax.imageio.ImageIO;
import com.wwdz.ch.core.storage.StorageService;
import com.wwdz.ch.core.system.SystemConfig;
import com.wwdz.ch.core.util.MediaUtil;
import com.wwdz.ch.core.util.PriceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -65,7 +66,7 @@ public class QCodeService {
byte[] imageData = drawPicture(inputStream, goodPicUrl,goodName,price);
ByteArrayInputStream inputStream2 = new ByteArrayInputStream(imageData);
// 存储分享图
String url = storageService.store(inputStream2, imageData.length, "image/jpeg", getKeyName(goodId));
String url = storageService.store(inputStream2, imageData.length, "image/jpeg", getKeyName(goodId), false);
logger.info("创建商品分享图 URL:{}",url);
return url;
} catch (WxErrorException e) {
......@@ -214,7 +215,7 @@ public class QCodeService {
FileInputStream inputStream = new FileInputStream(file);
fileStream = new FileInputStream(file);
url = storageService.store(inputStream, fileStream.available(), "image/jpeg", getShareUserKey(userId));
url = storageService.store(inputStream, fileStream.available(), "image/jpeg", getShareUserKey(userId), false);
logger.info("创建商品分享图 URL:{}",url);
} catch (WxErrorException e) {
......@@ -276,7 +277,7 @@ public class QCodeService {
byte[] imageData = drawTopicPicture(inputStream, picUrl,subtitle,price);
ByteArrayInputStream inputStream2 = new ByteArrayInputStream(imageData);
// 存储分享图
String url = storageService.store(inputStream2, imageData.length, "image/jpeg", getTopicKey(topicId));
String url = storageService.store(inputStream2, imageData.length, "image/jpeg", getTopicKey(topicId),false);
logger.info("创建活动主题分享图 URL:{}",url);
return url;
} catch (WxErrorException e) {
......@@ -362,7 +363,7 @@ public class QCodeService {
byte[] imageData = drawBrandPicture(inputStream, picUrl,name,defaultCategory);
ByteArrayInputStream inputStream2 = new ByteArrayInputStream(imageData);
// 存储分享图
String url = storageService.store(inputStream2, imageData.length, "image/jpeg", getBrandKey(brandId));
String url = storageService.store(inputStream2, imageData.length, "image/jpeg", getBrandKey(brandId), false);
logger.info("创建品牌商铺主题分享图 URL:{}",url);
return url;
} catch (WxErrorException e) {
......
......@@ -67,28 +67,12 @@ public class StorageService {
* @param fileName
* 文件索引名
*/
public String store(InputStream inputStream, long contentLength, String contentType, String fileName) {
public String store(InputStream inputStream, long contentLength, String contentType, String fileName, boolean rotation) {
BufferedImage bufferedImage = null;
boolean rotation = false;
try {
bufferedImage = ImageIO.read(inputStream);
Metadata metadata = ImageMetadataReader.readMetadata(inputStream);
Directory imageDirectory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
int orientation = 1;
try{
orientation = imageDirectory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
} catch (MetadataException ignore) {
}
switch (orientation) {
case 5: // - PI/2 and Flip X
case 6: // -PI/2 and -width
case 7: // PI/2 and Flip
case 8: // PI / 2
rotation = true;
}
} catch (Exception e) {
throw new RuntimeException(e);
logger.error("存储图片error:{}", e);
}
String type = MediaTypeEnum.getByMime(contentType) == null ? MediaTypeEnum.IMAGE_JPG.getExt() : MediaTypeEnum.getByMime(contentType).getExt();
String keyName;
......@@ -104,6 +88,8 @@ public class StorageService {
return generateUrl(keyName);
}
private String generateKey(String originalFilename) {
int index = originalFilename.lastIndexOf('.');
String suffix = originalFilename.substring(index);
......
package com.wwdz.ch.core.util;
import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.wwdz.ch.core.consts.CommConsts;
import com.wwdz.ch.core.consts.MediaTypeEnum;
import com.wwdz.ch.core.entity.ImageInfo;
......@@ -7,6 +11,7 @@ import com.xxdxxs.utils.CommonUtils;
import com.xxdxxs.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
......@@ -91,6 +96,31 @@ public class MediaUtil {
return null;
}
/**
* 图片是否旋转
* @param file
* @return
*/
public static boolean isRotation(MultipartFile file){
boolean rotation = false;
int orientation = 1;
try{
Metadata metadata = ImageMetadataReader.readMetadata(file.getInputStream());
Directory imageDirectory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
orientation = imageDirectory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
} catch (Exception e) {
logger.error("读取图片方向error:{}", e);
}
switch (orientation) {
case 5: // - PI/2 and Flip X
case 6: // -PI/2 and -width
case 7: // PI/2 and Flip
case 8: // PI / 2
rotation = true;
}
return rotation;
}
/**
* 获取展示图片
......
......@@ -1116,8 +1116,9 @@ public class ItemServiceImpl implements ItemService {
InputStream imageStream = bufferedImageToInputStream(bufferedImage, lastKey.substring(1));
qiniuStorage.store(imageStream, file.getSize(), file.getContentType(), keyName);*/
String originalFilename = file.getOriginalFilename();
String url = storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename);
String url = storageService.store(file.getInputStream(), 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.MediaUtil;
import com.wwdz.ch.core.util.ResponseUtil;
import com.wwdz.ch.db.dao.dts.DtsStorageDao;
import com.wwdz.ch.db.domain.Storage;
......@@ -78,7 +79,7 @@ public class WxStorageController {
String originalFilename = file.getOriginalFilename();
String url = storageService.store(file.getInputStream(), file.getSize(), file.getContentType(),
originalFilename);
originalFilename, MediaUtil.isRotation(file));
Map<String, Object> data = new HashMap<>();
data.put("url", url);
......
......@@ -89,12 +89,12 @@ public class WxPayServiceTest {
@Test
public void refund() {
List<String> list = Arrays.asList("DA2408041940485642547");
List<String> list = Arrays.asList("DA2408162000075498680");
list.forEach(orderId ->{
DistributionOrderRequestDto dto = new DistributionOrderRequestDto();
dto.setDistributionOrderId(orderId);
dto.setAmount("260");
dto.setRefundPrice("260");
dto.setAmount("430");
dto.setRefundPrice("430");
Result result = wxPayServiceApi.refund(dto);
logger.info("申请微信支付退款:{}", result);
});
......
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