RTM
RTM

Reputation: 173

How to convert webp to png or jpg using java in windows?

I got bitbucket.org/luciad/webp-imageio to work in Ubuntu, but I can't get it to work in Windows.

Here is what I do in Ubuntu:

  1. Download webp-imageio and libwebp source code (other versions can be found in the google webp downloads repository).

  2. Using cmake to compile libwebp and webp-imageio, there is a CMakefile.txt file in webp-imageio. Maybe you need to modify it? Then you will get webp-imageio.jar and libwebp-imageio.so (it will be .dll in windows)

  3. Put libwebp-imageio.so in the java project Native library location and webp-imageio.jar in the java build path.

  4. Then, run the following code:

File file1= new File("/home/rtm/Desktop/xixi.webp");  
File file2= new File("/home/rtm/Desktop/haha.png");  

System.loadLibrary("webp-imageio");
try {  
    BufferedImage im = ImageIO.read(file1);   
    ImageIO.write(im, "png", file2);  
} catch (IOException e) {  
    e.printStackTrace();  
}  
  1. Then, I use cmake and mingw-w64, compile it in windows (webp-imageio.jar and libwebp-imageio.dll). That doesn't work, however, as ImageIO.read(file1); returns null. WHY?

Here is my code for Windows:

File file1 = new File("D://workspace//demo//Test//unnamed.webp");
File file2 = new File("D://workspace//demo//Test//xixi.png");

System.loadLibrary("webp-imageio");
try {
    //FileUtils.copyFile(file1, file2);
    BufferedImage im = ImageIO.read(file1);
    ImageIO.write(im, "png", file2);
} catch (Exception e) {
    e.printStackTrace();
}

Here is the exception stack:

java.lang.IllegalArgumentException: image == null!
at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(Unknown Source)
at javax.imageio.ImageIO.getWriter(Unknown Source)
at javax.imageio.ImageIO.write(Unknown Source)

Upvotes: 4

Views: 10887

Answers (2)

RTM
RTM

Reputation: 173

well,I solved this problem by using google precompiled WebP utilities and library.It just need libWebp,you can find other version to match your system in http://downloads.webmproject.org/releases/webp/index.html. Then execute it in java,then is the code:

    //the "dwebp.exe"'s path
    String str1 = "D:/workspace/demo/Test/libwebp-1.3.0-windows-x64/bin/dwebp.exe";
    //the webp picture's path
    String str2 = "D:/workspace/demo/Test/unnamed.webp";
    //the converted picture's path
    String str3 = "D:/workspace/demo/Test/xixi.png";
    args = new String[]{str1, str2, "-o", str3};

    try {
        Runtime.getRuntime().exec(args);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

It can convert webp to PNG, JPEG, TIFF, WebP or raw Y'CbCr samples.

Upvotes: 10

You can too convert png for example to webp, following the same example from @RMT change 'dwebp.exe' by 'cwebp.exe', it's work for me.

//the "cwebp.exe"'s path
     String str1 = "C:\\test\\bin\\cwebp.exe";
//the png picture's path
     String str2 = "C:\\test\\xixi.png";
//the converted picture's path
    String str3 = "C:\\test\\xixie.webp";

Upvotes: 0

Related Questions