4

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)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
RTM
  • 163
  • 1
  • 9
  • Have your tried using double slashes? Seems like a pathing issue...java needs double slashes normally. //home//rtm//Desktop//xixi.webp – Petro Dec 11 '14 at 02:22
  • It is not the issue there – RTM Dec 11 '14 at 02:26
  • Well if it's throwing a nullpointer exception, then the location of xixi.webp must be wrong. Try backslashes instead, I use backslashes for windows files. "/home" would be a Linux directory, is your windows directory named /home or is it C:/home or is this nested inside it's own directory? – Petro Dec 11 '14 at 02:29
  • No,the full exception stack is 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),because imageio can't parse .webp – RTM Dec 11 '14 at 02:37
  • so what does "image == null! " mean? shouldn't it be 'image != null' or 'image == null'? – Petro Dec 11 '14 at 02:38
  • "image == null" because ImageIO.read(file1) return null,I said "imageio can't parse .webp",maybe there is problem in complie webp-imageio.jar and libwebp-imageio.dll – RTM Dec 11 '14 at 02:45
  • First weird thing I notice: instead of double slashes you should be using only one slash or double back-slashes: \\ – Daniel Dec 11 '14 at 03:27
  • OK, I tried "\\",it alse can't work.Obviously the problem is not there – RTM Dec 11 '14 at 04:12

2 Answers2

9

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.

domisum
  • 531
  • 1
  • 7
  • 12
RTM
  • 163
  • 1
  • 9
0

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";
anderson j mariño o.
  • 1,739
  • 2
  • 6
  • 7