How to read OS X *.icns file with Java

旧城冷巷雨未停 提交于 2020-01-04 05:22:14

问题


I want to read *.icns files in OS X into a BufferedImage. Help


回答1:


Try this: http://code.google.com/p/appengine-awt/source/browse/trunk/apache-sanselan/src/main/java/org/apache/sanselan/formats/icns/IcnsDecoder.java?spec=svn8&r=8

Which is actually from: http://incubator.apache.org/sanselan/site/index.html




回答2:


You need to convert ICNS to another image type first, and after load this image you can delete it. This is how to convert PNG to ICNS, so you just need to do in the opposite way:

public static void Png(File png, File icns) throws IOException{
    ImageIcon image = new ImageIcon(ImageIO.read(png));
    ImageIconAs(image, icns);
}

public static void ImageIconAs(ImageIcon ii, File icns) throws IOException{IconAs((Icon)ii,icns);}

public static void IconAs(Icon icon, File icns) throws IOException{
        if (icon != null) {
                BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB );
                Graphics2D g = bi.createGraphics();
                icon.paintIcon(new Canvas(), g, 0, 0 );
                g.dispose();
                File outputfile = new File("temp000.png");
                ImageIO.write(bi, "png", outputfile);
                execTerminal(new String[]{ "sips", "-s", "format", "tiff", 
                        "temp000.png","--out", "temp000.tiff" });  
                File apaga2 = new File("temp000.png");
                apaga2.delete();
                execTerminal(new String[]{ "tiff2icns", "-noLarge", 
                        "temp000.tiff", icns.getAbsolutePath()});
                File apaga = new File("temp000.tiff");
                apaga.delete();
        }
    }

static void execTerminal(String[] cmd){
        int exitCode = 0;
        try {
            exitCode = Runtime.getRuntime().exec(cmd).waitFor();
        } 
        catch (InterruptedException e) {e.printStackTrace();}
        catch (IOException e) {
            if (exitCode != 0) System.out.println("ln signaled an error with exit code " + exitCode);
        }
    }

You just need to use this to call the action:

Png(png_file,icns_file);



来源:https://stackoverflow.com/questions/3757677/how-to-read-os-x-icns-file-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!