import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; import javax.imageio.ImageIO; public class ImgExtractor { String[] names; int[][][] areas; BufferedImage[] imgs; public ImgExtractor(String imgDir, String areasDir, String outputDir){ loadAreas(areasDir); System.out.println("Areas loaded!"); loadPNGs(imgDir); System.out.println("PNGs loaded!"); savePNGs(outputDir); System.out.println("Images saved!"); } static public void main(String args[]){ if(args.length != 3){ System.out.println("Something wrong with your arguments!"); System.out.println("Valid call: AreasMarker pngsDir areasDir outputDir"); } else { new ImgExtractor(args[0],args[1],args[2]); } } public void loadPNGs2(String path){ File[] files = new File(path).listFiles(); imgs = new BufferedImage[files.length]; names = new String[files.length]; try{ for(int i = 0; i < files.length; i++){ names[i] = files[i].getName(); imgs[i] = ImageIO.read(files[i]); } } catch (Exception e){ System.out.println("Cannot open pngs!"); } } public void loadAreas2(String path){ File[] files = new File(path).listFiles(); areas = new int[names.length][][]; for(int i = 0; i < names.length; i++){ File file = findPng(names[i], files); if(file != null){ int areaCount = 0; Scanner sc = null; try { sc = new Scanner(file); while(sc.hasNextLine()){ areaCount++; sc.nextLine(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("Problem with your area file, soz!"); } areas[i] = new int[areaCount][4]; try { sc = new Scanner(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("Problem with your area file 2, soz!"); } for(int j = 0; j < areaCount; j++){ if(sc.next().equals("Image")){ areas[i][j][0] = sc.nextInt(); areas[i][j][1] = sc.nextInt(); areas[i][j][2] = sc.nextInt(); areas[i][j][3] = sc.nextInt(); } } } } } public void loadPNGs(String path){ File[] files = new File(path).listFiles(); imgs = new BufferedImage[names.length]; try{ for(int i = 0; i < files.length; i++){ //System.out.println("Name: " + names[i]); File png = getPNG(names[i], files); imgs[i] = ImageIO.read(png); } } catch (Exception e){ System.out.println("Cannot open pngs!"); } } public void loadAreas(String path){ File[] files = new File(path).listFiles(); names = new String[files.length]; areas = new int[names.length][][]; for(int i = 0; i < names.length; i++){ names[i] = files[i].getName().substring(0, files[i].getName().length()-7); File file = findPng(names[i], files); if(file != null){ int areaCount = 0; Scanner sc = null; try { sc = new Scanner(file); while(sc.hasNextLine()){ areaCount++; sc.nextLine(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("Problem with your area file, soz!"); } areas[i] = new int[areaCount][4]; try { sc = new Scanner(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("Problem with your area file 2, soz!"); } for(int j = 0; j < areaCount; j++){ if(sc.next().equals("Image")){ areas[i][j][0] = sc.nextInt(); areas[i][j][1] = sc.nextInt(); areas[i][j][2] = sc.nextInt(); areas[i][j][3] = sc.nextInt(); } } } } } public File findPng(String file, File[] files){ for(int i = 0; i < files.length; i++){ if(files[i].getName().equals(file+".images")){ return files[i]; } } System.out.println("Didn't find your area description file: " + file); return null; } public File getPNG(String file, File[] files){ for(int i = 0; i < files.length; i++){ if(files[i].getName().equals(file)){ return files[i]; } } System.out.println("Didn't find your png file: " + file); return null; } public void savePNGs(String path){ File dir = new File(path); if(!dir.exists()){ dir.mkdir(); } for(int i = 0; i < names.length; i++){ for(int j = 0; j < areas[i].length; j++){ int width = areas[i][j][2] - areas[i][j][0]+1; int height = areas[i][j][3] - areas[i][j][1]+1; //BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); BufferedImage bi = imgs[i].getSubimage(areas[i][j][0], areas[i][j][1], width, height); try { ImageIO.write(bi, "PNG", new File(path + "//" + names[i].substring(0, names[i].length()-4) + "-" + j + ".png")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("Cannot save your png: " + names[i] + ", soz!"); } } } } public static void showFiles(File[] files) { for (File file : files) { if (file.isDirectory()) { System.out.println("Directory: " + file.getName()); showFiles(file.listFiles()); // Calls same method again. } else { System.out.println("File: " + file.getName()); } } } }