Skip to content

Commit

Permalink
Uncommented the code in getScaledImage (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkovacevic committed May 6, 2024
1 parent 5bed4bd commit 10bfa5c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ or

Full description: https://proxy.services.wire.com/swagger#!/default/post

**Note:** `token` that comes with `conversation.init` events is _lifelong_. It should be stored for later usage. `token`
**Note:** `token` that comes with `conversation.bot_request` events is _lifelong_. It should be stored for later usage. `token`
that comes with other event types has lifespan of 20 seconds.

### Bot Examples
Expand Down
2 changes: 1 addition & 1 deletion backend/roman.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ logging:
"com.wire.bots.logger": ${LOG_LEVEL:-INFO}

swagger:
# make sure that this settings is the same as "server.rootPath"
# make sure that these settings is the same as "server.rootPath"
uriPrefix: /api
title: Roman Swagger
description: Roman - Wire Bots Proxy
Expand Down
73 changes: 33 additions & 40 deletions backend/src/main/java/com/wire/bots/roman/ImageProcessor.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package com.wire.bots.roman;

import com.wire.bots.roman.resources.Picture;
import com.wire.xenon.assets.ImagePreview;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class ImageProcessor {

private static final int MEDIUM_DIMENSION = 2896;

public static Picture getMediumImage(Picture picture) throws Exception {
return getScaledImage(picture, MEDIUM_DIMENSION);
return getScaledImage(picture);
}

private static Boolean shouldScaleOriginalSize(double width, double height, double dimension) {
final double maxPixelCount = 1.3 * dimension * dimension;
return (width > 1.3 * dimension || height > 1.3 * dimension)
private static Boolean shouldScaleOriginalSize(double width, double height) {
final double maxPixelCount = 1.3 * (double) ImageProcessor.MEDIUM_DIMENSION * (double) ImageProcessor.MEDIUM_DIMENSION;
return (width > 1.3 * (double) ImageProcessor.MEDIUM_DIMENSION || height > 1.3 * (double) ImageProcessor.MEDIUM_DIMENSION)
&& width * height > maxPixelCount;
}

private static Size getScaledSize(double origWidth, double origHeight, double dimension) {
private static Size getScaledSize(double origWidth, double origHeight) {
Size ret = new Size();
double op1 = Math.min(dimension / origWidth, dimension / origHeight);
double op2 = dimension / Math.sqrt(origWidth * origHeight);
double op1 = Math.min((double) ImageProcessor.MEDIUM_DIMENSION / origWidth, (double) ImageProcessor.MEDIUM_DIMENSION / origHeight);
double op2 = (double) ImageProcessor.MEDIUM_DIMENSION / Math.sqrt(origWidth * origHeight);
double scale = Math.max(op1, op2);
double width = Math.ceil(scale * origWidth);
ret.width = width;
Expand Down Expand Up @@ -52,37 +53,29 @@ private static BufferedImage resizeImage(BufferedImage originalImage,
return resizedImage;
}

// todo. fixme, xenon to expose getMimeType type and getImageData
private static Picture getScaledImage(ImagePreview image, int dimension) throws Exception {
// String resultImageType;
// switch ("image/jpeg") {
// case "image/jpeg":
// resultImageType = "jpg";
// break;
// case "image/png":
// resultImageType = "png";
// break;
// default:
// throw new IllegalArgumentException("Unsupported mime type");
// }
//
// int origWidth = image.getWidth();
// int origHeight = image.getHeight();
//
// BufferedImage resultImage = ImageIO.read(new ByteArrayInputStream(image.getImageData()));
//
// if (shouldScaleOriginalSize(origWidth, origHeight, dimension)) {
// Size scaledSize = getScaledSize(origWidth, origHeight, dimension);
// resultImage = resizeImage(resultImage, (int) scaledSize.width,
// (int) scaledSize.height);
// }
//
// try (ByteArrayOutputStream resultStream = new ByteArrayOutputStream()) {
// ImageIO.write(resultImage, resultImageType, resultStream);
// resultStream.flush();
// return new Picture(resultStream.toByteArray(), image.getMimeType());
// }
return null;
private static Picture getScaledImage(Picture image) throws Exception {
String resultImageType = switch (image.getMimeType()) {
case "image/jpeg" -> "jpg";
case "image/png" -> "png";
default -> throw new IllegalArgumentException("Unsupported mime type");
};

int origWidth = image.getWidth();
int origHeight = image.getHeight();

BufferedImage resultImage = ImageIO.read(new ByteArrayInputStream(image.getImageData()));

if (shouldScaleOriginalSize(origWidth, origHeight)) {
Size scaledSize = getScaledSize(origWidth, origHeight);
resultImage = resizeImage(resultImage, (int) scaledSize.width,
(int) scaledSize.height);
}

try (ByteArrayOutputStream resultStream = new ByteArrayOutputStream()) {
ImageIO.write(resultImage, resultImageType, resultStream);
resultStream.flush();
return new Picture(resultStream.toByteArray(), image.getMimeType());
}
}

private static class Size {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Picture extends ImagePreview {

private boolean aPublic;
private final byte[] imageData;
private final String mimeType;
private int width;
private int height;
private String retention;
Expand All @@ -16,6 +17,7 @@ public Picture(byte[] image, String mimeType)
{
super(UUID.randomUUID(), mimeType);
this.imageData = image;
this.mimeType = mimeType;
}

public void setPublic(boolean aPublic) {
Expand Down Expand Up @@ -45,4 +47,8 @@ public int getHeight() {
public void setHeight(int height) {
this.height = height;
}

public String getMimeType() {
return mimeType;
}
}

0 comments on commit 10bfa5c

Please sign in to comment.