Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void exportHero(int exportHero, File exportTarget) throws XMLStreamException, NoSuchAlgorithmException, FileNotFoundException, IOException, Exception {
- PluginHeld2 hero = heroesList[exportHero];
- heroTool.setAktivenHeld(hero);
- if (exportTarget.exists()) { //Alles klar, nur ändern
- /*File contentFile;
- //Thumbnail holen um MD5 zu berechnen
- File oldContentFile = Zip.getFileInZip(exportTarget, "content");
- String thumbnailMD5 = getTokenMD5FromFile(exportTarget);
- contentFile = new File ("content.xml");
- contentFile = writeContentFile(contentFile, hero, thumbnailMD5);
- Zip.modifyZipEntry(exportTarget, new File[] {contentFile});
- contentFile.delete();
- */
- JOptionPane.showMessageDialog(this, "Bitte ein neues Token erstellen, das updaten bestehender Tokens wird noch nicht unterstützt.", "Fehler", JOptionPane.INFORMATION_MESSAGE);
- } else { //Neues file
- File contentFile;
- File propertiesFile;
- File thumbLibFile;
- File portraitFile;
- File newPortraitFile;
- File xmlPortraitFile;
- //Bibliotheks-ThumbnailFile klarkriegen
- thumbLibFile = inStreamToFile(getClass().getResourceAsStream("/mtexport/thumbnail"), "thumbnail");
- thumbLibFile.deleteOnExit();
- //Die properties.xml erstellen
- propertiesFile = new File("properties.xml");
- propertiesFile = writePropertiesFile(propertiesFile);
- //Und jetzt die content.xml
- contentFile = new File("content.xml");
- contentFile = writeContentFile(contentFile, hero, null);
- //Maptool benoetigt ein Portrait mit XML unter ./assets/ !
- //portraitFile = getPortraitImg(heroTool.getPfadZumPortrait());
- portraitFile = new File(heroTool.getPfadZumPortrait());
- if(portraitFile.exists()) {
- //Portrait existiert. Daten werden in eine neue Datei kopiert.
- newPortraitFile = getNewPortrait(portraitFile, "assets");
- //Das Token-Image wird durch eine skallierte Kopie vom Portrait ersetzt.
- replaceWithWidthScaledBufferedImage(newPortraitFile, thumbLibFile, 50);
- } else {
- //Aus dem Standard Token-Image wird ein Portrait erstellt.
- newPortraitFile = getWidthScaledPortrait(thumbLibFile, "assets", 400);
- }
- //Und rein damit in die zip!
- Vector zipFiles = new Vector();
- zipFiles.add(propertiesFile);
- zipFiles.add(contentFile);
- zipFiles.add(thumbLibFile);
- zipFiles.add(newPortraitFile);
- Zip.packArchive(exportTarget, zipFiles);
- propertiesFile.delete();
- contentFile.delete();
- thumbLibFile.delete();
- newPortraitFile.delete();
- JOptionPane.showMessageDialog(this, "Export erfolgreich!", "Erfolg", JOptionPane.INFORMATION_MESSAGE);
- }
- }
- /**
- * Liefert das Portrait Bild zurueck, wenn es schon
- * existiert.
- */
- private File getPortraitImg(String pathToImg) throws Exception {
- File portraitFile;
- if(pathToImg.isEmpty()) {
- portraitFile = null;
- } else {
- portraitFile = new File(pathToImg);
- }
- return portraitFile;
- }
- /**
- * Kopiert nur die Daten des alten Portraits ins neue Portrait.
- * Der Name des neuen Portraits enthaelt als Prefix die md5-Summe.
- *
- * @param oldImage
- * Altes Bild von dem nur die Daten genutzt werden.
- * @param destDir
- * Ziel Ordner wo das neue Bild erstellt werden soll.
- */
- private File getNewPortrait(File oldImage, String destDir) throws Exception{
- String imgType;
- File newImage = null;
- File assetsDir;
- String md5Sum;
- try {
- imgType = getMimeType(oldImage.getAbsolutePath());
- md5Sum = getMD5Checksum(oldImage, imgType);
- assetsDir = makeDir(destDir);
- newImage = new File(assetsDir.getPath()+"/"+md5Sum+"."+imgType);
- newImage.createNewFile();
- copyFile(oldImage.getPath(), newImage.getPath());
- } catch( IOException ex){
- ex.printStackTrace();
- }
- return newImage;
- }
- /**
- * Aus einem Bild soll ein neu skaliertes Bild erstellt werden.
- * Nur die Weite muss angegeben werden, die neue Hoehe wird
- * aus dem alten Hoehe-Weite Verhaeltnis bestimmt. Der Name
- * und der Ort des neuen Bildes entsprechen dem alten Bild. Es
- * werden also nur die Daten des alten Bildes ausgetauscht.
- *
- * @param imgData
- * Enthaelt die Bild-Daten die kopiert werden sollen.
- * @param oldImage
- * Dieses Bild-Objekt soll mit den neuen Bild-Daten ueberschrieben
- * werden.
- * @param width
- * Die gewuenschte Breite des neuen Bildes.
- */
- public static void replaceWithWidthScaledBufferedImage(File imgData, File oldImage, int width) throws Exception {
- String imgType;
- BufferedImage bufOldImage;
- BufferedImage bufNewImage;
- try {
- imgType = getMimeType(imgData.getAbsolutePath());
- bufOldImage = ImageIO.read(imgData);
- bufNewImage = createWidthScaledImage(bufOldImage, width);
- ImageIO.write(bufNewImage, imgType, oldImage);
- } catch( IOException ex){
- ex.printStackTrace();
- }
- }
- /**
- * Aus dem Thumbnail soll ein Portrait erstellt werden.
- * Nur die Weite muss angegeben werden, die neue Hoehe wird
- * aus dem alten Hoehe-Weite Verhaeltnis bestimmt. Der neue
- * Name enthaelt die md5 und als Suffix den Datentyp.
- *
- * @param oldImage
- * Enthaelt die Bild-Daten.
- * @param destDir
- * Pfad des Verzeichnisses unter dem das neue Bild gespeichert werden soll.
- * @param width
- * Die Breite des neuen Bildes.
- */
- private File getWidthScaledPortrait(File oldImage, String destDir, int width) throws Exception {
- String imgType;
- String md5Sum;
- String md5SumTest;
- BufferedImage bufOldImage;
- BufferedImage bufNewImage;
- File newImage = null;
- File assetsDir;
- try{
- assetsDir = makeDir(destDir);
- imgType = getMimeType(oldImage.getAbsolutePath());
- bufOldImage = ImageIO.read(oldImage);
- bufNewImage = createWidthScaledImage(bufOldImage, width);
- md5Sum = getMD5Checksum(bufNewImage, imgType);
- newImage = new File(assetsDir.getPath()+"/"+md5Sum+"."+imgType);
- ImageIO.write(bufNewImage, imgType, newImage);
- } catch( IOException ex){
- ex.printStackTrace();
- }
- return newImage;
- }
- public static BufferedImage createWidthScaledImage(BufferedImage oldBufImg, int newW) throws Exception {
- BufferedImage newBufImg = null;
- int w = oldBufImg.getWidth();
- int h = oldBufImg.getHeight();
- //Festes Verhaeltnis von Hoehe zu Weite.
- int newH = h*newW/w;
- newBufImg = new BufferedImage(newW, newH, oldBufImg.getType());
- Graphics2D g = newBufImg.createGraphics();
- g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
- RenderingHints.VALUE_INTERPOLATION_BILINEAR);
- g.drawImage(oldBufImg, 0, 0, newW, newH, 0, 0, w, h, null);
- g.dispose();
- return newBufImg;
- }
- /**
- * Der ueber dirPath angegebene Ordner wird rekursiv
- * geloescht und neu angelegt.
- *
- * @param dirPath
- * Pfad zu dem zu erstellenden Verzeichnis.
- */
- public static File makeDir(String dirPath) throws Exception {
- File newDir;
- if(new File(dirPath).exists()) {
- //Erstmal Alles rekursiv loeschen
- newDir = new File(dirPath);
- delDir(newDir);
- }
- //Leeren Ordner anlegen
- new File(dirPath).mkdirs();
- newDir = new File(dirPath);
- return newDir;
- }
- /**
- * Rekursives loeschen von Verzeichnissen mit Inhalt
- * und weiteren Verzeichnissen
- *
- * @param dirToDelete
- * Oberstes Verzeichnis das geloescht werden soll
- */
- public static void delDir(File dirToDelete) throws Exception {
- File[] files = dirToDelete.listFiles();
- for (int nIndex = 0; nIndex < files.length; ++nIndex) {
- if (files[nIndex].isDirectory()) {
- delDir(files[nIndex]);
- }
- files[nIndex].delete();
- }
- }
- public static String getMimeType(String path) throws MalformedURLException, IOException {
- return new File(path).toURI().toURL().openConnection().getContentType().replaceAll("^.*/", "");
- }
- public static String getMD5Checksum(BufferedImage fileImg,
- String imgType) throws Exception {
- byte[] b = createChecksum(fileImg, imgType);
- String result = "";
- for (int i=0; i < b.length; i++) {
- result +=
- Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
- }
- return result;
- }
- public static byte[] createChecksum(BufferedImage bufImg, String imgType) throws Exception {
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- ImageIO.write(bufImg, imgType, os);
- InputStream fis = new ByteArrayInputStream(os.toByteArray());
- byte[] buffer = new byte[1024];
- MessageDigest complete = MessageDigest.getInstance("MD5");
- int numRead;
- do {
- numRead = fis.read(buffer);
- if (numRead > 0) {
- complete.update(buffer, 0, numRead);
- }
- } while (numRead != -1);
- fis.close();
- return complete.digest();
- }
- public static String getMD5Checksum(File fileImg, String imgType) throws Exception {
- byte[] b = createChecksum(fileImg, imgType);
- String result = "";
- for (int i=0; i < b.length; i++) {
- result +=
- Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
- }
- return result;
- }
- public static byte[] createChecksum(File fileImage, String imgType) throws Exception {
- InputStream fis = new FileInputStream(fileImage);
- byte[] buffer = new byte[1024];
- MessageDigest complete = MessageDigest.getInstance("MD5");
- int numRead;
- do {
- numRead = fis.read(buffer);
- if (numRead > 0) {
- complete.update(buffer, 0, numRead);
- }
- } while (numRead != -1);
- fis.close();
- return complete.digest();
- }
- private static void copy(InputStream in, OutputStream out) throws IOException {
- byte[] buffer = new byte[ 0xFFFF ];
- for ( int len; (len = in.read(buffer)) != -1; ) {
- out.write( buffer, 0, len );
- }
- }
- public static void copyFile(String src, String dest) throws Exception {
- FileInputStream fis = null;
- FileOutputStream fos = null;
- try
- {
- fis = new FileInputStream( src );
- fos = new FileOutputStream( dest );
- copy( fis, fos );
- }
- catch ( IOException e ) {
- e.printStackTrace();
- }
- finally {
- if ( fis != null )
- try { fis.close(); } catch ( IOException e ) { }
- if ( fos != null )
- try { fos.close(); } catch ( IOException e ) { }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement