Advertisement
JackoJammie

Untitled

Jul 14th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.87 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.             delDir(new File("assets"));
  65.  
  66.             JOptionPane.showMessageDialog(this, "Export erfolgreich!", "Erfolg", JOptionPane.INFORMATION_MESSAGE);
  67.         }
  68.     }
  69.    
  70.     /**
  71.      * Kopiert nur die Daten des alten Portraits ins neue Portrait.
  72.      * Der Name des neuen Portraits enthaelt als Prefix die md5-Summe.
  73.      *
  74.      * @param oldImage
  75.      * Altes Bild von dem nur die Daten genutzt werden.
  76.      * @param destDir
  77.      * Ziel Ordner wo das neue Bild erstellt werden soll.
  78.      */
  79.     private File getNewPortrait(File oldImage, String destDir) throws Exception{
  80.         String imgType;
  81.         File newImage = null;
  82.         File assetsDir;
  83.         String md5Sum;
  84.         try {
  85.             imgType = getMimeType(oldImage.getAbsolutePath());
  86.             md5Sum = getMD5Checksum(oldImage, imgType);
  87.             assetsDir = makeDir(destDir);
  88.             newImage = new File(assetsDir.getPath()+"/"+md5Sum+"."+imgType);
  89.             newImage.createNewFile();
  90.             copyFile(oldImage.getPath(), newImage.getPath());
  91.         } catch( IOException ex){
  92.             ex.printStackTrace();
  93.         }
  94.         return newImage;
  95.     }
  96.  
  97.     /**
  98.      * Aus einem Bild soll ein neu skaliertes Bild erstellt werden.
  99.      * Nur die Weite muss angegeben werden, die neue Hoehe wird
  100.      * aus dem alten Hoehe-Weite Verhaeltnis bestimmt. Der Name
  101.      * und der Ort des neuen Bildes entsprechen dem alten Bild. Es
  102.      * werden also nur die Daten des alten Bildes ausgetauscht.
  103.      *
  104.      * @param imgData
  105.      * Enthaelt die Bild-Daten die kopiert werden sollen.
  106.      * @param oldImage
  107.      * Dieses Bild-Objekt soll mit den neuen Bild-Daten ueberschrieben
  108.      * werden.
  109.      * @param width
  110.      * Die gewuenschte Breite des neuen Bildes.
  111.      */
  112.     public static void replaceWithWidthScaledBufferedImage(File imgData, File oldImage, int width) throws Exception {
  113.         String imgType;
  114.         BufferedImage bufOldImage;
  115.         BufferedImage bufNewImage;
  116.         try {
  117.             imgType = getMimeType(imgData.getAbsolutePath());
  118.             bufOldImage = ImageIO.read(imgData);
  119.             bufNewImage = createWidthScaledImage(bufOldImage, width);
  120.             ImageIO.write(bufNewImage, imgType, oldImage);
  121.         } catch( IOException ex){
  122.             ex.printStackTrace();
  123.         }
  124.     }    
  125.    
  126.     /**
  127.      * Aus dem Thumbnail soll ein Portrait erstellt werden, wenn das Portrait
  128.      * nicht existiert. Nur die Weite muss angegeben werden, die neue Hoehe
  129.      * wird aus dem alten Hoehe-Weite Verhaeltnis bestimmt. Der neue Name
  130.      * enthaelt die md5 und als Suffix den Datentyp.
  131.      *
  132.      * @param oldImage
  133.      * Enthaelt die Bild-Daten des Thumbnail's.
  134.      * @param destDir
  135.      * Pfad des Verzeichnisses unter dem das neue Bild gespeichert werden soll.
  136.      * @param width
  137.      * Die Breite des neuen Bildes.
  138.      */
  139.     private File getWidthScaledPortrait(File oldImage, String destDir, int width) throws Exception {
  140.         String imgType;
  141.         String md5Sum;
  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.     /**
  161.      * Diese Methode uebernimmt das eigentliche skalieren eines gepufferten
  162.      * Bildes.
  163.      *
  164.      * @param oldBufImg
  165.      * Zu skalierendes, gepuffertes Bild.
  166.      * @param newW
  167.      * Die neue Breite des gepufferten Bildes.
  168.      */
  169.     public static BufferedImage createWidthScaledImage(BufferedImage oldBufImg, int newW) throws Exception {
  170.         BufferedImage newBufImg = null;
  171.         int w = oldBufImg.getWidth();
  172.         int h = oldBufImg.getHeight();
  173.         //Festes Verhaeltnis von Hoehe zu Weite.
  174.         int newH = h*newW/w;
  175.         newBufImg = new BufferedImage(newW, newH, oldBufImg.getType());
  176.         Graphics2D g = newBufImg.createGraphics();
  177.         g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
  178.                 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  179.         g.drawImage(oldBufImg, 0, 0, newW, newH, 0, 0, w, h, null);
  180.         g.dispose();        
  181.         return newBufImg;
  182.     }  
  183.  
  184.     /**
  185.      * Der ueber dirPath angegebene Ordner wird rekursiv
  186.      * geloescht und neu angelegt.
  187.      *
  188.      * @param dirPath
  189.      * Pfad zu dem zu erstellenden Verzeichnis.
  190.      */    
  191.     public static File makeDir(String dirPath) throws Exception {
  192.         File newDir;
  193.         if(new File(dirPath).exists()) {
  194.             //Erstmal Alles rekursiv loeschen
  195.             newDir = new File(dirPath);
  196.             delDir(newDir);
  197.         }
  198.         //Leeren Ordner anlegen
  199.         new File(dirPath).mkdirs();
  200.         newDir = new File(dirPath);  
  201.         return newDir;
  202.     }
  203.  
  204.     /**
  205.      * Rekursives loeschen von Verzeichnissen mit Inhalt
  206.      * und weiteren Verzeichnissen
  207.      *
  208.      * @param dirToDelete
  209.      * Oberstes Verzeichnis das geloescht werden soll
  210.      */    
  211.     public static void delDir(File dirToDelete) throws Exception {
  212.         File[] files = dirToDelete.listFiles();
  213.         for (int nIndex = 0; nIndex < files.length; ++nIndex) {
  214.             if (files[nIndex].isDirectory()) {
  215.                 delDir(files[nIndex]);
  216.             }
  217.             files[nIndex].delete();
  218.         }
  219.     }
  220.  
  221.     /**
  222.      * Ueberprueft eine Datei(lokal auf der Platte oder extern im Internet)
  223.      * auf den wahren Datentyp. Es werden spezielle Bytes in der Datei
  224.      * zur Typisierung hernangezogen und nicht die Datei-Endung. Zurueck
  225.      * gegeben wird der Dateityp.
  226.      * @param path
  227.      * Pfad zur Datei.
  228.      * @return
  229.      * Zureuck gegeben wird ein String wie z.b. "png".
  230.      */
  231.     public static String getMimeType(String path) throws MalformedURLException, IOException {
  232.         return new File(path).toURI().toURL().openConnection().getContentType().replaceAll("^.*/", "");
  233.     }    
  234.    
  235.     /**
  236.      * Berechnet die MD5 eines gepufferten Bildes und gibt diese zurueck.
  237.      * Vorteil ist, dass die Datei noch nicht existieren muss. Erst das
  238.      * Bild bearbeiten und anschließend benennen und erstellen.
  239.      *
  240.      * @param fileImg
  241.      * Das gepufferte Bild welches berechnet werden soll.
  242.      * @param imgType
  243.      * Der Bildtyp des gepufferten Bildes, z.b. "png".
  244.      * @return
  245.      * Enthaelt die MD5 Summe als String.
  246.      */
  247.     public static String getMD5Checksum(BufferedImage fileImg,
  248.                                         String imgType) throws Exception {
  249.         byte[] b = createChecksum(fileImg, imgType);
  250.         String result = "";
  251.         for (int i=0; i < b.length; i++) {
  252.             result +=
  253.                 Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
  254.         }
  255.         return result;
  256.     }    
  257.    
  258.     /**
  259.      * Diese Methode berechnet die notwendige MessageDigest fuer die MD5
  260.      * Berechnung. Sie sollte daher nicht direkt aufgerufen werden.
  261.      *
  262.      * @param bufImg
  263.      * Das gepufferte Bild welches berechnet werden soll.
  264.      * @param imgType
  265.      * Ist der Bild-Typ z.b. "png".
  266.      */
  267.     private static byte[] createChecksum(BufferedImage bufImg, String imgType) throws Exception {
  268.         ByteArrayOutputStream os = new ByteArrayOutputStream();
  269.         ImageIO.write(bufImg, imgType, os);
  270.         InputStream fis = new ByteArrayInputStream(os.toByteArray());
  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.     /**
  285.      * Berechnet die MD5 eines Bildes vom Typ File und gibt diese zurueck.
  286.      *
  287.      * @param fileImg
  288.      * Das zu berechnende Bild vom Typ File.
  289.      * @param imgType
  290.      * Der Bildtyp des gepufferten Bildes, z.b. "png".
  291.      * @return
  292.      * Enthaelt die MD5 Summe als String.
  293.      */
  294.     public static String getMD5Checksum(File fileImg, String imgType) throws Exception {
  295.         byte[] b = createChecksum(fileImg, imgType);
  296.         String result = "";
  297.         for (int i=0; i < b.length; i++) {
  298.             result +=
  299.                 Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
  300.         }
  301.         return result;
  302.     }  
  303.  
  304.     /**
  305.      * Diese Methode berechnet die notwendige MessageDigest fuer die MD5
  306.      * Berechnung. Sie sollte daher nicht direkt aufgerufen werden.
  307.      *
  308.      * @param fileImage
  309.      * Das zu berechnende Bild vom Typ File.
  310.      * @param imgType
  311.      * Ist der Bild-Typ z.b. "png".
  312.      */
  313.     private static byte[] createChecksum(File fileImage, String imgType) throws Exception {
  314.         InputStream fis = new FileInputStream(fileImage);
  315.         byte[] buffer = new byte[1024];
  316.         MessageDigest complete = MessageDigest.getInstance("MD5");
  317.         int numRead;
  318.         do {
  319.             numRead = fis.read(buffer);
  320.             if (numRead > 0) {
  321.                 complete.update(buffer, 0, numRead);
  322.             }
  323.         } while (numRead != -1);
  324.         fis.close();
  325.         return complete.digest();
  326.     }  
  327.  
  328.     /**
  329.      * Kopiert die Daten in einer Datei in eine andere Datei, aber nicht den
  330.      * Namen der Datei.
  331.      *
  332.      * @param src
  333.      * Pfad zur Quelle.
  334.      * @param dest
  335.      * Pfad zur Ziel Datei.
  336.      */
  337.     public static void copyFile(String src, String dest) throws Exception{
  338.         FileInputStream  fis = null;
  339.         FileOutputStream fos = null;
  340.         try{
  341.           fis = new FileInputStream(src);
  342.           fos = new FileOutputStream(dest);
  343.           copy( fis, fos );
  344.         }catch(IOException e){
  345.           e.printStackTrace();
  346.         }finally{
  347.           if(fis != null)
  348.             try{
  349.                 fis.close();
  350.             }catch(IOException e){ }
  351.           if(fos != null)
  352.             try {
  353.                 fos.close();
  354.             }catch (IOException e){ }
  355.         }
  356.       }    
  357.  
  358.     /**
  359.      * Wird von der Methode copyFile intern benutzt und sollte, daher nicht
  360.      * direkt aufgerufen werden.
  361.      *
  362.      * @param in
  363.      * InputStream.
  364.      * @param out
  365.      * OutputStream.
  366.      */
  367.     private static void copy(InputStream in, OutputStream out) throws IOException {
  368.         byte[] buffer = new byte[ 0xFFFF ];
  369.         for ( int len; (len = in.read(buffer)) != -1; ) {
  370.           out.write( buffer, 0, len );
  371.         }
  372.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement