Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

webp

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)
like image 469
RTM Avatar asked Oct 21 '25 13:10

RTM


1 Answers

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.

like image 123
RTM Avatar answered Oct 23 '25 05:10

RTM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!