<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
package com.bdkj.cruise.controller;
import com.bdkj.cruise.model.Media;
import com.bdkj.cruise.service.MediaService;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import net.coobird.thumbnailator.Thumbnails;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@EnableAutoConfiguration
@RequestMapping("/imagemap")
public class HomeController {
@Autowired
private MediaService mediaService;
@RequestMapping(value = "/userList")
public String userSetting() {
return "media";
}
@RequestMapping(value = "/getImages")
public void getBou(HttpServletRequest request, HttpServletResponse response) throws IOException {
String shootingTime = "2018-09-28";
String riverName="长江";
List<Media> media = mediaService.selectBySTAndRName(shootingTime,riverName);
Map<String, List<Media>> map = new HashMap<String, List<Media>>();
int k=0;
if (media.size()>20){ k=20; }else {k=media.size();}
for (int i=0;i<k;i++){
String fileNP=media.get(i).getFilepath()+"\\"+media.get(i).getFilename();
StringBuilder sb=new StringBuilder(fileNP);
sb.insert(sb.length()-4,"_thum");
fileNP=sb.toString();
File file=new File(fileNP);
if (!file.exists()){
Thumbnails.of(media.get(i).getFilepath()+"\\"+media.get(i).getFilename())
.scale(0.3f)
.outputQuality(0.2f)
.toFile(fileNP);
}
}
map.put("media", media);
JSONObject json = JSONObject.fromObject(map);
try {
response.setContentType("text/html; charset=utf-8");
response.getWriter().print(json);
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping("/mediaSave")
public void mediaSave(HttpServletRequest request, HttpServletResponse response) throws Exception {
JSONObject jsonData = new JSONObject();
Map<String, String[]> paramMap = request.getParameterMap();
for (String name : paramMap.keySet()) {
String[] values = paramMap.get(name);
jsonData.put(name, StringUtils.join(values));
}
Media media = new Media();
media = (Media) JSONObject.toBean(jsonData, Media.class);
String folderpath = "G:\\sunxiang";
File file = new File(folderpath);
media.setFolderpath(folderpath);
func(file,media,mediaService);
int p = 0, k = -1;
JSONObject re = new JSONObject();
if (p != 0) {
re.put("state", 2);
} else {
if (k == 1) {
re.put("state", 0);
} else {
re.put("state", 1);
}
}
try {
response.setContentType("text/html; charset=utf-8");
response.getWriter().print(re);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void func(File file,Media media,MediaService mediaService) throws Exception {
File[] fs = file.listFiles();
for (File f : fs) {
if (f.isDirectory())
{
func(f,media,mediaService);
}
if (f.isFile())
{
String fName=f.toString().trim();
String name=fName.substring(fName.lastIndexOf("\\")+1);
media.setFilename(name);
String newUuid = java.util.UUID.randomUUID().toString();
media.setUuid(newUuid);
String filepath=fName.substring(0,fName.lastIndexOf("\\"));
media.setFilepath(filepath);
File jpegFile = new File(f.toString().replace("\\","\\\\"));
Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
for(Directory directory : metadata.getDirectories()){
for(Tag tag : directory.getTags()){
if ("GPS Latitude".equals(tag.getTagName())){
media.setB(angleTool(tag.getDescription()));
}
if ("GPS Longitude".equals(tag.getTagName())){
media.setL(angleTool(tag.getDescription()));
}
if ("GPS Altitude".equals(tag.getTagName())){
media.setH(Double.parseDouble(tag.getDescription().substring(0,tag.getTagName().lastIndexOf(" "))));
}
if ("Date/Time".equals(tag.getTagName())){
String date=tag.getDescription();
char[] status=date.toCharArray();
status[4]='-';
status[7]='-';
date=Arrays.toString(status).replaceAll("[\\[\\]\\s,]", "");
StringBuffer buf=new StringBuffer();
buf.append(date);
buf.insert(10," ");
date=buf.toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
media.setTime(sdf.parse(date));
}
}
}
mediaService.insertSelective(media);
}
}
}
private static double angleTool(String angStr){
double dd,mm,ss;
angStr=angStr.replace("°","|");
angStr=angStr.replace("'","|");
angStr=angStr.replace("\"","");
angStr=angStr.replace(" ","");
String[] angObj=angStr.split("\\|");
if (angObj.length ==3){
dd=Double.parseDouble(angObj[0]);
mm=Double.parseDouble(angObj[1])/60;
ss=Double.parseDouble(angObj[2])/3600;
return (dd+mm+ss);
} else{
return 0.00;
}
}
}