Эффекты изображений

This commit is contained in:
Victor 2016-03-10 13:40:01 +02:00
parent 428b497173
commit 6d0cb5efe9
3 changed files with 128 additions and 1 deletions

View File

@ -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)];

View File

@ -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;
}
}

View File

@ -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();