Last active
August 29, 2015 14:09
-
-
Save troughton/2fce6b766d5d25d0abf1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.*; | |
import java.util.*; | |
static class Utils { | |
static void saveImage(PApplet applet) { | |
int i = 0; | |
File file = null; | |
String fileName = null; | |
do { | |
i++; | |
fileName = "Iteration " + i + ".png"; | |
file = new File(applet.sketchPath(fileName)); | |
} | |
while (file.exists()); | |
applet.save(fileName); | |
if (i == 1) return; //This is the first image we've saved. | |
//Check to make sure that this image is not identical to the last. | |
byte[] thisFileChecksum = Utils.checksum(file); | |
byte[] lastFileChecksum = Utils.checksum(new File(applet.sketchPath("Iteration " + (i - 1) + ".png"))); | |
if (thisFileChecksum != null && lastFileChecksum != null && Arrays.equals(thisFileChecksum, lastFileChecksum)) { | |
file.delete(); | |
} | |
} | |
//Adapted from http://www.mkyong.com/java/how-to-generate-a-file-checksum-value-in-java/ | |
static byte[] checksum(File file) { | |
try { | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
FileInputStream fis = new FileInputStream(file); | |
byte[] dataBytes = new byte[1024]; | |
int nread = 0; | |
while ( (nread = fis.read (dataBytes)) != -1) { | |
md.update(dataBytes, 0, nread); | |
} | |
byte[] digest = md.digest(); | |
return digest; | |
} | |
finally { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment