Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.corba.se.impl.oa.poa.ActiveObjectMap.Key;
- import com.sun.media.jai.codec.FileSeekableStream;
- import com.sun.media.jai.codec.ImageCodec;
- import com.sun.media.jai.codec.ImageDecoder;
- import com.sun.media.jai.codec.ImageEncoder;
- import com.sun.media.jai.codec.SeekableStream;
- import com.sun.media.jai.codec.TIFFEncodeParam;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.StringWriter;
- import java.io.Writer;
- import java.util.AbstractList;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.media.jai.NullOpImage;
- import javax.media.jai.OpImage;
- import javax.media.jai.PlanarImage;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.filechooser.FileFilter;
- import javax.swing.filechooser.FileNameExtensionFilter;
- import javax.xml.transform.OutputKeys;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- import org.w3c.dom.Node;
- import sun.awt.SunHints.Value;
- /**
- *
- * @author ram
- */
- public class MergeTifFile extends javax.swing.JFrame {
- String dir = "";
- File dirr = null;
- String to = "";
- File too = null;
- String sym = "";
- HashMap<String, ArrayList<File>> fileList = new HashMap<>();
- private static FileFilterName tifName = new FileFilterName(".tif");
- // Her sætter jeg radiobutton og textboxs til at være true og false fra start.
- public MergeTifFile() {
- initComponents();
- rbtFolder.setSelected(true);
- txtSymbol.setEnabled(false);
- buttonGroup1.add(rbtFolder);
- buttonGroup1.add(rbtFile);
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- buttonGroup1 = new javax.swing.ButtonGroup();
- btnStart = new javax.swing.JButton();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- txtFromFolder = new javax.swing.JTextField();
- txtSaveTo = new javax.swing.JTextField();
- btnFrom = new javax.swing.JButton();
- btnTo = new javax.swing.JButton();
- rbtFolder = new javax.swing.JRadioButton();
- rbtFile = new javax.swing.JRadioButton();
- jLabel3 = new javax.swing.JLabel();
- txtSymbol = new javax.swing.JTextField();
- jLabel4 = new javax.swing.JLabel();
- jLabel5 = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- btnStart.setText("Start");
- btnStart.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnStartActionPerformed(evt);
- }
- });
- jLabel1.setText("From Folder:");
- jLabel2.setText("To Folder:");
- txtFromFolder.setEnabled(false);
- txtFromFolder.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- txtFromFolderActionPerformed(evt);
- }
- });
- txtSaveTo.setEnabled(false);
- txtSaveTo.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- txtSaveToActionPerformed(evt);
- }
- });
- btnFrom.setText("Browser");
- btnFrom.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnFromActionPerformed(evt);
- }
- });
- btnTo.setText("Browser");
- btnTo.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnToActionPerformed(evt);
- }
- });
- rbtFolder.setText("Folder");
- rbtFolder.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- rbtFolderActionPerformed(evt);
- }
- });
- rbtFile.setText("File");
- rbtFile.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- rbtFileActionPerformed(evt);
- }
- });
- jLabel3.setText("Split after:");
- txtSymbol.setText("-");
- jLabel4.setText("fx. /, \\, +, -, _ ");
- jLabel5.setText("if \".\" Enter \\\\. ");
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel3)
- .addGap(30, 30, 30)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(txtSymbol, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel5)
- .addComponent(jLabel4)))
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
- .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(rbtFolder)
- .addComponent(rbtFile)))
- .addGroup(layout.createSequentialGroup()
- .addGap(20, 20, 20)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(txtFromFolder, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(txtSaveTo, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE))))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(btnFrom)
- .addComponent(btnTo)))))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(17, 17, 17)
- .addComponent(rbtFolder)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(rbtFile)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel1)
- .addComponent(txtFromFolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(btnFrom))
- .addGap(35, 35, 35)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel2)
- .addComponent(txtSaveTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(btnTo))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel3)
- .addComponent(txtSymbol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jLabel4)
- .addGap(3, 3, 3)
- .addComponent(jLabel5)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(btnStart)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {
- //Hvis fra og til text boxs er tomme, så udføre ikke dette.
- if (txtFromFolder.getText().isEmpty() && txtSaveTo.getText().isEmpty()) {
- JOptionPane.showMessageDialog(null, "Try again.", "Fail", JOptionPane.INFORMATION_MESSAGE);
- }
- else
- {
- try
- {
- //indsætter ekstra \ hvis der står et i forvejen.
- dir = txtFromFolder.getText();
- dir = dir.replace("\\", "\\\\");
- System.out.println(dir);
- dirr = new File(dir);
- }
- catch (Exception e)
- {
- // Hvis ingen mapper findes.
- JOptionPane.showMessageDialog(null, "No folder found, try again.", "Fail.", JOptionPane.INFORMATION_MESSAGE);
- }
- try
- {
- //indsætter ekstra \ hvis der står et i forvejen.
- to = txtSaveTo.getText();
- to = to.replace("\\", "\\\\");
- System.out.println(to);
- too = new File(to);
- }
- catch (Exception e)
- {
- // Hvis ingen mapper findes.
- JOptionPane.showMessageDialog(null, "Cannot save this file.", "Fail.", JOptionPane.INFORMATION_MESSAGE);
- txtSaveTo.setText("");
- }
- try
- {
- sym = txtSymbol.getText();
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(null, "Enter in spilt box.", "Fail.", JOptionPane.INFORMATION_MESSAGE);
- }
- if (txtFromFolder.getText() == null && txtSaveTo.getText() == null)
- {
- System.err.println("Udfylde begge felter! Og prøv igen.");
- }
- else
- {
- if (rbtFolder.isSelected())
- {
- if(txtSymbol.getText() != null)
- {
- try
- {
- findFiles1(dirr);
- merg();
- }
- catch (Exception ex)
- {
- Logger.getLogger(MergeTifFile.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- else
- {
- JOptionPane.showMessageDialog(null, "Enter Symbol in .....!", "Fail", JOptionPane.INFORMATION_MESSAGE);
- }
- }
- else
- {
- try
- {
- findFiles1(dirr);
- }
- catch (Exception ex)
- {
- Logger.getLogger(MergeTifFile.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- }
- }
- private void rbtFolderActionPerformed(java.awt.event.ActionEvent evt) {
- //Hvis folder radiobutton er valget, så skal man ikke kunne skrive i txtSymbol.
- if (rbtFolder.isSelected()) {
- // JOptionPane.showMessageDialog(null, "You have selected Folder program.", "Folder", JOptionPane.INFORMATION_MESSAGE);
- txtSymbol.setEnabled(false);
- }
- }
- private void btnFromActionPerformed(java.awt.event.ActionEvent evt) {
- //Her fra vælger man hvilket mappe man ønsker at hente tif filer flere.
- FileFilter filter = new FileNameExtensionFilter("Tif File","tif");
- JFileChooser folderChooser = new JFileChooser();
- folderChooser.setCurrentDirectory(new java.io.File("."));
- folderChooser.addChoosableFileFilter(filter);
- folderChooser.setDialogTitle("Find folder");
- folderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- folderChooser.setAcceptAllFileFilterUsed(false);
- folderChooser.showOpenDialog(null);
- File f = folderChooser.getSelectedFile();
- String folderName = f.getAbsolutePath();
- txtFromFolder.setText(folderName);
- System.out.println("Load from folder: " + dirr);
- }
- private void btnToActionPerformed(java.awt.event.ActionEvent evt) {
- //Her til vælger man hvor den nye tif fil(er) skal lægges.
- JFrame parentFrame = new JFrame();
- JFileChooser fileChooser = new JFileChooser();
- fileChooser.setDialogTitle("Save file to...");
- fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- int userSelection = fileChooser.showSaveDialog(parentFrame);
- if (userSelection == JFileChooser.APPROVE_OPTION) {
- File fileToSave = fileChooser.getSelectedFile();
- String folderName = fileToSave.getAbsolutePath();
- txtSaveTo.setText(folderName);
- System.out.println("Save in folder: " + too);
- }
- }
- private void rbtFileActionPerformed(java.awt.event.ActionEvent evt) {
- //Vælger man rediobutton for filer, så skal man også skrive i txtSymbol, hvor filen skal splittes.
- if (rbtFile.isSelected())
- {
- if(txtSymbol.getText() != null || txtSymbol.getText() != "")
- {
- try
- {
- // JOptionPane.showMessageDialog(null, "You have selected File program.", "File", JOptionPane.INFORMATION_MESSAGE);
- txtSymbol.setEnabled(true);
- }
- catch (Exception ex)
- {
- Logger.getLogger(MergeTifFile.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- else
- {
- System.out.println("Enter Symbol in textbox. And try again.");
- }
- }
- }
- private void txtFromFolderActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void txtSaveToActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- //Her bliver der sorteret ud fra filtype og så bliver filen også smidt ind i en hashmap, hvis den ikke findes ellers lagt til en arrayList.
- public void findFiles1(File dir) throws IOException, Exception {
- File[] listOfFolder = dir.listFiles();
- //Folder
- for (File file : listOfFolder) {
- if (file.isDirectory()) {
- findFiles1(file);
- } else
- {
- if (tifName.accept(dir, file.getName()))
- {
- if (file.getName().endsWith(".tiff") || file.getName().endsWith(".tif"))
- {
- String key = file.getName().split(sym)[0];
- if (fileList.containsKey(key)) {
- fileList.get(key).add(file);
- System.out.println("Old file found: " + file.getName().split(sym)[0] + " is old: " + fileList.get(key).add(file) + file.getAbsolutePath());
- // List mapValues = ArrayList(fileList.values());
- Collections.sort(fileList.get(key));
- }
- else
- {
- fileList.put(key, new ArrayList<File>());
- fileList.get(key).add(file);
- System.out.println("New file found: " + file.getName().split(sym)[0] + " is new: " + fileList.get(key).add(file));
- System.out.println("Put new file i arrayList: " + fileList.put(key, new ArrayList<File>()));
- System.out.println("File name: " + file.getName() + " and file location " + file.getAbsolutePath());
- String fil = file.getAbsolutePath();
- }
- // }
- List mapKeys = new ArrayList(fileList.keySet());
- Collections.sort(mapKeys);
- }
- }
- else
- {
- System.err.println("This file : " + file.getName() + " not tif file.");
- }
- }
- }
- System.err.println("Done");
- }
- //Her sker alt samling af tif filer, ud fra den liste man får fra hashmappen.
- public void merg() throws IOException
- {
- String [] tifs = {};
- int numTifs = tifs.length; // 2 or many pages
- BufferedImage image[] = new BufferedImage[numTifs];
- for (int i = 0; i < numTifs; i++)
- {
- // SeekableStream ss = new FileSeekableStream(tifs[i]);
- try (SeekableStream ss = new FileSeekableStream(tifs[i])) {
- // SeekableStream ss = new FileSeekableStream(tifs[i]);
- ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", ss, null);
- PlanarImage pi = new NullOpImage(decoder.decodeAsRenderedImage(0), null, null, OpImage.OP_IO_BOUND);
- image[i] = pi.getAsBufferedImage();
- }
- }
- TIFFEncodeParam params = new TIFFEncodeParam();
- params.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
- System.out.println("Output: " + too);
- OutputStream out = new FileOutputStream(too + "\\" + "fff" + ".tif");
- ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out, params);
- List<BufferedImage> list = new ArrayList<BufferedImage>(image.length);
- for (int i = 1; i < image.length; i++) {
- list.add(image[i]);
- }
- params.setExtraImages(list.iterator());
- encoder.encode(image[0]);
- out.close();
- }
- // Tjekker om det er et Int eller en String, og hvis det er en String, så vil den returne false ellers true.
- public static boolean isNumeric(String str) {
- try {
- int i = Integer.parseInt(str);
- } catch (NumberFormatException nfe) {
- return false;
- }
- return true;
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(MergeTifFile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(MergeTifFile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(MergeTifFile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(MergeTifFile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new MergeTifFile().setVisible(true);
- }
- });
- }
- public static final void prettyPrint(Node xml) throws Exception {
- Transformer tf = TransformerFactory.newInstance().newTransformer();
- tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
- tf.setOutputProperty(OutputKeys.INDENT, "yes");
- Writer out = new StringWriter();
- tf.transform(new DOMSource(xml), new StreamResult(out));
- System.out.println(out.toString());
- }
- // Variables declaration - do not modify
- private javax.swing.JButton btnFrom;
- private javax.swing.JButton btnStart;
- private javax.swing.JButton btnTo;
- private javax.swing.ButtonGroup buttonGroup1;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JLabel jLabel4;
- private javax.swing.JLabel jLabel5;
- private javax.swing.JRadioButton rbtFile;
- private javax.swing.JRadioButton rbtFolder;
- private javax.swing.JTextField txtFromFolder;
- private javax.swing.JTextField txtSaveTo;
- private javax.swing.JTextField txtSymbol;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement