Skip to content

Commit

Permalink
service
Browse files Browse the repository at this point in the history
  • Loading branch information
anylots committed Jun 28, 2020
1 parent 84520de commit c3b2699
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package com.frame.detection.client;

/**
* image detect client
*
* @author anylots
* @version $Id: HttpClient.java, v 0.1 2020年06月26日 22:42 anylots Exp $
*/
public interface ImageDetectClient {

/**
* get image info
*
* @param imageLink image link
* @return
*/
String getImageInfo(String imageLink);
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
package com.frame.detection.client;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
* implement of imageDetectClient
*
* @author anylots
* @version $Id: HttpClientImpl.java, v 0.1 2020年06月26日 22:50 anylots Exp $
*/
@Component
public class ImageDetectClientImpl implements ImageDetectClient {

/**
* get image info
*
* @param imageLink image link
* @return
*/
@Override
public String getImageInfo(String imageLink) {

RestTemplate restTemplate = new RestTemplate();

// step 1. parameters
String url = "http://127.0.0.1:8081/detect/imageDetect";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("imageLink", imageLink);
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
paramMap.add("imageLink", imageLink);

// step 2. request
HttpEntity<MultiValueMap<String, String>> request;
request = new HttpEntity<>(map, headers);
request = new HttpEntity<>(paramMap, headers);

//step 3. post http call
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

Assert.isTrue(HttpStatus.OK == response.getStatusCode(), "http call is failed");
return response.getBody();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.awt.image.BufferedImage;

/**
* buffered image service
*
* @author anylots
* @version $Id: BufferedImageService.java, v 0.1 2020年06月26日 18:29 anylots Exp $
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.net.URL;

/**
* implement of BufferedImageService
*
* @author anylots
* @version $Id: BufferedImageServiceImpl.java, v 0.1 2020年06月26日 18:30 anylots Exp $
*/
Expand All @@ -34,18 +36,18 @@ public BufferedImage getRemoteBufferedImage(String imageURL) {
bufferedImage = ImageIO.read(is);
} catch (MalformedURLException e) {
e.printStackTrace();
System.out.println("imageURL: " + imageURL + ",无效!");
System.out.println("imageURL: " + imageURL + ",invalid!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("imageURL: " + imageURL + ",读取失败!");
System.out.println("imageURL: " + imageURL + ",read failed!");
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("imageURL: " + imageURL + ",流关闭异常!");
System.out.println("imageURL: " + imageURL + ",inputStream close error!");
}
}
return bufferedImage;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/frame/detection/common/ImageDetector.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

package com.frame.detection.common;

import java.awt.image.BufferedImage;

/**
* image detector
*
* @author anylots
* @version $Id: ImageDetector.java, v 0.1 2020年06月26日 18:36 anylots Exp $
*/
Expand All @@ -12,8 +12,8 @@ public interface ImageDetector {
/**
* detect
*
* @param imageURL
* @param imageUrl image url
* @return
*/
String detect(String imageURL);
String detect(String imageUrl);
}
12 changes: 9 additions & 3 deletions src/main/java/com/frame/detection/common/ImageDetectorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@
import org.springframework.stereotype.Component;

/**
* implement of ImageDetector
*
* @author anylots
* @version $Id: ImageDetectorImpl.java, v 0.1 2020年06月26日 18:39 anylots Exp $
*/
@Component
public class ImageDetectorImpl implements ImageDetector {

/**
* image detect client
*/
@Autowired
private ImageDetectClient imageDetectClient;

/**
* detect
*
* @param imageURL
* @param imageUrl image url
* @return
*/
@Override
public String detect(String imageURL) {
return imageDetectClient.getImageInfo(imageURL);
public String detect(String imageUrl) {
//get image info
return imageDetectClient.getImageInfo(imageUrl);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.frame.detection.constants;

/**
* constants of viewModel
*
* @author anylots
* @version $Id: ViewModelConstant.java, v 0.1 2020年06月28日 21:33 anylots Exp $
*/
public class ViewModelConstants {

/**
* detect html
*/
public static final String DETECT = "detect";

/**
* detectOut html
*/
public static final String DETECT_OUT = "detectOut";

/**
* image on detectOut
*/
public static final String DETECT_OUT_IMAGE = "detectOutImage";

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

package com.frame.detection.controller;

import com.frame.detection.constants.ViewModelConstants;
import com.frame.detection.service.ImageDetectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -38,7 +39,7 @@ public class ImageDetectController {
@RequestMapping(value = "/detect", method = RequestMethod.GET)
public String detect() {

return "detect";
return ViewModelConstants.DETECT;
}

/**
Expand All @@ -50,21 +51,13 @@ public String detect() {
@RequestMapping(value = "/detectImage", method = RequestMethod.POST)
public ModelAndView detectOut(String imageLink) {

// record service time consumption.
// it is recommended to delete the non development environment
// and reduce resource consumption
long startTime = System.currentTimeMillis();

// step 1. detect image by imageUrl
String detectFrame = imageDetectService.detect(imageLink);

// step 2. assemble modelAndView
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("detectOut");
modelAndView.addObject("img", detectFrame);

long endTime = System.currentTimeMillis();
logger.info("processing time: {} s", endTime - startTime);
modelAndView.setViewName(ViewModelConstants.DETECT_OUT);
modelAndView.addObject(ViewModelConstants.DETECT_OUT_IMAGE, detectFrame);

// step 3. return detect result page
return modelAndView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package com.frame.detection.service;

/**
* image Detect Service
* image detect service
*
* @author anylots
* @version $Id: ImageDetectService.java, v 0.1 2020年06月26日 18:22 anylots Exp $
Expand All @@ -12,7 +12,7 @@ public interface ImageDetectService {
/**
* detect
*
* @param imageUrl
* @param imageUrl image url
*/
String detect(String imageUrl);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
import org.springframework.stereotype.Service;

/**
* implement of ImageDetectService
*
* @author anylots
* @version $Id: ImageDetectServiceImpl.java, v 0.1 2020年06月26日 18:24 anylots Exp $
*/
@Service
public class ImageDetectServiceImpl implements ImageDetectService {

@Autowired
private BufferedImageService bufferedImageService;

/**
* image detector
*/
@Autowired
private ImageDetector imageDetector;

Expand Down
16 changes: 11 additions & 5 deletions src/main/resources/templates/detect.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">Navbar w/ text</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText"
aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarText">
Expand All @@ -38,17 +39,22 @@


<div class="container" style="margin-top:20px;">
<h1>Image Detect Service</h1>
<h1>Object Detection Management</h1>
<form enctype='multipart/form-data' method='POST' action="detectImage" target="_blank">
<label>Upload file :</label>
<label class="file-box">
<input type="file" name="file" title="Search for a file to add 1" style="margin-top:40px;"
class="btn-primary"/>
</label>
<br>
<label style="margin-top:20px;">Picture link :</label>
<input type="text" class="txt_input" name="imageLink" value=""
style="margin-top:5px;width:400px"/>

<div class="input-group mb-3" style="margin-top:5px;width:500px">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">Picture link</span>
</div>
<input name="imageLink" type="text" class="form-control" placeholder="" aria-label="Username"
aria-describedby="basic-addon1">
</div>
<br>


Expand Down

0 comments on commit c3b2699

Please sign in to comment.