Skip to content

Commit

Permalink
Simplify AWT image resize test
Browse files Browse the repository at this point in the history
  • Loading branch information
galderz committed Sep 17, 2021
1 parent 32162c7 commit 2b5d57d
Showing 1 changed file with 8 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@
public class AwtImageResizeTest {

@Test
public void testImageResize() {
public void testImageResize() throws IOException {
final String imageData = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZMAAAC5CAYAAADtXNKFAAAQsUlEQVR4Xu3dW6hUZRsH8PfrvsygwAjKLgwis7Ik6KKSIAjTCkUi7CAkQdERIm86XQRCJ6sL6yKz0DA7IwQFlYEmSJkR4YVIRdCRDhfdSNn3PfMxw97j7Nkz+1lz2vN7YbMz13rWrN96nf+8611rzX+OHj36b9EIECBAgEBC4D/CJKFnVQIECBCoCQgTHYEAAQIE0gLCJE2oAAECBAgIE32AAAECBNICwiRNqAABAgQICBN9gAABAgTSAsIkTagAAQIECAgTfYAAAQIE0gLCJE2oAAECBAgIE32AAAECBNICwiRNqAABAgQICBN9gAABAgTSAsIkTagAAQIECAgTfYAAAQIE0gLCJE2oAAECBAgIE32AAAECBNICwiRNqAABAgQICBN9gAABAgTSAsIkTagAAQIECAgTfYAAAQIE0gLCJE2oAAECBAgIE32AAAECBNICwiRNqAABAgQICBN9gAABAgTSAsIkTagAAQIECAgTfYAAAQIE0gLCJE2oAAECBAgIE32AAAECBNICwiRNqAABAgQICBN9gAABAgTSAsIkTagAAQIECAgTfYAAAQIE0gLCJE2oAAECBAgIE32AAAECBNICwiRNqAABAgQICBN9gAABAgTSAsIkTagAAQIECAgTfYAAAQIE0gLCJE2oAAECBAgIE32AAAECBNICwiRNqAABAgQICBN9gAABAgTSAsIkTagAAQIECAgTfYAAAQIE0gLCJE2oAAECBAgIE32AAAECBNICwiRNqAABAgQICBN9gAABAgTSAsIkTagAAQIECAgTfYAAAQIE0gLCJE2oAAECBAgIE32AAAECBNICwiRNqAABAgQICBN9gAABAgTSAsIkTagAAQIECAgTfYAAAQIE0gLCJE2oAAECBAgIE32AAAECBNICwiRNqAABAgQICBN9gAABAgTSAsIkTagAAQIECAgTfYAAAQIE0gLCJE2oAAECBAgIE32AAAECBNICwiRNqAABAgQICBN9gAABAgTSAsIkTagAAQIECAgTfYAAAQIE0gLCJE2oAAECBAgIE32AAAECBNICwiRNqAABAgQICBN9gAABAgTSAsIkTagAAQIECAgTfYAAAQIE0gLCJE2owKAE/vjjj1L/+fPPP495Gf/++28544wzaj8aAQK9FRAmvfVVPSGwa9eu8s0339QC49tvv639d/0n/l837cQTTyznnXdeqf++9NJLy+mnny5oukG0LIE2AsJE9xiYQATD7t27y+HDh0uMIr744osZh8VMdyLC5bLLLqsFTQRM/GgECHQvIEy6N7PGDAU+/vjj8sknn5T4HT/D2iJYImAiWBYtWmT0MuAD9csvv5STTz65HDp0qBw5cqT8/PPPteOjDZeAMBmu4zHrXk2Exssvv1zefvvt2umqKtqcOXNqb/Axqqi3+p9jG/H/43eMfKLFfx84cGDGm47a8eYVwbJixQrhMmPJ7lZ89tlny5tvvlnidGerdtNNN5WHHnrI8eiOtWdLC5Oe0Y534QiPRx99tHbqqttWn8uoT57X/xwh0Rwi3dSOUInXEz8xBxO/p3qjalf3tNNOKxdeeGFt5CJcujkCnS27c+fO8sgjj5TPPvusoxUiUOJHG6yAMBms/6zbeoxE7r333o5CJEIiTiktXLiwLFmypMSbdPy53y1ec4xiIlgiYLodxdRPi914440Def399url9iJIli9f3vUmtm/fXlatWtX1elaoTkCYVGc51pXiU/8zzzxTnn766bans+KTfPzEaaNhvWS3PoKJcIkRVjfhEvsUn5JjHyeehhvrztHFzh933HHHLH311VeX+fPn10aTYVoP/uYFjVC6gO7BosKkB6jjVjL+ca9du3bKSfWY47j77rvLXXfdNZJvsPVwef7558v+/fvLTz/9VFrd1zLxuMeb3s0331wefPDBkdznfvfhGBFGH2o+LTrViCMm46+//vpJp8LmzZtX+zBjhNLvo/f/7QmTwbjPmq3GP96YG5lqcj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LrTvOptVAUdJhs3bmyU69ezwqp6/cNYR5gM41HxmghULLBjx44S3+/x+++/T6q8ffv2smrVqkq21nyl1KJFi8r+/fsrqV11kVajk2EbQVW9z72uJ0x6Law+gSERiEB5/fXXS/ye2OJ01+bNm1Ov8sorrywffPBBo8acOXNqQRJXSw1rax6deF5X7kgJk5yftQmMlEDMo9xxxx0lvou9qkCJ7yWJUc/ENgp3l8dFAhEo8Tse4+9+k1xXFiY5P2sTGEmB1atXHzNCWbduXdm0aVNX+/Pll18e89DGGKW89957XdWx8OgLCJPRP4b2gMCMBJpvLIwi8TW3a9asKTFxPl1rdZPkWWedVftmQ48omU5v9v29MJl9x9QeEehIoN0d83HT4WuvvTblDY6HDh0qt9566zFfKfz++++XK664oqPtW2h2CQiT2XU87Q2BrgU2bNhQ1q9ff8x6cZXXypUrJ13tFSGydevWSY8kqa9Y5ZVhXe+EFQYuIEwGfgi8AAKDF4jnVb3yyistn1O1ZMmSsnDhwrJ79+5y8ODBli92WO8nGbzs+LwCYTI+x9qeEphWYPny5WXnzp3TLjdxAUHSFdesXViYzNpDa8cIzExg7969tdNeu3btalsgrv6K7weJkYtGQJjoAwQItBSIx41s27at7Nu3b9Lfx0hk2bJlZfHixeQINASEic5AgEBbge+//778/fff5ciRI2XBggW0CLQUECY6BgECBAikBYRJmlABAgQIEBAm+gABAgQIpAWESZpQAQIECBAQJvoAAQIECKQFhEmaUAECBAgQECb6AAECBAikBYRJmlABAgQIEBAm+gABAgQIpAWESZpQAQIECBAQJvoAAQIECKQFhEmaUAECBAgQECb6AAECBAikBYRJmlABAgQIEBAm+gABAgQIpAWESZpQAQIECBAQJvoAAQIECKQFhEmaUAECBAgQECb6AAECBAikBYRJmlABAgQIEBAm+gABAgQIpAWESZpQAQIECBAQJvoAAQIECKQFhEmaUAECBAgQECb6AAECBAikBYRJmlABAgQIEBAm+gABAgQIpAWESZpQAQIECBAQJvoAAQIECKQFhEmaUAECBAgQECb6AAECBAikBYRJmlABAgQIEBAm+gABAgQIpAWESZpQAQIECBAQJvoAAQIECKQFhEmaUAECBAgQECb6AAECBAikBYRJmlABAgQIEBAm+gABAgQIpAWESZpQAQIECBAQJvoAAQIECKQFhEmaUAECBAgQECb6AAECBAikBYRJmlABAgQIEBAm+gABAgQIpAWESZpQAQIECBAQJvoAAQIECKQFhEmaUAECBAgQECb6AAECBAikBYRJmlABAgQIEBAm+gABAgQIpAWESZpQAQIECBAQJvoAAQIECKQFhEmaUAECBAgQECb6AAECBAikBYRJmlABAgQIEBAm+gABAgQIpAWESZpQAQIECBAQJvoAAQIECKQFhEmaUAECBAgQ+C9CtmAUEj4ccwAAAABJRU5ErkJggg==";
final String expected = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANkAAABkCAIAAACEiAKKAAAHaklEQVR4Xu3ba0iTbRgHcM3jJmlWlilNEoPoQ1ChVisKKpEgKTIpIyWLgiAauA+diD4UmH6Ilw4U5iI/FFIWhFaEFJS40rCMTi8dXzRLsoO5sqa9vH/2YOhd7p267bnU/++T7r63Z/P5P9d93dsM+JdIhgD1BiKdMIskBbNIUjCLJAWzSFIwiyQFs0hSMIskBbNIUjCLJAWzSFIwiyQFs0hSMIskBbNIUjCLJAWzSFIwiyQFs0hSMIskBbNIUjCLJAWzSFIwiyQFs0hSMIskBbNIUjCLJAWzSFIwiyQFs0hSMIskBbNIUjCLJAWzSFIwiyQFs0hSMIskBbNIUjCLfero6Ghpafmnm8PhUGeQV430LP748ePZs2e1tbWXLl2y2Wz5+fnZ2dlms3ny5Mnjxo2Ljo6O6oZfk5KSlixZYrVaS0pKampq2tra1IejQRiJWezs7Hz69GlpaWlOTs706dPj4uKQM4PBEBgYGNAtLCxs7Nix48ePT3CJjY1FLkeNGqWN4gf8imhmZGQUFhZeu3bt+fPnTqdTPZKucJndvXu3vLzc5vL48eOuri51kiQjK4uIC07P5s2bES8tWEFBQcjc1KlTU1JSMjMzUfOOHDly/vz56urqe/fuNTQ0/O3y8OHD+vr6Gzdu4KRizuLFi3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kFvXU0dGBTUxiYiJKVEJCQllZmTrDAyiKJpMJ9fXChQvqmFsWiwXHTUtLa29vV8f0wCzqDHsXu90+b948xMJoNBYVFfXrkyEUwp07d6IoJicn9/erGA0NDbGxsehcKysr1TE9MIsivH79OisrC5vr0NDQTZs2uX+DsCfkKT4+Pjw8vLS0VB3zgPa+94oVKzxf3H2HWZQCe9sDBw4gjghHamrqy5cv1Rm/6ezs1MKEojiwdRYlOdplwN2qFzGLslRUVKB9RLxQ7c6dO6cO93by5EntW4nKV3I8hzRnZmbicLm5ueqY3zGLsqB9xA43IyMDe5GJEyeePXtWndENhVP7drrVanX/QYt7N2/eRJpjYmIGVlm9iFmUqKmpacuWLah5UVFRxcXF6rBrA56eno7tjtlsxmR1uD/QG/zl4q1PgAaMWRQKEbFYLCEhIShayGXPTwsxVFBQgIUVu+Dq6ure9xvCmEW5Pn36hN2M0WjU3se+fPlyc3MzNhl5eXkGgwFV02azDWZ1loZZFM3pdCKC2M0EBQWhgzSZTNo/0yCIhYWFYv+LamCYxSGgra1t//79cXFxAa7/f8Wet76+Xp009DGLQwOaRYfDgTUauZTwvrQvMIskBbNIUjCLJAWzSFIwiyQFs0hSMIskBbNIUjCLJAWzSFIwiyQFs0hSMIskBbNIUjCLJAWzSFIwiyQFs0hSMIskBbNIUjCLJAWzSFIwiyQFs0hSMIskBbNIUjCLJAWzSFIwiyQFs0hSMIskBbNIUvwHioQevmxBJKYAAAAASUVORK5CYII=";

final InputStream imageStream = fromBase64(imageData);
final String resizeImage = resizeImage(imageStream, 100);
final byte[] imageBytes = Base64.getDecoder().decode(imageData.split(",", 2)[1]);
final String resizeImage = resizeImage(imageBytes, 100);
Assertions.assertEquals(expected, resizeImage);
}

private String resizeImage(InputStream inputStream, int height)
{
BufferedImage inputImage = readImage(inputStream);
private String resizeImage(byte[] imageBytes, int height) throws IOException {
BufferedImage inputImage = ImageIO.read(new ByteArrayInputStream(imageBytes));

int currentW = inputImage.getWidth();
int currentH = inputImage.getHeight();
Expand All @@ -38,53 +37,12 @@ private String resizeImage(InputStream inputStream, int height)

Image originalImage = inputImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);

resizedImage.getGraphics().drawImage(originalImage, 0, 0, null);

byte[] imageBytes = writeImage(resizedImage);
return String.format("data:image/png;base64,%s", Base64.getEncoder().encodeToString(imageBytes));
}

private byte[] writeImage(BufferedImage resizedImage)
{
try
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(resizedImage, "png", outputStream);
return outputStream.toByteArray();
}
catch (IOException io)
{
throw new UncheckedIOException("Cannot resize image, failed to write resized data to oputut stream", io);
}
}

private BufferedImage readImage(InputStream inputStream)
{
try
{
return ImageIO.read(inputStream);
}
catch (IOException io)
{
throw new UncheckedIOException("Cannot resize image, failed to read image", io);
}
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(resizedImage, "png", outputStream);

private static InputStream fromBase64(final String content)
{
final String data = extractData(content);
byte[] bytes = Base64.getDecoder().decode(data);
return new ByteArrayInputStream(bytes);
return String.format("data:image/png;base64,%s", Base64.getEncoder().encodeToString(outputStream.toByteArray()));
}

private static String extractData(final String content)
{
if (content.startsWith("data:image"))
{
return content.split(",", 2)[1];
}

return content;
}
}

0 comments on commit 2b5d57d

Please sign in to comment.