diff --git a/DivaBotGuardian/DivaBotGuardian.jar b/DivaBotGuardian/DivaBotGuardian.jar index 1863ceb..b4d1bcf 100644 Binary files a/DivaBotGuardian/DivaBotGuardian.jar and b/DivaBotGuardian/DivaBotGuardian.jar differ diff --git a/DivaBotGuardian/src/sig/ColorRegion.java b/DivaBotGuardian/src/sig/ColorRegion.java new file mode 100644 index 0000000..33767a7 --- /dev/null +++ b/DivaBotGuardian/src/sig/ColorRegion.java @@ -0,0 +1,82 @@ +package sig; + +import java.awt.Color; +import java.awt.Rectangle; +import java.awt.image.BufferedImage; +import java.util.Arrays; + +public class ColorRegion { + Rectangle region; + BufferedImage img; + + ColorRegion(BufferedImage img, Rectangle region){ + this.region=region; + this.img=img; + } + + public boolean getRedRange(int min,int max) { + int avgRed = getRed(); + return avgRed>=min&&avgRed<=max; + } + public boolean getGreenRange(int min,int max) { + int avgGreen = getGreen(); + return avgGreen>=min&&avgGreen<=max; + } + public boolean getBlueRange(int min,int max) { + int avgBlue = getBlue(); + return avgBlue>=min&&avgBlue<=max; + } + + public boolean getAllRange(int min,int max) { + return getRedRange(min,max)&&getGreenRange(min,max)&&getBlueRange(min,max); + } + public boolean getAllRange(int minRed,int maxRed,int minGreen,int maxGreen,int minBlue,int maxBlue) { + return getRedRange(minRed,maxRed)&&getGreenRange(minGreen,maxGreen)&&getBlueRange(minBlue,maxBlue); + } + + public int getRed() { + int total = 0; + for (int x=0;x=region.x+region.width||region.y+y<0||region.y+y>=region.y+region.height) { + continue; + } + total+=new Color(img.getRGB(region.x+x, region.y+y)).getRed(); + } + } + return total/(region.width*region.height); + } + public int getGreen() { + int total = 0; + for (int x=0;x=region.x+region.width||region.y+y<0||region.y+y>=region.y+region.height) { + continue; + } + total+=new Color(img.getRGB(region.x+x, region.y+y)).getGreen(); + } + } + return total/(region.width*region.height); + } + public int getBlue() { + int total = 0; + for (int x=0;x=region.x+region.width||region.y+y<0||region.y+y>=region.y+region.height) { + continue; + } + total+=new Color(img.getRGB(region.x+x, region.y+y)).getBlue(); + } + } + return total/(region.width*region.height); + } + + public String toString() { + StringBuilder sb = new StringBuilder("ColorRegion(Region: "); + return sb.append(region).append(",") + .append("R:").append(getRed()).append(",") + .append("G:").append(getGreen()).append(",") + .append("B:").append(getBlue()).append(")") + .toString(); + } +} diff --git a/DivaBotGuardian/src/sig/Guardian.java b/DivaBotGuardian/src/sig/Guardian.java index aa7ef22..5567084 100644 --- a/DivaBotGuardian/src/sig/Guardian.java +++ b/DivaBotGuardian/src/sig/Guardian.java @@ -1,6 +1,13 @@ package sig; +import java.awt.Color; +import java.awt.Point; +import java.awt.Rectangle; +import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; + +import javax.imageio.ImageIO; import sig.utils.FileUtils; @@ -8,40 +15,138 @@ public class Guardian { public static int USERID = -1; public static long streamLastModified = -1; public static STAGE currentStage = STAGE.STARTING; + public static int FRAMECOUNT = 0; + + public static Point UPPERLEFTCORNER = null; + public static Point LOWERRIGHTCORNER = null; enum STAGE{ STARTING, + CALIBRATING, RUNNING, CLEANUP; } + public static BufferedImage CropFutureToneImage(BufferedImage img) throws IOException { + Point crop1 = null; + Point crop2 = null; + + Color col = new Color(img.getRGB(0, 0)); + if (col.getRed()<=5&&col.getGreen()<=5&&col.getBlue()<=5) { + boolean done=false; + for (int x=img.getWidth()-1;x>=img.getWidth()*(7f/8);x--) { + for (int y=0;y=5&&col.getGreen()>=5&&col.getBlue()>=5) { + crop1 = new Point(x,y); + done=true; + break; + } + } + if (done) { + break; + } + } + done=false; + for (int x=0;x=img.getHeight()*(7f/8);y--) { + col = new Color(img.getRGB(x, y)); + if (col.getRed()>=5&&col.getGreen()>=5&&col.getBlue()>=5) { + crop2 = new Point(x,y); + done=true; + break; + } + } + if (done) { + break; + } + } + UPPERLEFTCORNER=new Point(crop2.x,crop1.y); + LOWERRIGHTCORNER=new Point(crop1.x,crop2.y); + //img = img.getSubimage(crop2.x, crop1.y, crop1.x-crop2.x, crop2.y-crop1.y); + } + //System.out.println("Future Tone? "+MyRobot.FUTURETONE); + return img; + } + + public static int scale1280(int original) { + //772,175 + //UPPERLEFTCORNER[x=37,y=22],LOWERRIGHTCORNER[x=1239,y=692] + return (int)(original*((double)(LOWERRIGHTCORNER.x-UPPERLEFTCORNER.x)/1280))+UPPERLEFTCORNER.x; + } + public static int scale720(int original) { + return (int)(original*((double)(LOWERRIGHTCORNER.y-UPPERLEFTCORNER.y)/720))+UPPERLEFTCORNER.y; + } + public static void main(String[] args) throws InterruptedException, IOException { USERID = Integer.parseInt(args[0]); File f = new File("streams","output"+USERID+".png"); - File tempf = new File("streams","tempoutput"+USERID+".png"); - switch (currentStage) { - case STARTING:{ - while (currentStage==STAGE.STARTING) { - if (f.exists()) { - streamLastModified = f.lastModified(); - currentStage=STAGE.RUNNING; + if (f.exists()) { + f.delete(); + } + File[] tempf = new File[10]; + + for (int i=0;i=streamLastModified+5000) { - currentStage=STAGE.CLEANUP; - System.out.println("Stream is no longer being updated! Shutting down!"); + }break; + case CALIBRATING:{ + while (currentStage==STAGE.CALIBRATING) { + try { + HandleStreamFile(f, tempf[FRAMECOUNT]); + CropFutureToneImage(ImageIO.read(tempf[FRAMECOUNT])); + currentStage=STAGE.RUNNING; + } catch (IOException | InvocationTargetException | NullPointerException e) { + } + Thread.sleep(100); + } + }break; + case RUNNING:{ + while (currentStage==STAGE.RUNNING) { + long startTime = System.currentTimeMillis(); + try { + HandleStreamFile(f, tempf[FRAMECOUNT]); + BufferedImage img = ImageIO.read(tempf[FRAMECOUNT]); + //1227x690 //1.04319478402607986960065199674 + //1280x720 + ColorRegion cr = new ColorRegion(img,new Rectangle(scale1280(772),scale720(175),5,5)); + System.out.println(UPPERLEFTCORNER+","+LOWERRIGHTCORNER+"///"+"Is Song select? "+cr.getAllRange(160,200,0,15,170,200)+" "+cr+" ("+(System.currentTimeMillis()-startTime)+"ms)"); + } catch (IOException | InvocationTargetException | NullPointerException e) { + System.out.println("Skip error frame.("+(System.currentTimeMillis()-startTime)+"ms)"); + } + FRAMECOUNT=(FRAMECOUNT+1)%10; + Thread.sleep(100); } - Thread.sleep(100); + }break; + case CLEANUP:{ + f.delete(); + System.exit(0); + break program; } - }break; - case CLEANUP:{ - f.delete(); - }break; + } + } + } + + private static void HandleStreamFile(File f, File tempf) throws IOException, InvocationTargetException { + FileUtils.copyFile(f, tempf); + System.out.println(System.currentTimeMillis()+"/"+streamLastModified); + if (System.currentTimeMillis()>=streamLastModified+5000) { + currentStage=STAGE.CLEANUP; + System.out.println("Stream is no longer being updated! Shutting down!"); + } else { + streamLastModified=f.lastModified(); } } } diff --git a/DivaBotGuardian/src/sig/utils/FileUtils.java b/DivaBotGuardian/src/sig/utils/FileUtils.java index bce5562..9c38dde 100644 --- a/DivaBotGuardian/src/sig/utils/FileUtils.java +++ b/DivaBotGuardian/src/sig/utils/FileUtils.java @@ -14,6 +14,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; +import java.lang.reflect.InvocationTargetException; import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.URL;