Skip to content

Instantly share code, notes, and snippets.

@davidpcrd
Created January 15, 2020 17:11
Show Gist options
  • Select an option

  • Save davidpcrd/b1f6ac08c9b04ac018a8ef752d9eb2c7 to your computer and use it in GitHub Desktop.

Select an option

Save davidpcrd/b1f6ac08c9b04ac018a8ef752d9eb2c7 to your computer and use it in GitHub Desktop.
Quick java file to get clip and export to file
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
public class ClipToImgFile {
static String name = "export.png";
static String path;
public static void main(String[] args) {
path = args[0];
checkArg(args);
try {
BufferedImage ii = (BufferedImage) Toolkit.getDefaultToolkit().getSystemClipboard()
.getData(DataFlavor.imageFlavor);
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
ArrayList<Integer> list = new ArrayList<Integer>();
for (File file : listOfFiles) {
System.out.println(file.getName());
if (file.isFile() && file.getName().contains("export") && file.getName().contains(".png")) {
name = "export (1).png";
if (file.getName().contains("export (") && file.getName().contains(".png")) {
String[] ss = file.getName().split("\\(");
String[] sss = ss[1].split("\\)");
int n = Integer.parseInt(sss[0]);
list.add(Integer.valueOf(n));
System.out.println(n);
}
}
}
int p = 0;
System.out.println(list.size());
if (list.size() >= 1) {
for (int nbt : list) {
if (nbt > p) {
name = "export (" + (nbt + 1) + ").png";
}
p = nbt;
}
}
saveToFile(ii, path + "\\" + name);
} catch (HeadlessException | UnsupportedFlavorException | IOException e) {
System.err.println("[Error] Unable get image");
}
}
public static void saveToFile(BufferedImage img, String out) throws FileNotFoundException, IOException {
File outputfile = new File(out);
outputfile.createNewFile();
ImageIO.write(img, "png", outputfile);
}
public static void checkArg(String[] args) {
if (args.length < 1) {
System.err.println("[Error] Export pass missing");
System.exit(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment