Advertisement
JackoJammie

Untitled

Jul 14th, 2011
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.51 KB | None | 0 0
  1.     private void exportHero(int exportHero, File exportTarget) throws XMLStreamException, NoSuchAlgorithmException, FileNotFoundException, IOException, Exception {
  2.         PluginHeld2 hero = heroesList[exportHero];
  3.         heroTool.setAktivenHeld(hero);
  4.  
  5.         if (exportTarget.exists()) { //Alles klar, nur ändern
  6.             /*File contentFile;
  7.  
  8.             //Thumbnail holen um MD5 zu berechnen
  9.             File oldContentFile = Zip.getFileInZip(exportTarget, "content");
  10.             String thumbnailMD5 = getTokenMD5FromFile(exportTarget);
  11.  
  12.             contentFile = new File ("content.xml");
  13.             contentFile = writeContentFile(contentFile, hero, thumbnailMD5);
  14.  
  15.             Zip.modifyZipEntry(exportTarget, new File[] {contentFile});
  16.             contentFile.delete();
  17. */
  18.             JOptionPane.showMessageDialog(this, "Bitte ein neues Token erstellen, das updaten bestehender Tokens wird noch nicht unterstützt.", "Fehler", JOptionPane.INFORMATION_MESSAGE);
  19.  
  20.         } else { //Neues file
  21.             File contentFile;
  22.             File propertiesFile;
  23.             File thumbLibFile;
  24.             File portraitFile;
  25.             File newPortraitFile;
  26.             File xmlPortraitFile;
  27.  
  28.             //Bibliotheks-ThumbnailFile klarkriegen
  29.             thumbLibFile = inStreamToFile(getClass().getResourceAsStream("/mtexport/thumbnail"), "thumbnail");
  30.             thumbLibFile.deleteOnExit();
  31.  
  32.             //Die properties.xml erstellen
  33.             propertiesFile = new File("properties.xml");
  34.             propertiesFile = writePropertiesFile(propertiesFile);
  35.  
  36.             //Und jetzt die content.xml
  37.             contentFile = new File("content.xml");
  38.             contentFile = writeContentFile(contentFile, hero, null);
  39.            
  40.             //Maptool benoetigt ein Portrait mit XML unter ./assets/ !
  41.             portraitFile = new File(heroTool.getPfadZumPortrait());
  42.             if(portraitFile.exists()) {
  43.                 //Portrait existiert. Daten werden in eine neue Datei kopiert.
  44.                 newPortraitFile = getNewPortrait(portraitFile, "assets");
  45.                 //Das Token-Image wird durch eine skallierte Kopie vom Portrait ersetzt.
  46.                 replaceWithWidthScaledBufferedImage(newPortraitFile, thumbLibFile, 50);
  47.             } else {
  48.                 //Aus dem Standard Token-Image wird ein Portrait erstellt.
  49.                 newPortraitFile = getWidthScaledPortrait(thumbLibFile, "assets", 400);
  50.             }
  51.  
  52.             //Und rein damit in die zip!
  53.             Vector zipFiles = new Vector();
  54.             zipFiles.add(propertiesFile);
  55.             zipFiles.add(contentFile);
  56.             zipFiles.add(thumbLibFile);
  57.             zipFiles.add(newPortraitFile);
  58.             Zip.packArchive(exportTarget, zipFiles);
  59.  
  60.             propertiesFile.delete();
  61.             contentFile.delete();
  62.             thumbLibFile.delete();
  63.             newPortraitFile.delete();
  64.  
  65.             JOptionPane.showMessageDialog(this, "Export erfolgreich!", "Erfolg", JOptionPane.INFORMATION_MESSAGE);
  66.         }
  67.     }  
  68.    
  69.     /**
  70.      * Kopiert nur die Daten des alten Portraits ins neue Portrait.
  71.      * Der Name des neuen Portraits enthaelt als Prefix die md5-Summe.
  72.      *
  73.      * @param oldImage
  74.      * Altes Bild von dem nur die Daten genutzt werden.
  75.      * @param destDir
  76.      * Ziel Ordner wo das neue Bild erstellt werden soll.
  77.      */
  78.     private File getNewPortrait(File oldImage, String destDir) throws Exception{
  79.         String imgType;
  80.         File newImage = null;
  81.         File assetsDir;
  82.         String md5Sum;
  83.         try {
  84.             imgType = getMimeType(oldImage.getAbsolutePath());
  85.             md5Sum = getMD5Checksum(oldImage, imgType);
  86.             assetsDir = makeDir(destDir);
  87.             newImage = new File(assetsDir.getPath()+"/"+md5Sum+"."+imgType);
  88.             newImage.createNewFile();
  89.             copyFile(oldImage.getPath(), newImage.getPath());
  90.         } catch( IOException ex){
  91.             ex.printStackTrace();
  92.         }
  93.         return newImage;
  94.     }
  95.  
  96.     /**
  97.      * Aus einem Bild soll ein neu skaliertes Bild erstellt werden.
  98.      * Nur die Weite muss angegeben werden, die neue Hoehe wird
  99.      * aus dem alten Hoehe-Weite Verhaeltnis bestimmt. Der Name
  100.      * und der Ort des neuen Bildes entsprechen dem alten Bild. Es
  101.      * werden also nur die Daten des alten Bildes ausgetauscht.
  102.      *
  103.      * @param imgData
  104.      * Enthaelt die Bild-Daten die kopiert werden sollen.
  105.      * @param oldImage
  106.      * Dieses Bild-Objekt soll mit den neuen Bild-Daten ueberschrieben
  107.      * werden.
  108.      * @param width
  109.      * Die gewuenschte Breite des neuen Bildes.
  110.      */
  111.     public static void replaceWithWidthScaledBufferedImage(File imgData, File oldImage, int width) throws Exception {
  112.         String imgType;
  113.         BufferedImage bufOldImage;
  114.         BufferedImage bufNewImage;
  115.         try {
  116.             imgType = getMimeType(imgData.getAbsolutePath());
  117.             bufOldImage = ImageIO.read(imgData);
  118.             bufNewImage = createWidthScaledImage(bufOldImage, width);
  119.             ImageIO.write(bufNewImage, imgType, oldImage);
  120.         } catch( IOException ex){
  121.             ex.printStackTrace();
  122.         }
  123.     }    
  124.    
  125.     /**
  126.      * Aus dem Thumbnail soll ein Portrait erstellt werden.
  127.      * Nur die Weite muss angegeben werden, die neue Hoehe wird
  128.      * aus dem alten Hoehe-Weite Verhaeltnis bestimmt. Der neue
  129.      * Name enthaelt die md5 und als Suffix den Datentyp.
  130.      *
  131.      * @param oldImage
  132.      * Enthaelt die Bild-Daten.
  133.      * @param destDir
  134.      * Pfad des Verzeichnisses unter dem das neue Bild gespeichert werden soll.
  135.      * @param width
  136.      * Die Breite des neuen Bildes.
  137.      */
  138.     private File getWidthScaledPortrait(File oldImage, String destDir, int width) throws Exception {
  139.         String imgType;
  140.         String md5Sum;
  141.         String md5SumTest;
  142.         BufferedImage bufOldImage;
  143.         BufferedImage bufNewImage;
  144.         File newImage = null;
  145.         File assetsDir;
  146.         try{
  147.             assetsDir = makeDir(destDir);
  148.             imgType = getMimeType(oldImage.getAbsolutePath());
  149.             bufOldImage = ImageIO.read(oldImage);
  150.             bufNewImage = createWidthScaledImage(bufOldImage, width);
  151.             md5Sum = getMD5Checksum(bufNewImage, imgType);
  152.             newImage = new File(assetsDir.getPath()+"/"+md5Sum+"."+imgType);
  153.             ImageIO.write(bufNewImage, imgType, newImage);
  154.         } catch( IOException ex){
  155.             ex.printStackTrace();
  156.         }
  157.         return newImage;
  158.     }
  159.  
  160.     public static BufferedImage createWidthScaledImage(BufferedImage oldBufImg, int newW) throws Exception {
  161.         BufferedImage newBufImg = null;
  162.         int w = oldBufImg.getWidth();
  163.         int h = oldBufImg.getHeight();
  164.         //Festes Verhaeltnis von Hoehe zu Weite.
  165.         int newH = h*newW/w;
  166.         newBufImg = new BufferedImage(newW, newH, oldBufImg.getType());
  167.         Graphics2D g = newBufImg.createGraphics();
  168.         g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
  169.                 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  170.         g.drawImage(oldBufImg, 0, 0, newW, newH, 0, 0, w, h, null);
  171.         g.dispose();        
  172.         return newBufImg;
  173.     }  
  174.  
  175.     /**
  176.      * Der ueber dirPath angegebene Ordner wird rekursiv
  177.      * geloescht und neu angelegt.
  178.      *
  179.      * @param dirPath
  180.      * Pfad zu dem zu erstellenden Verzeichnis.
  181.      */    
  182.     public static File makeDir(String dirPath) throws Exception {
  183.         File newDir;
  184.         if(new File(dirPath).exists()) {
  185.             //Erstmal Alles rekursiv loeschen
  186.             newDir = new File(dirPath);
  187.             delDir(newDir);
  188.         }
  189.         //Leeren Ordner anlegen
  190.         new File(dirPath).mkdirs();
  191.         newDir = new File(dirPath);  
  192.         return newDir;
  193.     }
  194.  
  195.     /**
  196.      * Rekursives loeschen von Verzeichnissen mit Inhalt
  197.      * und weiteren Verzeichnissen
  198.      *
  199.      * @param dirToDelete
  200.      * Oberstes Verzeichnis das geloescht werden soll
  201.      */    
  202.     public static void delDir(File dirToDelete) throws Exception {
  203.         File[] files = dirToDelete.listFiles();
  204.         for (int nIndex = 0; nIndex < files.length; ++nIndex) {
  205.             if (files[nIndex].isDirectory()) {
  206.                 delDir(files[nIndex]);
  207.             }
  208.             files[nIndex].delete();
  209.         }
  210.     }
  211.  
  212.     public static String getMimeType(String path) throws MalformedURLException, IOException {
  213.         return new File(path).toURI().toURL().openConnection().getContentType().replaceAll("^.*/", "");
  214.     }    
  215.    
  216.     public static String getMD5Checksum(BufferedImage fileImg,
  217.                                         String imgType) throws Exception {
  218.         byte[] b = createChecksum(fileImg, imgType);
  219.         String result = "";
  220.         for (int i=0; i < b.length; i++) {
  221.             result +=
  222.                 Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
  223.         }
  224.         return result;
  225.     }    
  226.    
  227.     public static byte[] createChecksum(BufferedImage bufImg, String imgType) throws Exception {
  228.         ByteArrayOutputStream os = new ByteArrayOutputStream();
  229.         ImageIO.write(bufImg, imgType, os);
  230.         InputStream fis = new ByteArrayInputStream(os.toByteArray());
  231.         byte[] buffer = new byte[1024];
  232.         MessageDigest complete = MessageDigest.getInstance("MD5");
  233.         int numRead;
  234.         do {
  235.             numRead = fis.read(buffer);
  236.             if (numRead > 0) {
  237.                 complete.update(buffer, 0, numRead);
  238.             }
  239.         } while (numRead != -1);
  240.         fis.close();
  241.         return complete.digest();
  242.     }    
  243.    
  244.     public static String getMD5Checksum(File fileImg, String imgType) throws Exception {
  245.         byte[] b = createChecksum(fileImg, imgType);
  246.         String result = "";
  247.         for (int i=0; i < b.length; i++) {
  248.             result +=
  249.                 Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
  250.         }
  251.         return result;
  252.     }  
  253.  
  254.     public static byte[] createChecksum(File fileImage, String imgType) throws Exception {
  255.         InputStream fis = new FileInputStream(fileImage);
  256.         byte[] buffer = new byte[1024];
  257.         MessageDigest complete = MessageDigest.getInstance("MD5");
  258.         int numRead;
  259.         do {
  260.             numRead = fis.read(buffer);
  261.             if (numRead > 0) {
  262.                 complete.update(buffer, 0, numRead);
  263.             }
  264.         } while (numRead != -1);
  265.         fis.close();
  266.         return complete.digest();
  267.     }  
  268.  
  269.     private static void copy(InputStream in, OutputStream out) throws IOException {
  270.         byte[] buffer = new byte[ 0xFFFF ];
  271.         for ( int len; (len = in.read(buffer)) != -1; ) {
  272.           out.write( buffer, 0, len );
  273.         }
  274.     }
  275.      
  276.     public static void copyFile(String src, String dest) throws Exception {
  277.         FileInputStream  fis = null;
  278.         FileOutputStream fos = null;
  279.      
  280.         try
  281.         {
  282.           fis = new FileInputStream( src );
  283.           fos = new FileOutputStream( dest );
  284.      
  285.           copy( fis, fos );
  286.         }
  287.         catch ( IOException e ) {
  288.           e.printStackTrace();
  289.         }
  290.         finally {
  291.           if ( fis != null )
  292.             try { fis.close(); } catch ( IOException e ) { }
  293.           if ( fos != null )
  294.             try { fos.close(); } catch ( IOException e ) { }
  295.         }
  296.       }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement