From 6d0cb5efe9eabdb7b12ea98041222be17f109d19 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 10 Mar 2016 13:40:01 +0200 Subject: [PATCH] =?UTF-8?q?=D0=AD=D1=84=D1=84=D0=B5=D0=BA=D1=82=D1=8B=20?= =?UTF-8?q?=D0=B8=D0=B7=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/holdfast/samobot/MainThread.java | 3 +- .../samobot/commands/ImageEffect.java | 125 ++++++++++++++++++ src/holdfast/samobot/commands/Shakal.java | 1 + 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 src/holdfast/samobot/commands/ImageEffect.java diff --git a/src/holdfast/samobot/MainThread.java b/src/holdfast/samobot/MainThread.java index 4621a3e..72e7ac2 100644 --- a/src/holdfast/samobot/MainThread.java +++ b/src/holdfast/samobot/MainThread.java @@ -45,6 +45,7 @@ public class MainThread implements Runnable { new Stats(), new When(), new Shakal(), + new ImageEffect(ImageEffect.Type.KEK), new AnnimonResponse() }; @@ -70,10 +71,10 @@ public class MainThread implements Runnable { JSONObject response = new JSONObject(obj).getJSONObject("response"); pts = response.getInt("new_pts"); - if (messagesLength == 1 && Util.random(80) == 5) { JSONArray jsonMessages = response.getJSONObject("messages").getJSONArray("items"); JSONArray jsonProfiles = response.getJSONArray("profiles"); final int messagesLength = jsonMessages.length(); + if (messagesLength == 0 && Util.random(80) == 5) { String message; if (Util.random(6) == 2) { message = MESSAGES[Util.random(MESSAGES.length)]; diff --git a/src/holdfast/samobot/commands/ImageEffect.java b/src/holdfast/samobot/commands/ImageEffect.java new file mode 100644 index 0000000..b8f4bfb --- /dev/null +++ b/src/holdfast/samobot/commands/ImageEffect.java @@ -0,0 +1,125 @@ +package holdfast.samobot.commands; + +import holdfast.samobot.IOUtil; +import holdfast.samobot.Util; +import holdfast.samobot.VK; +import java.awt.Graphics2D; +import java.awt.geom.AffineTransform; +import java.awt.image.AffineTransformOp; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import javax.imageio.ImageIO; + +/** + * + * @author aNNiMON + */ +public class ImageEffect extends PhotoAttachmentCommand { + + public enum Type { + KEK(new String[] { "кек" }); + + private final String[] command; + + Type(String[] command) { + this.command = command; + } + + public String[] getCommand() { + return command; + } + } + + private final Type type; + + public ImageEffect(Type type) { + this.type = type; + } + + @Override + protected String[] command() { + return type.getCommand(); + } + + @Override + protected boolean execute(String message, String userName, String photoUrl) throws IOException { + forward = 0; + final BufferedImage image = IOUtil.downloadImage(photoUrl); + final BufferedImage result; + switch (type) { + case KEK: + default: + result = processKek(image); + } + + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ImageIO.write(result, "jpg", baos); + baos.flush(); + final String photo = VK.uploadPhoto(baos.toByteArray(), "jpg"); + baos.close(); + + send(userName, photo); + return true; + } + + private BufferedImage processKek(BufferedImage image) { + final int width = image.getWidth(); + final int height = image.getHeight(); + final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + final Graphics2D g2d = result.createGraphics(); + g2d.drawImage(image, 0, 0, null); + + switch (Util.random(4)) { + case 0: { + // copy top part to bottom + AffineTransform tx = AffineTransform.getScaleInstance(1, -1); + tx.translate(0, -height); + AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); + g2d.drawImage(op.filter(image, null), + 0, 0, width, height / 2, + 0, 0, width, height / 2, + null); + } break; + + case 1: { + // copy bottom part to top + AffineTransform tx = AffineTransform.getScaleInstance(1, -1); + tx.translate(0, -height); + AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); + g2d.drawImage(op.filter(image, null), + 0, height / 2, width, height, + 0, height / 2, width, height, + null); + } break; + + + case 2: { + // copy right part to left + AffineTransform tx = AffineTransform.getScaleInstance(-1, 1); + tx.translate(-width, 0); + AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); + g2d.drawImage(op.filter(image, null), + 0, 0, width / 2, height, + 0, 0, width / 2, height, + null); + } break; + + case 3: + default: { + // copy left part to right + AffineTransform tx = AffineTransform.getScaleInstance(-1, 1); + tx.translate(-width, 0); + AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); + g2d.drawImage(op.filter(image, null), + width / 2, 0, width, height, + width / 2, 0, width, height, + null); + } + } + + g2d.dispose(); + + return result; + } +} diff --git a/src/holdfast/samobot/commands/Shakal.java b/src/holdfast/samobot/commands/Shakal.java index 45ec194..5ac50f9 100644 --- a/src/holdfast/samobot/commands/Shakal.java +++ b/src/holdfast/samobot/commands/Shakal.java @@ -23,6 +23,7 @@ public class Shakal extends PhotoAttachmentCommand { @Override protected boolean execute(String message, String userName, String photoUrl) throws IOException { + forward = 0; final BufferedImage image = IOUtil.downloadImage(photoUrl); final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();