First commit
This commit is contained in:
parent
e74b60b2b6
commit
5b86206864
7
ImageTimeRenameByExifGUI/.classpath
Normal file
7
ImageTimeRenameByExifGUI/.classpath
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/ImageTimeRenameByExifLib"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
17
ImageTimeRenameByExifGUI/.project
Normal file
17
ImageTimeRenameByExifGUI/.project
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>ImageTimeRenameByExifGUI</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@ -0,0 +1,25 @@
|
||||
package it.danieleverducci.renamebyexif.example.logic;
|
||||
|
||||
import it.danieleverducci.renamebyexif.example.window.ShowDialog;
|
||||
|
||||
import javax.swing.SwingWorker;
|
||||
|
||||
public class BackgroundTaskRunner extends SwingWorker<Void, Void> {
|
||||
private PhotoOrdererLauncher po;
|
||||
private String source;
|
||||
private String destination;
|
||||
|
||||
public BackgroundTaskRunner(PhotoOrdererLauncher po, String source, String destination) {
|
||||
this.po = po;
|
||||
this.source = source;
|
||||
this.destination = destination;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground() throws Exception {
|
||||
//Start file ordering
|
||||
if(!po.orderPhotos(source, destination)) ShowDialog.errorDialog();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package it.danieleverducci.renamebyexif.example.logic;
|
||||
|
||||
import it.danieleverducci.renamebyexif.example.window.ShowDialog;
|
||||
import it.danieleverducci.renamebyexif.lib.PhotoOrderer;
|
||||
import it.danieleverducci.renamebyexif.lib.interfaces.OnProgressUpdateListener;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class PhotoOrdererLauncher implements OnProgressUpdateListener {
|
||||
private OnProgressUpdateListener onProgressUpdateListener;
|
||||
|
||||
public boolean orderPhotos(String sourceDirectory, String destinationDirectory) {
|
||||
File sourceDir = new File(sourceDirectory);
|
||||
if(!sourceDir.exists() || !sourceDir.isDirectory()) return false;
|
||||
File destDir = new File(destinationDirectory);
|
||||
|
||||
//Verify if destDir exist. Create it if not.
|
||||
boolean proceed=false;
|
||||
if(!destDir.isDirectory()){
|
||||
if(ShowDialog.createFolderDialog(destinationDirectory)){
|
||||
proceed = destDir.mkdirs();
|
||||
}
|
||||
} else proceed=true;
|
||||
|
||||
if(proceed) {
|
||||
PhotoOrderer po = new PhotoOrderer();
|
||||
po.setLogging(true);
|
||||
po.setRecursive(true);
|
||||
po.setSkipSingleFilesError(true);
|
||||
po.setOnProgressUpdateListener(this);
|
||||
try {
|
||||
po.renameAndMoveFilesInDir(sourceDir, destDir);
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else onProgressUpdateListener.onProgressUpdate(-1);
|
||||
return proceed;
|
||||
}
|
||||
|
||||
public void setOnProgressUpdateListener(OnProgressUpdateListener listener){
|
||||
this.onProgressUpdateListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProgressUpdate(int progress) {
|
||||
onProgressUpdateListener.onProgressUpdate(progress);
|
||||
}
|
||||
}
|
@ -0,0 +1,163 @@
|
||||
package it.danieleverducci.renamebyexif.example.window;
|
||||
|
||||
import it.danieleverducci.renamebyexif.example.logic.BackgroundTaskRunner;
|
||||
import it.danieleverducci.renamebyexif.example.logic.PhotoOrdererLauncher;
|
||||
import it.danieleverducci.renamebyexif.lib.interfaces.OnProgressUpdateListener;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JProgressBar;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
public class MainWindow implements OnProgressUpdateListener {
|
||||
|
||||
private JFrame frmImageordererByDaniele;
|
||||
private JTextField sourceTextView;
|
||||
private JTextField destinationTextView;
|
||||
private JButton destinationSelectButton;
|
||||
private JProgressBar progressBar;
|
||||
private JButton btnOrderFiles;
|
||||
private PhotoOrdererLauncher po;
|
||||
|
||||
/**
|
||||
* Launch the application.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
MainWindow window = new MainWindow();
|
||||
window.frmImageordererByDaniele.setVisible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the application.
|
||||
*/
|
||||
public MainWindow() {
|
||||
initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the contents of the frame.
|
||||
*/
|
||||
private void initialize() {
|
||||
frmImageordererByDaniele = new JFrame();
|
||||
frmImageordererByDaniele.setTitle("ImageOrderer by Daniele Verducci");
|
||||
frmImageordererByDaniele.setBounds(100, 100, 700, 211);
|
||||
frmImageordererByDaniele.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frmImageordererByDaniele.getContentPane().setLayout(null);
|
||||
frmImageordererByDaniele.setResizable(false);
|
||||
|
||||
sourceTextView = new JTextField();
|
||||
sourceTextView.setBounds(12, 15, 539, 19);
|
||||
frmImageordererByDaniele.getContentPane().add(sourceTextView);
|
||||
sourceTextView.setColumns(10);
|
||||
|
||||
JButton sourceSelectButton = new JButton("Source");
|
||||
sourceSelectButton.addActionListener(new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
//Open file window
|
||||
String selectedPath = MainWindow.showFileDialog(sourceTextView.getText());
|
||||
if(selectedPath!=null) sourceTextView.setText(selectedPath);
|
||||
}
|
||||
});
|
||||
sourceSelectButton.setBounds(563, 12, 117, 25);
|
||||
frmImageordererByDaniele.getContentPane().add(sourceSelectButton);
|
||||
|
||||
destinationTextView = new JTextField();
|
||||
destinationTextView.setColumns(10);
|
||||
destinationTextView.setBounds(12, 49, 539, 19);
|
||||
frmImageordererByDaniele.getContentPane().add(destinationTextView);
|
||||
|
||||
destinationSelectButton = new JButton("Destination");
|
||||
destinationSelectButton.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
//Open file window
|
||||
String selectedPath = MainWindow.showFileDialog(sourceTextView.getText());
|
||||
if(selectedPath!=null) destinationTextView.setText(selectedPath);
|
||||
}
|
||||
});
|
||||
destinationSelectButton.setBounds(563, 49, 117, 25);
|
||||
frmImageordererByDaniele.getContentPane().add(destinationSelectButton);
|
||||
|
||||
progressBar = new JProgressBar();
|
||||
progressBar.setBounds(22, 105, 658, 14);
|
||||
progressBar.setIndeterminate(false);
|
||||
progressBar.setMaximum(100);
|
||||
frmImageordererByDaniele.getContentPane().add(progressBar);
|
||||
|
||||
btnOrderFiles = new JButton("Order files");
|
||||
btnOrderFiles.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
//User clicked the start button
|
||||
po = new PhotoOrdererLauncher();
|
||||
po.setOnProgressUpdateListener(MainWindow.this);
|
||||
String source = sourceTextView.getText();
|
||||
String destination = destinationTextView.getText();
|
||||
//Execute code in background to not block the UI
|
||||
BackgroundTaskRunner tr = new BackgroundTaskRunner(po, source, destination);
|
||||
progressBar.setValue(0);
|
||||
//Disable button to prevent user click it a second time
|
||||
btnOrderFiles.setEnabled(false);
|
||||
//Start thread
|
||||
tr.execute();
|
||||
}
|
||||
});
|
||||
btnOrderFiles.setBounds(309, 135, 117, 25);
|
||||
frmImageordererByDaniele.getContentPane().add(btnOrderFiles);
|
||||
|
||||
//Set source and destination dirs default
|
||||
File currentDirFile = new File("");
|
||||
String currentDir = "/";
|
||||
try {
|
||||
currentDir = currentDirFile.getCanonicalPath();
|
||||
} catch (IOException e) {}
|
||||
sourceTextView.setText(currentDir);
|
||||
destinationTextView.setText(currentDir);
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected static String showFileDialog(String currentPath) {
|
||||
File currentPathFile = new File(currentPath);
|
||||
if(!(currentPathFile.exists()&¤tPathFile.isDirectory())) currentPath="";
|
||||
|
||||
JFileChooser ch = new JFileChooser(new File(currentPath));
|
||||
ch.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
||||
int userSelectedButton = ch.showOpenDialog(null);
|
||||
if(userSelectedButton==JFileChooser.APPROVE_OPTION){
|
||||
//User clicked yes. Return dir path
|
||||
File selectedDir = ch.getSelectedFile();
|
||||
try {
|
||||
return selectedDir.getCanonicalPath();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error: unable to get user selected path.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProgressUpdate(int percent) {
|
||||
if(percent>99 || percent<0) {
|
||||
btnOrderFiles.setEnabled(true);
|
||||
progressBar.setValue(0);
|
||||
} else progressBar.setValue(percent);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package it.danieleverducci.renamebyexif.example.window;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
public class ShowDialog {
|
||||
/**
|
||||
* Shows a dialog asking the user to create a folder
|
||||
* @param folder the folder name to display to the user
|
||||
* @return true if the user wants the folder to be created, false otherwise
|
||||
*/
|
||||
public static boolean createFolderDialog(String folder){
|
||||
if(folder==null || folder.equals("")) return false;
|
||||
|
||||
String[] options = {"Yes", "No"};
|
||||
String title = "Create folder?";
|
||||
String message = "The folder "+folder+" does not exist. Do you want to create it?";
|
||||
|
||||
int result = JOptionPane.showOptionDialog(null, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
|
||||
return result==0;
|
||||
}
|
||||
|
||||
public static void errorDialog(){
|
||||
JOptionPane.showMessageDialog(null,
|
||||
"Unable to proceed: the destination folder does not exist.",
|
||||
"Operation not started",
|
||||
JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
}
|
8
ImageTimeRenameByExifLib/.classpath
Normal file
8
ImageTimeRenameByExifLib/.classpath
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
|
||||
<classpathentry kind="lib" path="libs/metadata-extractor-2.6.4.jar"/>
|
||||
<classpathentry kind="lib" path="libs/xmpcore.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
17
ImageTimeRenameByExifLib/.project
Normal file
17
ImageTimeRenameByExifLib/.project
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>ImageTimeRenameByExifLib</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
54
ImageTimeRenameByExifLib/libs/metadata_extractor_license.txt
Normal file
54
ImageTimeRenameByExifLib/libs/metadata_extractor_license.txt
Normal file
@ -0,0 +1,54 @@
|
||||
Apache License
|
||||
|
||||
Version 2.0, January 2004
|
||||
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
|
||||
You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
14
ImageTimeRenameByExifLib/libs/xmpcore_license.txt
Normal file
14
ImageTimeRenameByExifLib/libs/xmpcore_license.txt
Normal file
@ -0,0 +1,14 @@
|
||||
|
||||
The BSD License
|
||||
|
||||
Copyright (c) 2009, Adobe Systems Incorporated All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of Adobe Systems Incorporated, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANT ABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
@ -0,0 +1,164 @@
|
||||
package it.danieleverducci.renamebyexif.lib;
|
||||
|
||||
import it.danieleverducci.renamebyexif.lib.disk.JpegFileFilter;
|
||||
import it.danieleverducci.renamebyexif.lib.interfaces.OnProgressUpdateListener;
|
||||
import it.danieleverducci.renamebyexif.lib.interfaces.OnSingleFileProgressUpdateListener;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import com.drew.imaging.ImageMetadataReader;
|
||||
import com.drew.metadata.Metadata;
|
||||
import com.drew.metadata.exif.ExifSubIFDDirectory;
|
||||
|
||||
public class PhotoOrderer {
|
||||
private int totalFilesToProcess = 0;
|
||||
private int processedFiles = 0;
|
||||
private OnProgressUpdateListener onProgressUpdateListener;
|
||||
private OnSingleFileProgressUpdateListener onSingleFileProgressUpdateListener;
|
||||
private boolean logging = false;
|
||||
private boolean skipSingleFilesError = true;
|
||||
private boolean recursive = true;
|
||||
|
||||
/**
|
||||
* Cycle all files and rename+move it according to the EXIF or file creation data.
|
||||
* @param sourceDir is the directory containing all the files to be processed
|
||||
* @param destDir is the destination directory
|
||||
* @throws IOException if a file cannot be read or written while skipSingleFileError is set to false.
|
||||
* @throws IllegalArgumentException if sourceDir or destDir is not a directory, does not exist or cannot be read/written
|
||||
*/
|
||||
public void renameAndMoveFilesInDir(File sourceDir, File destDir) throws IOException, IllegalArgumentException{
|
||||
if(!sourceDir.isDirectory() || !destDir.isDirectory())
|
||||
throw new IllegalArgumentException("The source or destination directory does not exist or is not a directory");
|
||||
File[] filesInSourceDir = sourceDir.listFiles(new JpegFileFilter());
|
||||
renameAndMoveFileArray(filesInSourceDir, destDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cycle all files and rename+move it according to the EXIF or file creation data.
|
||||
* @param filesInSourceDir is an array of files to be processed,
|
||||
* @param destDir is the destination directory
|
||||
* @throws IOException if a file cannot be read or written while skipSingleFileError is set to false.
|
||||
*/
|
||||
public void renameAndMoveFileArray(File[] filesInSourceDir, File destDir) throws IOException {
|
||||
if(!destDir.isDirectory())
|
||||
throw new IllegalArgumentException("The destination directory does not exist or is not a directory");
|
||||
|
||||
totalFilesToProcess = filesInSourceDir.length;
|
||||
for(int i=0; i<filesInSourceDir.length; i++){
|
||||
if(filesInSourceDir[i].isFile()){
|
||||
try {
|
||||
renameAndMoveSingleFile(filesInSourceDir[i], destDir);
|
||||
} catch (IOException e) {
|
||||
if(!skipSingleFilesError) throw e;
|
||||
else {
|
||||
if(logging) {
|
||||
System.out.println("Disk I/O error:");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if(recursive) renameAndMoveFileArray(filesInSourceDir[i].listFiles(new JpegFileFilter()), destDir);
|
||||
}
|
||||
//Rename finished: reinitialize for object recycling
|
||||
processedFiles=0;
|
||||
}
|
||||
|
||||
private void renameAndMoveSingleFile(File sourceFile, File destinationDirectory) throws IOException{
|
||||
//Retrieve exif data
|
||||
Metadata metadata;
|
||||
try {
|
||||
metadata = ImageMetadataReader.readMetadata(sourceFile);
|
||||
} catch (Exception e) {
|
||||
if(logging) System.out.println("Unable to get image metadata for "+sourceFile.getName()+"\nThe file will be ignored.");
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
Date creationDate;
|
||||
if(metadata!=null){
|
||||
//Retrieve image shooting date from exif data
|
||||
ExifSubIFDDirectory directory = metadata.getDirectory(ExifSubIFDDirectory.class);
|
||||
creationDate = directory.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
|
||||
} else {
|
||||
//If exif data is unavailable, retrieve file creation date
|
||||
Path path = Paths.get(sourceFile.getCanonicalPath());
|
||||
BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
|
||||
FileTime creationTime = attributes.creationTime();
|
||||
creationDate = new Date(creationTime.toMillis());
|
||||
}
|
||||
if(creationDate==null) {
|
||||
if(logging) System.out.println("Unable to retrieve date for file "+sourceFile.getName()+". It will be ignored.");
|
||||
return;
|
||||
}
|
||||
String creationDateString = new SimpleDateFormat("yyyy-MM-dd HH-mm").format(creationDate);
|
||||
File destinationFile;
|
||||
int fileCounter = 0;
|
||||
//Exif data contains the date minutes, but not seconds. So there can be more photos taken in the same minute.
|
||||
//To avoid file collision, we add a counter at the end of the file and increment it by 1 if the file exists.
|
||||
do{
|
||||
destinationFile = new File(destinationDirectory.getCanonicalFile().toString()+File.separator+creationDateString+" - "+fileCounter+".jpg");
|
||||
fileCounter++;
|
||||
}while(destinationFile.exists());
|
||||
|
||||
processedFiles++;
|
||||
//Copy file
|
||||
try{
|
||||
Files.copy(sourceFile.toPath(), destinationFile.toPath());
|
||||
if(logging) System.out.println(sourceFile.getName()+" renamed to "+destinationFile.getName());
|
||||
if(onSingleFileProgressUpdateListener!=null) onSingleFileProgressUpdateListener.onSingleFileProgressUpdate(sourceFile, destinationFile, processedFiles, totalFilesToProcess, true);
|
||||
}catch(IOException e){
|
||||
if(logging) System.out.println(sourceFile.getName()+" renaming to "+destinationFile.getName()+" failed: "+e.toString());
|
||||
if(onSingleFileProgressUpdateListener!=null) onSingleFileProgressUpdateListener.onSingleFileProgressUpdate(sourceFile, destinationFile, processedFiles, totalFilesToProcess, false);
|
||||
throw e;
|
||||
}
|
||||
//Notify progression update
|
||||
if(onProgressUpdateListener!=null) onProgressUpdateListener.onProgressUpdate((int)(((float)processedFiles/(float)totalFilesToProcess)*100.0f)+1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Enable or disable console logging
|
||||
*/
|
||||
public void setLogging(boolean logging) {
|
||||
this.logging = logging;
|
||||
}
|
||||
|
||||
/**
|
||||
* If @param skipSingleFilesError is set to true, the rename will continue ignoring errors.
|
||||
* Otherwise, an exception will be throw
|
||||
*/
|
||||
public void setSkipSingleFilesError(boolean skipSingleFilesError) {
|
||||
this.skipSingleFilesError = skipSingleFilesError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called to notify the progression file by file
|
||||
*/
|
||||
public void setOnSingleFileProgressUpdateListener(
|
||||
OnSingleFileProgressUpdateListener onSingleFileProgressUpdateListener) {
|
||||
this.onSingleFileProgressUpdateListener = onSingleFileProgressUpdateListener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called to notify the progression in percentage
|
||||
*/
|
||||
public void setOnProgressUpdateListener(OnProgressUpdateListener onProgressUpdateListener) {
|
||||
this.onProgressUpdateListener = onProgressUpdateListener;
|
||||
}
|
||||
|
||||
/**
|
||||
* If @param recursive is set to true, the directories found will be recursively scanned searching
|
||||
* for other orderable files
|
||||
*/
|
||||
public void setRecursive(boolean recursive) {
|
||||
this.recursive = recursive;
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package it.danieleverducci.renamebyexif.lib.disk;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class JpegFileFilter implements java.io.FileFilter {
|
||||
private static final String[] SUPPORTED_FILE_TYPES = {".jpg", "jpeg"};
|
||||
|
||||
@Override
|
||||
public boolean accept(File pathname) {
|
||||
String filename = pathname.getName();
|
||||
if(filename.length()<4) return false;
|
||||
String extension = (filename.substring(filename.length()-4)).toLowerCase(); //Last 4 letters of the name. Es: .jpg, .png, jpeg etc
|
||||
for(int i=0; i<SUPPORTED_FILE_TYPES.length; i++){
|
||||
if(extension.equals(SUPPORTED_FILE_TYPES[i].toLowerCase())) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package it.danieleverducci.renamebyexif.lib.interfaces;
|
||||
|
||||
public interface OnProgressUpdateListener {
|
||||
/**
|
||||
* Called on every progress update.
|
||||
* The parameter @param progress is an integer from 0 to 100 representing
|
||||
* the percent of files renamed.
|
||||
*/
|
||||
public void onProgressUpdate(int progress);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package it.danieleverducci.renamebyexif.lib.interfaces;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public interface OnSingleFileProgressUpdateListener {
|
||||
public void onSingleFileProgressUpdate(File source, File destination, int currentFileCount, int totalFileCount, boolean success);
|
||||
}
|
Loading…
Reference in New Issue
Block a user