Advertisement
JackoJammie

Untitled

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