正在处理中的扫描目录和移动图片

Scanning directory and moving pictures in processing

我必须为一个学校项目编写一个代码,它将扫描某个目录(在本例中为 c:/test )以查找其中出现的任何图像文件,并将它们传输到另一个目录,我将在其中存储他们的文件位置,然后将其上传到我的数据库。 出于测试目的,我假设图像文件将出现在 c:/test 目录中,格式为 1.jpeg、2.jpeg 等。现在,如果我将 1.jpeg 文件拖到测试文件夹中,程序就可以运行。但是,当我尝试拖入 2.jpeg 时,代码什么也不做。这是由于 nr_of_files 由于某种原因而增加的事实。有人能给我指出正确的方向吗?

import java.nio.file.*;
import de.bezier.data.sql.*;
import java.io.*;
import java.util.Date;
String[] filenames;
String path = "C:\test\";
int nr_of_files = 0;
int picnames = 1;
String imgonefilepath;
MySQL db;
String user = "root";
String pass = "";
String database = "imageloc";



void setup () {
  db = new MySQL(this, "localhost", database, user, pass);
  checkdir();
}



void draw () {

  if ( checkdir() > 0 ) {
    while ( nr_of_files < filenames.length ) {
      move();
      println( "new file in test folder: " + filenames[nr_of_files++] );
    }
    getPath();
    //sendData();
  }
  println("nr_of_files: " + nr_of_files);
  println("filenames.length" + filenames.length);
  println("picnames: " + picnames);

}

void move() {

  String newpath = "C:\test123\";
  Path source = Paths.get(path + picnames + ".jpeg");
  Path newdir = Paths.get(newpath + picnames + ".jpeg");
  int numberofnewfiles = checkdir();

  if (numberofnewfiles > 0) {

    try {
      Files.move(source, newdir);
    } 
    catch (IOException e) {
      println(e);
    }
  }
  //picnames++;
  println(picnames);
}
void getPath() {
  File img_one = dataFile("C:\test123\1.jpeg");
  String img_onefile_path = img_one.getPath();
  boolean exist = img_one.isFile();

  if (exist == true) {
    println(img_onefile_path);

    if ( db.connect() ) {
      db.query("INSERT INTO ogloc (location) VALUES ('%s')", img_onefile_path);

      println("success");
      println(img_onefile_path);
    } else {
      println("failure");
    }
  }
}
void sendData() {
  if ( db.connect() ) {
    db.query("INSERT INTO ogloc (location) VALUES ('%s')", imgonefilepath);
    // numeric++;
    println("success");
    println(imgonefilepath);
    // Files.move(Paths.get("C:/test/) );
  } else {
    println("failure");
  }
  //println( "INSERT INTO test (b64) VALUES ('" + b64string + "')" );
}


int checkdir() {
  //  println("Listing all control filenames in a directory: ");
  filenames = listFileNames( path );
  //  print("difference in length:  ");
  //  println(filenames.length - nr_of_files);
  return filenames.length - nr_of_files;
}



String[] listFileNames(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    String names[] = file.list();
    return names;
  } else {
    // If it's not a directory
    return null;
  }
}

其中一个问题是您 hard-coding 只是图像 1.jpeg 的路径,因此文件夹中的任何其他图像都不会移动。看看这个代码片段:

String path = "C:\...\test\";

void setup() {
    move();
}

void draw() {
}

void move() {

    File dir1 = new File(path);
    if (dir1.isDirectory()) {

        //Get all files as array from the source directory
        File[] content = dir1.listFiles();

        //Iterate through each file
        for (int i = 0; i < content.length; i++) {
            //Get the file original Path
            Path source = Paths.get(content[i].getPath());
            //Get the file destination path by appending File name to the new Path
            Path newpath = Paths.get("C:\...\test123\" + source.getFileName());

            //Move the files
            try {
                Files.move(source, newpath);
            } catch (IOException e) {
                print(e);
            }
        }
    }
}

此代码段将扫描包含多个图像的源目录,并将这些图像移动到目标目录。剩下的就是添加数据库存储方面 - 例如您可以遍历目标文件夹并以与上面所示类似的方式检索文件路径,或者您可以按照自己的喜好进行操作。

P.S。请注意绘图函数每秒启动 60 次 - 因此,您最好不要将方法放入其中 - draw() 应该真正用于绘制动画和类似的东西。