Cenotso 6安装 webplib 0.4.2库,使用java调用成功


1,关于webp

http://baike.baidu.com/view/4447461.htm

WebP是Google新推出的影像技术,它可让网页图档有效进行压缩,同时又不影响图片格式兼容与实际清晰度,进而让整体网页下载速度加快。
与JPEG相同,WebP是一种有损压缩利用预测编码技术。但谷歌表示,这种格式的主要优势在于高效率。他们发现,“在质量相同的情况下,
WebP格式图像的体积要比JPEG格式图像小40%。
目前, Google放出了WebP文件解码器(libvpx)和命令行工具(webpconv),用于JPEG等格式图片与WebP格式之间的转换。

官方网站:
https://code.google.com/p/webp/
https://developers.google.com/speed/webp/

webp最新版本是 0.4.2 ,下载地址:

https://chromium.googlesource.com/webm/libwebp/+/0.4.2

http://downloads.webmproject.org/releases/webp/libwebp-0.4.2.tar.gz

https://chromium.googlesource.com/webm/libwebp/+archive/0.4.2.tar.gz

2,安装(按照0.4.0安装的)

解压缩:
tar -zxvf libwebp-0.4.0.tar.gz

进入文件:
cd libwebp-0.4.2/
安装编译环境(需要openjdk):
yum install  autoconf gcc make java-1.6.0-openjdk java-1.6.0-openjdk-devel java-1.6.0-openjdk
./configure
make & make install
默认的安装目录: 

/usr/local/lib

3,编译javalib库 

调用webp需要使用swig ,跨语言调用框架。http://www.swig.org/
cd libwebp-0.4.2/swig/

openjdk安装目录为:/usr/lib/jvm/java-openjdk

所以编译命令为:
gcc -shared -fPIC -fno-strict-aliasing -O2 -I/usr/lib/jvm/java-openjdk/include -I/usr/lib/jvm/java-openjdk/include/linux  libwebp_java_wrap.c  -lwebp  -o /usr/local/lib/libwebp_jni.so
直接将so文件安装到/usr/local/lib

如果编译错误:(则openjdk安装目录有问题,重新安装)
 #include <jni.h>
                 ^
compilation terminated.
jni.h在目录:/usr/lib/jvm/java-openjdk/include


 #include "jni_md.h"
                    ^
compilation terminated.
jni_md.h在目录:/usr/lib/jvm/java-openjdk/include/linux

编写java代码:
import com.google.webp.libwebp;

import java.lang.reflect.Method;

public class libwebp_jni_example {
  static {
    System.out.println("###################");
    System.out.println(System.getProperty("java.library.path"));
    System.loadLibrary("webp_jni");
  }

  /**
   * usage: java -cp libwebp.jar:. libwebp_jni_example
   */
  public static void main(String argv[]) {
    final int version = libwebp.WebPGetDecoderVersion();
    System.out.println("libwebp version: " + Integer.toHexString(version));

    System.out.println("libwebp methods:");
    final Method[] libwebpMethods = libwebp.class.getDeclaredMethods();
    for (int i = 0; i < libwebpMethods.length; i++) {
      System.out.println(libwebpMethods[i]);
    }
  }
}

编译java代码:(检查libwebp.jar jar是否在当前目录)
javac libwebp_jni_example.java -cp libwebp.jar 

运行java代码:

java -cp libwebp.jar:. -Djava.library.path=/usr/local/lib libwebp_jni_example 

如果有问题:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no webp_jni in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
        at java.lang.Runtime.loadLibrary0(Runtime.java:849)
        at java.lang.System.loadLibrary(System.java:1088)
        at libwebp_jni_example.<clinit>(libwebp_jni_example.java:9)
说明/usr/local/lib没有libwebp_jni.so,检查是否编译so文件,文件大小是否正确。

如果有问题:
Exception in thread "main" java.lang.UnsatisfiedLinkError: /usr/local/lib/libwebp_jni.so: libwebp.so.5: cannot open shared object file: No such file or directory
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1965)
        at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1890)
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1880)
        at java.lang.Runtime.loadLibrary0(Runtime.java:849)
        at java.lang.System.loadLibrary(System.java:1088)
        at libwebp_jni_example.<clinit>(libwebp_jni_example.java:9)
说明目录LD_LIBRARY_PATH和java.library.path目录不一致。或者libwebp.so文件有问题,比如大小为0,则可能编译有问题,目前在centos6编译成功。

则设置环境变量:export LD_LIBRARY_PATH=/usr/local/lib

/usr/local/lib目录下面文件如下:

-rw-r--r--. 1 root root 4412068 Feb  3 16:40 libwebp.a
-rwxr-xr-x. 1 root root   37138 Feb  3 16:44 libwebp_jni.so
-rwxr-xr-x. 1 root root     952 Feb  3 16:40 libwebp.la
lrwxrwxrwx. 1 root root      16 Feb  3 16:40 libwebp.so -> libwebp.so.5.0.2
lrwxrwxrwx. 1 root root      16 Feb  3 16:40 libwebp.so.5 -> libwebp.so.5.0.2
-rwxr-xr-x. 1 root root 2389782 Feb  3 16:40 libwebp.so.5.0.2
drwxr-xr-x. 2 root root      23 Feb  3 16:40 pkgconfig


4,如果都没有问题,则运行成功结果如下:

###################
/usr/local/lib
libwebp version: 400
libwebp methods:
public static int com.google.webp.libwebp.WebPGetDecoderVersion()
public static byte[] com.google.webp.libwebp.WebPDecodeRGBA(byte[],long,int[],int[])
public static byte[] com.google.webp.libwebp.WebPDecodeARGB(byte[],long,int[],int[])
public static int com.google.webp.libwebp.WebPGetInfo(byte[],long,int[],int[])
public static byte[] com.google.webp.libwebp.WebPDecodeRGB(byte[],long,int[],int[])
public static byte[] com.google.webp.libwebp.WebPDecodeBGR(byte[],long,int[],int[])
public static byte[] com.google.webp.libwebp.WebPDecodeBGRA(byte[],long,int[],int[])
public static int com.google.webp.libwebp.WebPGetEncoderVersion()
private static byte[] com.google.webp.libwebp.wrap_WebPEncodeRGB(byte[],int,int,int[],int,int,int,float)
private static byte[] com.google.webp.libwebp.wrap_WebPEncodeBGR(byte[],int,int,int[],int,int,int,float)
private static byte[] com.google.webp.libwebp.wrap_WebPEncodeRGBA(byte[],int,int,int[],int,int,int,float)
private static byte[] com.google.webp.libwebp.wrap_WebPEncodeBGRA(byte[],int,int,int[],int,int,int,float)
private static byte[] com.google.webp.libwebp.wrap_WebPEncodeLosslessRGB(byte[],int,int,int[],int,int,int)
private static byte[] com.google.webp.libwebp.wrap_WebPEncodeLosslessBGR(byte[],int,int,int[],int,int,int)
private static byte[] com.google.webp.libwebp.wrap_WebPEncodeLosslessRGBA(byte[],int,int,int[],int,int,int)
private static byte[] com.google.webp.libwebp.wrap_WebPEncodeLosslessBGRA(byte[],int,int,int[],int,int,int)
public static byte[] com.google.webp.libwebp.WebPEncodeRGB(byte[],int,int,int,float)
public static byte[] com.google.webp.libwebp.WebPEncodeRGBA(byte[],int,int,int,float)
public static byte[] com.google.webp.libwebp.WebPEncodeBGR(byte[],int,int,int,float)
public static byte[] com.google.webp.libwebp.WebPEncodeBGRA(byte[],int,int,int,float)
public static byte[] com.google.webp.libwebp.WebPEncodeLosslessRGB(byte[],int,int,int)
public static byte[] com.google.webp.libwebp.WebPEncodeLosslessRGBA(byte[],int,int,int)
public static byte[] com.google.webp.libwebp.WebPEncodeLosslessBGR(byte[],int,int,int)
public static byte[] com.google.webp.libwebp.WebPEncodeLosslessBGRA(byte[],int,int,int)




上一篇:8.3. Spring Cloud 相关的 application.properties 配置


下一篇:RDIFramework.NET框架SOA解决方案(集Windows服务、WinForm形式与IIS形式发布)-分布式应用