Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 KB | None | 0 0
  1. import javax.imageio.ImageIO;
  2. import java.awt.*;
  3. import java.awt.image.BufferedImage;
  4. import java.util.*;
  5. import java.io.*;
  6. import java.util.List;
  7. import java.util.concurrent.ExecutorService;
  8. import java.util.concurrent.Executors;
  9.  
  10. import static java.lang.Math.*;
  11.  
  12. public class Main extends PrintWriter {
  13.     BufferedReader br;
  14.     StringTokenizer stok;
  15.     Random rand = new Random(31);
  16.  
  17.     static void resizePhoto(File in, File out) throws IOException {
  18.         BufferedImage img = ImageIO.read(in);
  19.  
  20.         int w = img.getWidth();
  21.         int h = img.getHeight();
  22.         double coef;
  23.         if (w < h) {
  24.             coef = 1080.0 / w;
  25.         } else {
  26.             coef = 1080.0 / h;
  27.         }
  28.         int nw = (int) (coef * w);
  29.         int nh = (int) (coef * h);
  30.         Image scaled = img.getScaledInstance(nw, nh, Image.SCALE_DEFAULT);
  31.  
  32.         BufferedImage res = new BufferedImage(scaled.getWidth(null), scaled.getHeight(null), BufferedImage.TYPE_INT_RGB);
  33.         res.createGraphics().drawImage(scaled, null, null);
  34.  
  35.         out.mkdirs();
  36.         ImageIO.write(res, "JPG", out);
  37.     }
  38.  
  39.     class Worker implements Runnable {
  40.         File in, out;
  41.  
  42.         Worker(File in, File out) {
  43.             this.in = in;
  44.             this.out = out;
  45.         }
  46.  
  47.         @Override
  48.         public void run() {
  49.             try {
  50.                 resizePhoto(in, out);
  51.             } catch (IOException ignore) {
  52.             }
  53.         }
  54.     }
  55.  
  56.     ExecutorService executor = Executors.newFixedThreadPool(7);
  57.  
  58.     void dfs(File f, String path) {
  59.         if (f.isFile()) {
  60.             if (f.getName().toLowerCase().endsWith(".jpg")) {
  61.                 executor.execute(new Worker(f, new File(path)));
  62.             }
  63.         } else {
  64.             File[] files = f.listFiles();
  65.             for (File file : files) {
  66.                 dfs(file, path + "/" + file.getName());
  67.             }
  68.         }
  69.     }
  70.  
  71.     void solve() throws IOException {
  72.         File startDir = new File("/Users/avgarder/Cloud.Mail.Ru/Свадьба фото 1 авг");
  73.         String startPath = "/Users/avgarder/Cloud.Mail.Ru/resize/Свадьба фото 1 авг";
  74.         dfs(startDir, startPath);
  75.         executor.shutdown();
  76.     }
  77.  
  78.     void run() throws IOException {
  79.         try {
  80.             solve();
  81.             close();
  82.         } catch (Exception e) {
  83.             e.printStackTrace();
  84.             System.exit(abs(-1));
  85.         }
  86.     }
  87.  
  88.     public static void main(String[] args) throws IOException {
  89.         try {
  90.             Locale.setDefault(Locale.US);
  91.         } catch (Exception ignore) {
  92.         }
  93.         new Main().run();
  94.     }
  95.  
  96.     Main() {
  97.         super(System.out);
  98.         br = new BufferedReader(new InputStreamReader(System.in));
  99.     }
  100.  
  101.     Main(String s) throws FileNotFoundException {
  102.         super("".equals(s) ? "output.txt" : s + ".out");
  103.         br = new BufferedReader(new FileReader("".equals(s) ? "input.txt" : s + ".in"));
  104.     }
  105.  
  106.     String next() {
  107.         try {
  108.             while (stok == null || !stok.hasMoreTokens()) {
  109.                 stok = new StringTokenizer(br.readLine());
  110.             }
  111.         } catch (IOException e) {
  112.             return null;
  113.         }
  114.         return stok.nextToken();
  115.     }
  116.  
  117.     int nextInt() {
  118.         return Integer.parseInt(next());
  119.     }
  120.  
  121.     long nextLong() {
  122.         return Long.parseLong(next());
  123.     }
  124.  
  125.     double nextDouble() {
  126.         return Double.parseDouble(next());
  127.     }
  128.  
  129.     int[] nextIntArray(int len) {
  130.         int[] res = new int[len];
  131.         for (int i = 0; i < len; i++) {
  132.             res[i] = nextInt();
  133.         }
  134.         return res;
  135.     }
  136.  
  137.     void shuffle(int[] a) {
  138.         for (int i = 1; i < a.length; i++) {
  139.             int x = rand.nextInt(i + 1);
  140.             int t = a[i];
  141.             a[i] = a[x];
  142.             a[x] = t;
  143.         }
  144.     }
  145.  
  146.     boolean nextPerm(int[] p) {
  147.         for (int a = p.length - 2; a >= 0; --a)
  148.             if (p[a] < p[a + 1])
  149.                 for (int b = p.length - 1; ; --b)
  150.                     if (p[b] > p[a]) {
  151.                         int t = p[a];
  152.                         p[a] = p[b];
  153.                         p[b] = t;
  154.                         for (++a, b = p.length - 1; a < b; ++a, --b) {
  155.                             t = p[a];
  156.                             p[a] = p[b];
  157.                             p[b] = t;
  158.                         }
  159.                         return true;
  160.                     }
  161.         return false;
  162.     }
  163.  
  164.     <T> List<T>[] genAdjacencyList(int countVertex) {
  165.         List<T>[] res = new List[countVertex];
  166.         for (int i = 0; i < countVertex; i++) {
  167.             res[i] = new ArrayList<T>();
  168.         }
  169.         return res;
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement