Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.util.*;
- import java.io.*;
- import java.util.List;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import static java.lang.Math.*;
- public class Main extends PrintWriter {
- BufferedReader br;
- StringTokenizer stok;
- Random rand = new Random(31);
- static void resizePhoto(File in, File out) throws IOException {
- BufferedImage img = ImageIO.read(in);
- int w = img.getWidth();
- int h = img.getHeight();
- double coef;
- if (w < h) {
- coef = 1080.0 / w;
- } else {
- coef = 1080.0 / h;
- }
- int nw = (int) (coef * w);
- int nh = (int) (coef * h);
- Image scaled = img.getScaledInstance(nw, nh, Image.SCALE_DEFAULT);
- BufferedImage res = new BufferedImage(scaled.getWidth(null), scaled.getHeight(null), BufferedImage.TYPE_INT_RGB);
- res.createGraphics().drawImage(scaled, null, null);
- out.mkdirs();
- ImageIO.write(res, "JPG", out);
- }
- class Worker implements Runnable {
- File in, out;
- Worker(File in, File out) {
- this.in = in;
- this.out = out;
- }
- @Override
- public void run() {
- try {
- resizePhoto(in, out);
- } catch (IOException ignore) {
- }
- }
- }
- ExecutorService executor = Executors.newFixedThreadPool(7);
- void dfs(File f, String path) {
- if (f.isFile()) {
- if (f.getName().toLowerCase().endsWith(".jpg")) {
- executor.execute(new Worker(f, new File(path)));
- }
- } else {
- File[] files = f.listFiles();
- for (File file : files) {
- dfs(file, path + "/" + file.getName());
- }
- }
- }
- void solve() throws IOException {
- File startDir = new File("/Users/avgarder/Cloud.Mail.Ru/Свадьба фото 1 авг");
- String startPath = "/Users/avgarder/Cloud.Mail.Ru/resize/Свадьба фото 1 авг";
- dfs(startDir, startPath);
- executor.shutdown();
- }
- void run() throws IOException {
- try {
- solve();
- close();
- } catch (Exception e) {
- e.printStackTrace();
- System.exit(abs(-1));
- }
- }
- public static void main(String[] args) throws IOException {
- try {
- Locale.setDefault(Locale.US);
- } catch (Exception ignore) {
- }
- new Main().run();
- }
- Main() {
- super(System.out);
- br = new BufferedReader(new InputStreamReader(System.in));
- }
- Main(String s) throws FileNotFoundException {
- super("".equals(s) ? "output.txt" : s + ".out");
- br = new BufferedReader(new FileReader("".equals(s) ? "input.txt" : s + ".in"));
- }
- String next() {
- try {
- while (stok == null || !stok.hasMoreTokens()) {
- stok = new StringTokenizer(br.readLine());
- }
- } catch (IOException e) {
- return null;
- }
- return stok.nextToken();
- }
- int nextInt() {
- return Integer.parseInt(next());
- }
- long nextLong() {
- return Long.parseLong(next());
- }
- double nextDouble() {
- return Double.parseDouble(next());
- }
- int[] nextIntArray(int len) {
- int[] res = new int[len];
- for (int i = 0; i < len; i++) {
- res[i] = nextInt();
- }
- return res;
- }
- void shuffle(int[] a) {
- for (int i = 1; i < a.length; i++) {
- int x = rand.nextInt(i + 1);
- int t = a[i];
- a[i] = a[x];
- a[x] = t;
- }
- }
- boolean nextPerm(int[] p) {
- for (int a = p.length - 2; a >= 0; --a)
- if (p[a] < p[a + 1])
- for (int b = p.length - 1; ; --b)
- if (p[b] > p[a]) {
- int t = p[a];
- p[a] = p[b];
- p[b] = t;
- for (++a, b = p.length - 1; a < b; ++a, --b) {
- t = p[a];
- p[a] = p[b];
- p[b] = t;
- }
- return true;
- }
- return false;
- }
- <T> List<T>[] genAdjacencyList(int countVertex) {
- List<T>[] res = new List[countVertex];
- for (int i = 0; i < countVertex; i++) {
- res[i] = new ArrayList<T>();
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement