Skip to content

Commit

Permalink
feat: add resize
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed May 1, 2024
1 parent c91a76a commit cc30c52
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,32 @@
*/
package ch.xxx.aidoclibchat.adapter.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import ch.xxx.aidoclibchat.usecase.mapping.ImageMapper;
import ch.xxx.aidoclibchat.usecase.service.ImageService;

@RestController
@RequestMapping("rest/image")
public class ImageController {
private static final Logger LOG = LoggerFactory.getLogger(ImageController.class);
private final ImageMapper imageMapper;
private final ImageService imageService;

public ImageController(ImageMapper imageMapper) {
public ImageController(ImageMapper imageMapper, ImageService imageService) {
this.imageMapper = imageMapper;
this.imageService = imageService;
}

@PostMapping("/query")
public String postImageQuery(@RequestParam(required = true) String query, @RequestParam("file") MultipartFile imageQuery) {
var imageDto = this.imageMapper.map(imageQuery, query);
return "";
public String postImageQuery(@RequestParam(required = true) String query, @RequestParam("file") MultipartFile imageQuery) {
var result = this.imageService.queryImage(this.imageMapper.map(imageQuery, query));
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ public enum DocumentType {
};

public enum ImageType {
JPEG, PNG, SVG, UNKNOWN
JPEG("jpg"), PNG("png"), SVG("svg"), UNKNOWN("unknown");

private String type;

private ImageType(String type) {
this.type = type;
}

@Override
public String toString() {
return this.type;
}
}

public static final String ID = "id";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright 2023 Sven Loesekann
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ch.xxx.aidoclibchat.usecase.service;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import ch.xxx.aidoclibchat.domain.model.dto.ImageDto;

@Service
public class ImageService {
private static final Logger LOG = LoggerFactory.getLogger(ImageService.class);

public String queryImage(ImageDto imageDto) {
imageDto = resizeImage(imageDto);
return "";
}

private ImageDto resizeImage(ImageDto imageDto) {
try {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageDto.getImageContent()));
int targetHeight = image.getHeight();
int targetWidth = image.getWidth();
if(image.getHeight() > 672 && image.getWidth() > 672) {
if(image.getHeight() > image.getWidth()) {
targetHeight = image.getHeight() / (image.getHeight() / 672);
targetWidth = image.getWidth() / (image.getHeight() / 672);
} else {
targetHeight = image.getHeight() / (image.getWidth() / 672);
targetWidth = image.getWidth() / (image.getWidth() / 672);
}
}
var outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
outputImage.getGraphics().drawImage(image.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH), 0,0, null);
var ios = new ByteArrayOutputStream();
ImageIO.write(outputImage, imageDto.getImageType().toString(), ios);
imageDto.setImageContent(ios.toByteArray());
imageDto.setContentSize(ios.toByteArray().length);
} catch (IOException e) {
LOG.info("Image resize failed.",e);
}
return imageDto;
}
}

0 comments on commit cc30c52

Please sign in to comment.